     ###########################################################
 
     updateEnvelope: ->
-
+        if @envelopeReset
+            @envelopeReset = false
+            @envelopeCycle = @envelopePeriod
+            @envelope = 0xF
+        else if @envelopeCycle > 0
+            @envelopeCycle--
+        else if @envelope > 0 # Envelope cycle is 0
+            @envelopeCycle = @envelopePeriod
+            @envelope--
+        else if @envelopeLoop # Envelope is 0
+            @envelope = 0xF
 
     ###########################################################
     # Length counter

        value

    setTimer: (value) ->
         @timerPeriod = (@timerPeriod or 0) & 0x700 | (value & 0xFF) # Low 8 bits

    ###########################################################
    # Length counter / Timer register

        value

    setLenghtCounter: (value) ->
        @timerPeriod = (@timerPeriod or 0) & 0x0FF | (value & 0x7) << 8 # Higher 3 bits of timer
        @lengthCounter = (value & 0xF8) >>> 3  # Length counter value
        @dutyPosition = 0                      # Output waveform position is reseted
        @envelopeReload = true                 # Envelope and its divider will be reseted

    ###########################################################
    # Tick
