def handle(self, *args, **options): #reader = csv.reader(open('/home/jasonhideki/songscape/www/media/snippets/snippets.csv')) #trying out using outputs.csv instead: reader = csv.reader(open('/home/jasonhideki/songscape/output.csv')) for i, row in enumerate(reader): if i: try: path = row[0] offset = float(row[1]) duration = float(row[2]) md5 = hashlib.md5(open(path).read()).hexdigest() print i, md5, path recording = Recording.objects.get(md5=md5) snippet = Snippet.objects.get(recording=recording, offset=offset, duration=duration) wavy.slice_wave( path, os.path.join(BASE_DIR, snippet.get_soundfile_name()), offset, duration, 8000) except Recording.DoesNotExist: print "Can't find the recording on row %s at path %s" % ( i, path) except: print 'Something went wrong on row %s with recording %s' % ( i, path) raise
def save_soundfile(self, replace=False, path='', max_framerate=None): filename = self.get_soundfile_name() if not path: name = os.path.join(settings.SNIPPET_DIR, filename) path = os.path.join(settings.MEDIA_ROOT, name) else: path = os.path.join(path, filename) try: if not os.path.exists(path): colonpath = path.replace(':', '_') if os.path.exists(colonpath): filename = filename.replace(':', '_') path = colonpath replace = False else: replace = True except (ValueError, SuspiciousOperation, AttributeError): replace = True if replace: wav_file = open(path, 'w') wavy.slice_wave(settings.RECORDINGS_ROOT, wav_file, self.offset, self.duration, max_framerate=max_framerate) wav_file.close() return filename
def handle(self, *args, **options): reader = csv.reader( open( '/home/jasonhideki/songscape/www/media/snippets/snippets.csv')) #remove header row reader.next() #make a list of all the unique recording paths that don't have serial number 000000 Recordings = [] for row in reader: if row[0] not in Recordings and row[0][16:22] != '000000': Recordings.append(row[0]) #for every unique recording, pick 5 random snippet rows random_rows = [] for i in range(len(Recordings)): random_numbers = [] while len(random_numbers) < 5: random_number = random.randrange(2 + i * 60, 63 + i * 60, 1) if random_number not in random_numbers: random_numbers.append(random_number) random_rows.extend(random_numbers) reader = csv.reader( open( '/home/jasonhideki/songscape/www/media/snippets/snippets.csv')) reader.next() writer = open("output.csv", 'wb') writer = csv.writer(writer, delimiter=',') for i, row in enumerate(reader): if i in random_rows: #print("This is the line.") #print(row) writer.writerow(row) #extract snippets based on output.csv reader = csv.reader(open('/home/jasonhideki/songscape/output.csv')) for i, row in enumerate(reader): if i: try: path = row[0] offset = float(row[1]) duration = float(row[2]) md5 = hashlib.md5(open(path).read()).hexdigest() print i, md5, path #changed get criteria to path because weather test 000000 files have the same md5 as their non-000000 counterparts recording = Recording.objects.get(path=path) #recording = Recording.objects.get(md5 = md5) snippet = Snippet.objects.get(recording=recording, offset=offset, duration=duration) wavy.slice_wave( path, os.path.join(BASE_DIR, snippet.get_soundfile_name()), offset, duration, 8000) except Recording.DoesNotExist: print "Can't find the recording on row %s at path %s" % ( i, path) except: print 'Something went wrong on row %s with recording %s' % ( i, path) raise
def save_soundfile(self, replace=False, path="", max_framerate=None): filename = self.get_soundfile_name() if not path: name = os.path.join(settings.SNIPPET_DIR, filename) path = os.path.join(settings.MEDIA_ROOT, name) else: path = os.path.join(path, filename) try: if not os.path.exists(path): replace = True except (ValueError, SuspiciousOperation, AttributeError): replace = True if replace: wav_file = open(path, "w") wavy.slice_wave(self.recording.path, wav_file, self.offset, self.duration, max_framerate=max_framerate) wav_file.close() return filename
def handle(self, *args, **options): reader = csv.reader(open(args[0])) for i, row in enumerate(reader): if i: try: path = row[0] offset = float(row[1]) duration = float(row[2]) md5 = hashlib.md5(open(path).read()).hexdigest() print i, md5, path recording = Recording.objects.get(md5 = md5) snippet = Snippet.objects.get(recording=recording, offset=offset, duration=duration) wavy.slice_wave(path, os.path.join(BASE_DIR, snippet.get_soundfile_name()), offset, duration, 8000) except Recording.DoesNotExist: print "Can't find the recording on row %s at path %s" % (i, path) except: print 'Something went wrong on row %s with recording %s' % (i, path) raise
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') wavy.slice_wave( source, wav_file, 0, 0.5) #input file, output file, start in seconds, duraction in seconds wav_file.close() #spectrogram of sliced file plt.figure(1) nfft = 1024 fs = 256 Pxx, freqs, bins, im = plt.specgram(audio[0], nfft, fs) plt.savefig('stitch_specgram.jpg', dpi=100)
os.makedirs(source+"_results/audio/bird") if not os.path.exists(source+"_results/audio/not_bird"): os.makedirs(source+"_results/audio/not_bird") if not os.path.exists(source+"_results/imgs/bird"): os.makedirs(source+"_results/imgs/bird") if not os.path.exists(source+"_results/imgs/not_bird"): os.makedirs(source+"_results/imgs/not_bird") duration = 1 #Half a second offset = 0; #attempting to slice while(offset + duration < float(len(audio[0])/(no_channels*frequency))): suffix = str(offset) + "-" + str(offset + duration) wav_file = open(source + "_results/audio/audio_"+ suffix + ".wav", 'w') wavy.slice_wave(source, wav_file, offset, duration) #input file, output file, start in seconds, duration in seconds wav_file.close() #print("Segment: " + suffix) fragment = wavy.get_audio(source + "_results/audio/audio_"+ suffix + ".wav") #spectrogram of sliced file plt.figure(1) nfft=1024 fs=256#Sampling frequency plt.clf() plt.cla() Pxx, freqs, bins, im = plt.specgram(fragment[0], nfft, fs, cmap='binary', vmin = 0, vmax = 200) plt.axis('off') plt.savefig(source + '_results/imgs/' + suffix + '_specgram.jpg', dpi=100, bbox_inches='tight') rows = [] cols = []
def setUp(self): self.one_second = StringIO() slice_wave('test/three-second.wav', self.one_second, offset=1, duration=1) self.low_framerate = StringIO() slice_wave('test/three-second.wav', self.low_framerate, max_framerate=4000)