 import Foundation
 import AVFoundation
 
-private let kDVGNoiseFloor: Float = -40.0
-
-struct DataRange {
-    let location: Double
-    let length: Double
-    
-    init(var location: Double, length: Double) {
-        assert(location >= 0.0)
-        assert(length > 0.0)
-        assert(length <= 1.0)
-        location = min(location, 1 - length)
-        
-        self.location = location
-        self.length   = length
-    }
-    
-    init() {
-        self.location = 0.0
-        self.length   = 1.0
-    }
-}
-
-enum AudioAnalizerState {
-    case Idle
-    case Reading
-    case Finished
-}
-
-class AudioSamplesSource: ChannelSource {
-    
-    //MARK: Initialization
+class AudioSamplesSource {
+
+    //MARK: - Initialization
     convenience init(asset: AVAsset) {
         self.init()
         self.asset       = asset

                    return
                }
                
                print(audioFormat)
                self!.audioFormat = audioFormat
                print(self!.audioFormat.mBitsPerChannel)
                dispatch_async(dispatch_get_main_queue()) {
                    completion(true)
                }

    }
    
    init() {
        self.createChannelsForDefaultLogicTypes()
    }
    
    //MARK: -
    //MARK: - Inner configuration
    func configureChannelsForSamplesCount(samplesCount: Int, timeRange: CMTimeRange) {
        
        let estimatedSampleCount = timeRange.duration.seconds * self.audioFormat.mSampleRate
        
        for index in self.maxValueChannels.indices {
            let channel = self.maxValueChannels[index]
            channel.totalCount = Int(Double(samplesCount) * pow(2.0, Double(index)))
            channel.blockSize  = Int(ceil(estimatedSampleCount/Double(channel.totalCount)))
        }
        
        for index in self.avgValueChannels.indices {
            let channel = self.avgValueChannels[index]
            channel.totalCount = Int(Double(samplesCount) * pow(2.0, Double(index)))
            channel.blockSize  = Int(ceil(estimatedSampleCount/Double(channel.totalCount)))
        }
    }
    
    func createChannelsForDefaultLogicTypes() {
        
        var maxValueChannels = [Channel<Int16>]()
        
        for _ in 0..<channelPerLogicProviderType {
            let channel        = Channel<Int16>(logicProvider: AudioMaxValueLogicProvider())
            channel.identifier = self.identifierForLogicProviderType(AudioMaxValueLogicProvider)
            maxValueChannels.append(channel)
        }
        
        self.maxValueChannels = maxValueChannels
        //???: Is there any reason to store Float?
        var avgValueChannels = [Channel<Float>]()
        
        for _ in 0..<channelPerLogicProviderType {
            let channel        = Channel<Float>(logicProvider: AudioAverageValueLogicProvider())
            channel.identifier = self.identifierForLogicProviderType(AudioAverageValueLogicProvider)
            avgValueChannels.append(channel)
        }
        self.avgValueChannels = avgValueChannels
    }

    //MARK: - Reading
    //TODO: There's no need in such public methods (combine with read method)
    func prepareToRead(completion: (Bool) -> ()) {
        
        assert(self.audioSource != nil, "No audio source")
