def testEmpty(self):
        inputFilename = join(testdata.audio_dir, 'generated', 'empty',
                             'empty.aiff')
        outputFilename = 'audiowritertest.aiff'
        loader = AudioLoader(filename=inputFilename)
        pool = Pool()
        writer = AudioWriter(filename=outputFilename, format='aiff')
        loader.audio >> writer.audio
        loader.numberChannels >> None
        loader.sampleRate >> None
        loader.md5 >> None
        loader.bit_rate >> None
        loader.codec >> None
        run(loader)

        loader = AudioLoader(filename=outputFilename)
        loader.audio >> (pool, 'audio')
        loader.numberChannels >> None
        loader.sampleRate >> None
        loader.md5 >> None
        loader.bit_rate >> None
        loader.codec >> None
        run(loader)
        os.remove(outputFilename)

        self.assertEqualVector(pool.descriptorNames(), [])
    def testRegression(self):
        impulse = [-1, 0.3, 0.9999, 0.5, 0.2, 0.1]
        impulsePos = [0, 111, 5013, 20013, 11359, 44099]
        filename = "audiowritertest.wav"
        size = 44100
        signal = []
        for i in range(size):
            signal.append([0, 0])
        i = 0
        for pos in impulsePos:
            signal[pos][0] = impulse[i]
            signal[pos][1] = -impulse[i]
            i += 1

        # write the audio file:
        gen = VectorInput(signal)
        writer = AudioWriter(filename=filename)
        gen.data >> writer.audio
        run(gen)

        # load the audio file and validate:
        loader = AudioLoader(filename=filename)
        pool = Pool()

        loader.audio >> (pool, 'audio')
        loader.numberChannels >> None
        loader.sampleRate >> None
        loader.md5 >> None
        loader.bit_rate >> None
        loader.codec >> None
        run(loader)
        os.remove(filename)

        self.compare(pool['audio'], signal)
    def encoderTest(self, filename, bitrate=320, precision=1e-7):
        from math import sin, pi
        format = os.path.splitext(filename)[1].split('.')[1]
        sr = 44100
        sine = [i / 44100. * sin(2.0 * pi * 10.0 * i / sr) for i in range(sr)]
        #sine = [0.5*sin(2.0*pi*10.0*i/sr) for i in xrange(sr)]
        signal = array([[val, val] for val in sine])

        # write the audio file:
        gen = VectorInput(signal)
        writer = AudioWriter(filename=filename, format=format, bitrate=bitrate)
        gen.data >> writer.audio
        run(gen)

        # load the audio file and validate:
        loader = AudioLoader(filename=filename)
        mixer = MonoMixer(type='left')
        pool = Pool()
        loader.audio >> mixer.audio
        mixer.audio >> (pool, 'audio')
        loader.numberChannels >> mixer.numberChannels
        loader.sampleRate >> None
        loader.md5 >> None
        loader.bit_rate >> None
        loader.codec >> None
        run(loader)

        from essentia.standard import ZeroCrossingRate
        zcr = int(
            ZeroCrossingRate(threshold=0.001)(pool['audio']) *
            len(pool['audio']) + 0.5)
        expected = int(ZeroCrossingRate(threshold=0.0)(sine) * len(sine) + 0.5)
        # for debugging:
        #from pylab import show, plot, figure
        #from essentia.standard import MonoLoader
        #plot(sine)
        #plot(MonoLoader(filename=filename)())
        #show(figure)

        os.remove(filename)

        self.assertAlmostEqual(max(pool['audio']), max(sine), precision)
        self.assertEqual(
            zcr, expected)  # expected should be 20 (double the frequency)
    def testSaturation(self):
        impulse = [2, -2.3, 1.9999, -1.5, 1.2, 1.1]
        impulsePos = [0, 111, 5013, 20013, 11359, 44099]
        filename = "audiowritertest.wav"
        size = 44100
        signal = []
        expected = []
        for i in range(size):
            signal.append([0, 0])
            expected.append([0, 0])

        i = 0
        # For 32 bytes floats audio ranges from
        # -1.0 to 1.0 - 1.0 / 32767.0
        for pos in impulsePos:
            signal[pos][0] = impulse[i]
            signal[pos][1] = -impulse[i]
            if signal[pos][0] > 1:
                expected[pos][0] = 1 - 1.0 / 32767.0
                expected[pos][1] = -1
            else:
                expected[pos][0] = -1
                expected[pos][1] = 1 - 1.0 / 32767.0
            i += 1

        # write the audio file:
        gen = VectorInput(signal)
        writer = AudioWriter(filename=filename)
        gen.data >> writer.audio
        run(gen)

        # load the audio file and validate:
        loader = AudioLoader(filename=filename)
        pool = Pool()

        loader.audio >> (pool, 'audio')
        loader.numberChannels >> None
        loader.sampleRate >> None
        loader.md5 >> None
        loader.bit_rate >> None
        loader.codec >> None
        run(loader)
        os.remove(filename)
        self.compare(pool['audio'], expected)
    def testSaturation(self):
        impulse = [2, -2.3, 1.9999, -1.5, 1.2, 1.1]
        impulsePos = [0, 111, 5013, 20013, 11359, 44099]
        filename = "audiowritertest.wav"
        size = 44100
        signal = []
        expected = []
        for i in range(size):
            signal.append([0, 0])
            expected.append([0, 0])

        i = 0
        # strangely, negative values saturate at -1.0 - 1.0/32767.0
        # instead of -1.0 and have the positive saturate at 1.0-1/32767
        for pos in impulsePos:
            signal[pos][0] = impulse[i]
            signal[pos][1] = -impulse[i]
            if signal[pos][0] > 1:
                expected[pos][0] = 1
                expected[pos][1] = -1-1.0/32767.0
            else:
                expected[pos][0] =  -1-1.0/32767.0
                expected[pos][1] = 1
            i+=1

        # write the audio file:
        gen = VectorInput(signal)
        writer = AudioWriter(filename=filename)
        gen.data >> writer.audio
        run(gen)

        # load the audio file and validate:
        loader = AudioLoader(filename=filename)
        pool = Pool()

        loader.audio >> (pool, 'audio')
        loader.numberChannels >> None
        loader.sampleRate >> None
        loader.md5 >> None
        run(loader)
        os.remove(filename)
        self.compare(pool['audio'], expected)
    def testOneSample(self):
        filename = "audiowritertest.wav"
        signal = array([[0.5, 0.3]])

        # write the audio file:
        gen = VectorInput(signal)
        writer = AudioWriter(filename=filename)
        gen.data >> writer.audio
        run(gen)

        # load the audio file and validate:
        loader = AudioLoader(filename=filename)
        pool = Pool()

        loader.audio >> (pool, 'audio')
        loader.numberChannels >> None
        loader.sampleRate >> None
        run(loader)
        os.remove(filename)
        self.compare(pool['audio'], signal)