def testEmpty(self): inputFilename = join(testdata.audio_dir, 'generated', 'empty', 'empty.aiff') # NOTE: AudioLoader will through exception on "empty.wav" complaining that # it cannot read stream info, using "empty.aiff" therefore... loader = AudioLoader(filename=inputFilename) mixer = MonoMixer(type='left') pool = Pool() loader.audio >> mixer.audio mixer.audio >> (pool, "mix") loader.numberChannels >> mixer.numberChannels loader.sampleRate >> None loader.md5 >> None run(loader) self.assertEqualVector(pool.descriptorNames(), [])
def testEmpty(self): filename = join(testdata.audio_dir, 'generated', 'empty', 'empty.aiff') loader = AudioLoader(filename=filename) trim = StereoTrimmer(startTime=0, endTime=1, sampleRate=44100) pool = Pool() loader.audio >> trim.signal loader.numberChannels >> None loader.sampleRate >> None loader.md5 >> None loader.bit_rate >> None loader.codec >> None trim.signal >> (pool, 'slice') run(loader) self.assertEqualVector(pool.descriptorNames(), [])
def testEmpty(self): filename = join(testdata.audio_dir, 'generated', 'empty', 'empty.wav') loader = AudioLoader(filename=filename) mux = StereoDemuxer() #gen = VectorInput([{'left':None,'right':None} for i in range(10)]) pool = Pool() loader.audio >> mux.audio loader.numberChannels >> (pool, 'channels') loader.sampleRate>> (pool, 'sampleRate') mux.left >> (pool, 'left') mux.right >> (pool, 'right') run(loader) self.assertRaises(KeyError, lambda: pool['left']) self.assertRaises(KeyError, lambda: pool['right'])
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 get_audio_loaded_song(song_path: str): """Loads the file given at the path and returns the audio as a stereosample Parameters ---------- song_path : str The file path of the song Returns ------- stereosample The input stereo audio signal """ path = get_absolute_path(song_path) loader = AudioLoader(filename=path) return loader
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 testEmpty(self): filename = join(testdata.audio_dir, 'generated', 'empty', 'empty.aiff') loader = AudioLoader(filename=filename) demuxer = StereoDemuxer() muxer = StereoMuxer() p = Pool() loader.audio >> demuxer.audio loader.sampleRate >> None loader.numberChannels >> None loader.md5 >> None loader.bit_rate >> None loader.codec >> None demuxer.left >> muxer.left demuxer.right >> muxer.right muxer.audio >> (p, 'result') run(loader) self.assertEqual(p.descriptorNames(), [])
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)
def testRegression(self): # All the values have to be inside the # dynamic range [-1.0, 1.0 - 1 / 32767.0] # For the out of dynamic range case see # testSaturation. impulse = [-.95, 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 load_audio(type='mono'): raw_audio = OrderedDict() stem_audio = OrderedDict() if 'mono' in type: # loads raw audio loader = MonoLoader() for name in gNameTracks: path = gRawPath[name] loader.configure(filename=path) pool = essentia.Pool() loader.audio >> (pool, 'loader.audio') essentia.run(loader) print 'Raw track contains %d samples of Audio' % len( pool['loader.audio']) raw_audio[name] = pool['loader.audio'] essentia.reset(loader) # loads stem audio for name in gNameTracks: path = gStemPath[name] loader.configure(filename=path) pool = essentia.Pool() loader.audio >> (pool, 'loader.audio') essentia.run(loader) print 'Stem track contains %d samples of Audio' % len( pool['loader.audio']) stem_audio[name] = pool['loader.audio'] essentia.reset(loader) elif 'stereo' in type: # loads raw audio Stereo: for name in gNameTracks: path = gRawPath[name] loader = AudioLoader(filename=path) pool = essentia.Pool() loader.audio >> (pool, 'loader.audio') loader.sampleRate >> None loader.numberChannels >> None loader.md5 >> None loader.bit_rate >> None loader.codec >> None essentia.run(loader) print 'Raw Stereo track contains %d samples of Audio' % len( pool['loader.audio']) raw_audio[name] = pool['loader.audio'] essentia.reset(loader) # loads stem stereo for name in gNameTracks: path = gStemStereoPath[name] loader = AudioLoader(filename=path) pool = essentia.Pool() loader.audio >> (pool, 'loader.audio') loader.sampleRate >> None loader.numberChannels >> None loader.md5 >> None loader.bit_rate >> None loader.codec >> None essentia.run(loader) print 'Stem Stereo track contains %d samples of Audio' % len( pool['loader.audio']) stem_audio[name] = pool['loader.audio'] essentia.reset(loader) return raw_audio, stem_audio