 
     func setVolumeSequence(sequence:Array<Float>) {
         self.volumeSequence = sequence
+        self.renderVolumes()
     }
 
     func lengthOfSections(totalLength:UInt32, sequence: Array<Tone>) -> Array<UInt32> {
+        return lengthOfSections(totalLength, sequence: sequence.map({ (tone) -> UInt32 in
+            UInt32(tone.length)
+        }))
+    }
+
+    func lengthOfSections(totalLength:UInt32, sequence: Array<UInt32>) -> Array<UInt32> {
         var sectionLength = Array<UInt32>()
         var sectionCount:UInt32 = 0
 
         for (var i = Int(0); i < sequence.count; i++) {
-            sectionCount += UInt32(sequence[i].length)
+            sectionCount += sequence[i]
         }
 
         var currentLength:UInt32 = 0;
 
         for (var i = Int(0); i < sequence.count; i++) {
-            sectionLength.append(UInt32(sequence[i].length) * UInt32(totalLength / sectionCount))
+            sectionLength.append(UInt32(sequence[i]) * UInt32(totalLength / sectionCount))
             currentLength += sectionLength[i]
         }
 
         //spread rounding error
         var index = 0
+
         while (currentLength < totalLength) {
             sectionLength[index++ % sectionLength.count]++
             currentLength++

    func setToneSequence(sequence:Array<Tone>) {
        self.toneSequence = sequence

        self.renderFrequencyTable()
    }

    func render(buffer:AVAudioPCMBuffer) -> Bool {
        let sampleCount = Int(self.length * UInt64(SampleRate) / 1000)
        let partitionCount = sampleCount / (volumeSequence.count - 1)
        var volumeIndex = 0

        var i = Int(0)

        while (volumeIndex < volumeSequence.count - 1) {
            let current = Float(volumeSequence[volumeIndex])
            let diff = Float(volumeSequence[volumeIndex + 1]) - current

            if (self.frequencyTable.count == sampleCount) {
                for (var j = Int(0); j < partitionCount; j++) {
                    let volume = current + Float(j) * diff / Float(partitionCount)
                    let value = volume * sin(Float(i + j) * 2 * π * self.frequencyTable[i + j] / SampleRate)
                    buffer.floatChannelData.memory[j + i] = value
                    // second channel
                    buffer.floatChannelData.memory[sampleCount + j + i] = value
                }
            }
            else {
                for (var j = Int(0); j < partitionCount; j++) {
                    let volume = current + Float(j) * diff / Float(partitionCount)
                    let value = volume * sin(Float(i + j) * 2 * π * 440 / SampleRate)
                    buffer.floatChannelData.memory[j + i] = value
                    // second channel
                    buffer.floatChannelData.memory[sampleCount + j + i] = value
                }
            }
            i += partitionCount
            volumeIndex++
        }
        
        return false
    }
}
        return sectionLength
    }

    private func renderVolumes() {
        let sampleCount = UInt32(self.length * UInt64(SampleRate) / 1000)

        self.volumes = Array<Float>()

        let sectionLength = lengthOfSections(sampleCount, sequence: self.volumeSequence.map({ (volume) -> UInt32 in
                UInt32(1)
            }))

        for (var i = 0; i < sectionLength.count; i++) {
            let length = sectionLength[i]
            let volume = self.volumeSequence[i]

            for (var j:UInt32 = 0; j < length; j++) {
                volumes.append(volume)
            }
        }
    }

    private func renderFrequencies() {
        let sampleCount = UInt32(self.length * UInt64(SampleRate) / 1000)

        self.frequencies = Array<Float>()

        let sectionLength = lengthOfSections(sampleCount, sequence: self.toneSequence)

