def get_audio(self, max_framerate=settings.MAX_FRAMERATE): try: print "Getting audio locally" return wavy.get_audio(self.get_soundfile_name(), max_framerate=max_framerate) except (ValueError, SuspiciousOperation, AttributeError, IOError): print "Getting audio remotely" return self.recording.get_audio(self.offset, self.duration, max_framerate=max_framerate)
def get_audio(self, offset=0, duration=0, max_framerate=settings.MAX_FRAMERATE): return wavy.get_audio(self.path, offset=offset, duration=duration, max_framerate=max_framerate)
def handle(self, *args, **options): detectors = [SimpleKiwi(), Energy(), LowEnergy(), Amplitude()] db_detectors = [] now = time.time() for detector in detectors: try: db_detectors.append( Detector.objects.get(code=detector.code, version=detector.version)) except Detector.DoesNotExist: db_detectors.append( Detector(code=detector.code, version=detector.version, description=detector.description)) db_detectors[-1].save() detectors = zip(detectors, db_detectors) kiwi_detector = Detector.objects.get( code='simple-north-island-brown-kiwi') recordings = Recording.objects.all().order_by('?') for recording in recordings: snippets = Snippet.objects.filter(recording=recording).exclude( scores__detector=kiwi_detector).order_by('offset') if len(snippets): fid = io.open(recording.path, 'rb', buffering=BUFFER_SIZE) fid.peek(BUFFER_SIZE) with closing(fid): for snippet in snippets: try: audio = Audio(*get_audio(recording.path, snippet. offset, snippet.duration)) count = 0 for detector, db_detector in detectors: score = detector.score(audio) if not count: print '%s %0.1f %0.1f' % ( snippet, time.time() - now, score) now = time.time() try: s = Score.objects.get(detector=db_detector, snippet=snippet) s.delete() except Score.DoesNotExist: pass s = Score(detector=db_detector, snippet=snippet, score=score) s.save() count += 1 except KeyboardInterrupt: raise except WaveError: print recording.path, 'scoring failed because of a WAV error' break except: print detector, snippet, 'Scoring failed', sys.exc_info( )[0]
def get_audio(self, max_framerate=settings.MAX_FRAMERATE): try: print 'Getting audio locally' return wavy.get_audio(self.get_soundfile_name(), max_framerate=max_framerate) except (ValueError, SuspiciousOperation, AttributeError, IOError): print 'Getting audio remotely' return self.recording.get_audio(self.offset, self.duration, max_framerate=max_framerate)
def handle(self, *args, **options): detectors = [SimpleKiwi(), Energy(), LowEnergy(), Amplitude()] db_detectors = [] now = time.time() for detector in detectors: try: db_detectors.append(Detector.objects.get(code=detector.code, version=detector.version)) except Detector.DoesNotExist: db_detectors.append(Detector(code=detector.code, version=detector.version, description=detector.description)) db_detectors[-1].save() detectors = zip(detectors, db_detectors) kiwi_detector = Detector.objects.get(code = 'simple-north-island-brown-kiwi') recordings = Recording.objects.all().order_by('?') for recording in recordings: snippets = Snippet.objects.filter(recording=recording).exclude(scores__detector=kiwi_detector).order_by('offset') if len(snippets): fid = io.open(recording.path, 'rb', buffering=BUFFER_SIZE) fid.peek(BUFFER_SIZE) with closing(fid): for snippet in snippets: try: audio = Audio(*get_audio(recording.path, snippet.offset, snippet.duration)) count = 0 for detector, db_detector in detectors: score = detector.score(audio) if not count: print '%s %0.1f %0.1f' % (snippet, time.time() - now, score) now = time.time() try: s = Score.objects.get(detector=db_detector, snippet=snippet) s.delete() except Score.DoesNotExist: pass s = Score(detector=db_detector, snippet=snippet, score=score) s.save() count += 1 except KeyboardInterrupt: raise except WaveError: print recording.path, 'scoring failed because of a WAV error' break except: print detector, snippet, 'Scoring failed', sys.exc_info()[0]
def test_duration(self): audio0, framerate0 = get_audio('test/three-second.wav', duration=0.5) self.assertEqual(len(audio0), 4000) self.assertEqual(audio0[0], self.audio[0])
def test_subsampling(self): self.low_framerate.seek(0) audio, framerate = get_audio(self.low_framerate) audio3, framerate3 = get_audio('test/three-second.wav') self.assertEqual(audio[2000], audio3[4000])
def test_low_framerate(self): self.low_framerate.seek(0) audio, framerate = get_audio(self.low_framerate) self.assertEqual(len(audio), 12000) self.assertEqual(framerate, 4000)
def setUp(self): self.audio, self.framerate = get_audio('test/three-second.wav')
def test_slice(self): self.one_second.seek(0) audio3, framerate3 = get_audio('test/three-second.wav') audio1, framerate1 = get_audio(self.one_second) self.assertEqual(audio3[8000], audio1[0])
import os, wave import numpy as np import datetime import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt #from pylab import figure, specgram, savefig, imshow import wavy import sys #source="stitchbird-song.wav" source=sys.argv[1] audio=wavy.get_audio(source) print audio print(len(audio[0])) #convert to length no_channels=1.0 frequency=44100 print('Length of file is: '+str(float(len(audio[0])/(no_channels*frequency)))+' seconds') #for i in audio: # print(i) if not os.path.exists(source+"_results"): os.makedirs(source+"_results") if not os.path.exists(source+"_results/audio/bird"): os.makedirs(source+"_results/audio/bird")
def test_offset_duration(self): audio1, framerate1 = get_audio('test/three-second.wav', offset=1, duration=1) self.assertEqual(len(audio1), 8000) self.assertEqual(audio1[0], self.audio[8000])
def test_offset(self): audio2, framerate2 = get_audio('test/three-second.wav', offset=1) self.assertEqual(len(audio2), 16000) self.assertEqual(audio2[0], self.audio[8000])
def test_keep_framerate(self): audio, framerate = get_audio('test/three-second.wav', max_framerate=8000) self.assertEqual(len(audio), 24000) self.assertEqual(framerate, 8000)
def test_no_upsampling(self): audio, framerate = get_audio('test/three-second.wav', max_framerate=10000) self.assertEqual(len(audio), 24000) self.assertEqual(framerate, 8000)
def test_third_sampling(self): audio, framerate = get_audio('test/three-second.wav', max_framerate=3000) self.assertEqual(len(audio), 8000) self.assertEqual(framerate, 8000/3.0)
def test_subsampling(self): audio, framerate = get_audio('test/three-second.wav', max_framerate=6000) self.assertEqual(len(audio), 12000) self.assertEqual(framerate, 4000)
import os, wave import numpy as np import datetime import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt #from pylab import figure, specgram, savefig, imshow import wavy import sys #source="stitchbird-song.wav" source = sys.argv[1] audio = wavy.get_audio(source) print audio print(len(audio[0])) #convert to length no_channels = 2.0 frequency = 44100 print('Length of file is: ' + str(float(len(audio[0]) / (no_channels * frequency))) + ' seconds') #for i in audio: # print(i) #attempting to slice wav_file = open("stitchout.wav", 'w')
def test_slice_length(self): self.one_second.seek(0) audio1, framerate1 = get_audio(self.one_second) self.assertEqual(len(audio1), 8000) self.assertEqual(framerate1, 8000)