def supports_file(self, fname: str) -> bool: # Readable by audiotools? try: audiotools.open(fname) return True except UnsupportedFile: return False
def check_file(self): try: self.verify() except (audiotools.InvalidFile, audiotools.UnsupportedFile, ValueError): # We failed to open it in audiotools, so the streamer # won't be able to play it; try re-encoding for good measure. try: filename = self.encode() except VerifyError: error("failed to encode the song") except: error("unknown error on {0}".format(self.filename)) fileobj = audiotools.open(filename) length = long(fileobj.seconds_length()) # If there was no error we now have two files, delete the old one os.remove(args.file) else: filename = self.new_path(self.uuid()) self.rename(self.filename, filename) fileobj = audiotools.open(filename) length = long(fileobj.seconds_length()) print json.dumps({"length": length, "success": True, "filename": filename}) sys.exit(0)
def transcode(self, in_filepath, out_filepath): """Transcode audio file.""" logger.info("Transcoding from {} to {}", in_filepath, out_filepath) try: if not self._mode.startswith('replaygain'): audiotools.open(in_filepath).convert( out_filepath, self._format, compression=self._compression) else: in_file = audiotools.open(in_filepath) rp_info = self.get_replaygain(in_filepath) if rp_info: pcmreader = audiotools.replaygain.ReplayGainReader( in_file.to_pcm(), rp_info.gain + self._replaygain_preamp_gain, rp_info.peak) self._format.from_pcm(out_filepath, pcmreader, compression=self._compression) else: logger.warning("No ReplayGain info found {}", in_filepath) audiotools.open(in_filepath).convert( out_filepath, self._format, compression=self._compression) except (audiotools.EncodingError, audiotools.UnsupportedFile) as err: raise IOError("Failed to transcode file {}: {}".format( in_filepath, err)) from err
def convert_to_flac(self, path, r_hash): w_path = "{0}/out_{1}.wav".format(path, r_hash) n_path = "{0}/out_{1}.flac".format(path, r_hash) if self.debug: print "**converting {0}/out_{1}.wav to {0}/out_{1}.flac**".format(path, r_hash) audiotools.open(w_path).convert(n_path, audiotools.FlacAudio) return n_path
def any_to_wav(src, dst): log.info('any to wav: %s > %s' % (src, dst)) if not os.path.isfile(src): raise IOError('unable to access %s' % src) src_path, src_ext = os.path.splitext(src) if src_ext.lower() == '.wav': # shutil.copyfile(src, dst) return src try: audiotools.open(src).convert(dst, audiotools.WaveAudio) except Exception as e: log.info('file %s not supported by audiotools' % src) name, ext = os.path.splitext(src) if ext.lower() in ['.mp3', ]: mp3_to_wav(src, dst) if ext.lower() in ['.m4a', '.mp4']: m4a_to_wav(src, dst) print name print ext log.debug('to wav: %s > %s' % (src, dst)) if os.path.exists(dst): return dst
def any_to_wav(src, dst): log.info('any to wav: %s > %s' % (src, dst)) if not os.path.isfile(src): raise IOError('unable to access %s' % src) src_path, src_ext = os.path.splitext(src) if src_ext.lower() == '.wav': # shutil.copyfile(src, dst) return src try: audiotools.open(src).convert(dst, audiotools.WaveAudio) except Exception as e: log.info('file %s not supported by audiotools' % src) name, ext = os.path.splitext(src) if ext.lower() in [ '.mp3', ]: mp3_to_wav(src, dst) if ext.lower() in ['.m4a', '.mp4']: m4a_to_wav(src, dst) print name print ext log.debug('to wav: %s > %s' % (src, dst)) if os.path.exists(dst): return dst
def convert_wav_to_flac(audio): """ Takes the path of a wav audio file, then converts it to flac 16000hz and names it temp.flac Note, you must install the audiotools package. http://sourceforge.net/projects/audiotools/files/ """ audiotools.open(audio).convert("temp.flac",audiotools.FlacAudio)
def check_file(self): try: self.verify() except (audiotools.InvalidFile, audiotools.UnsupportedFile, ValueError): # We failed to open it in audiotools, so the streamer # won't be able to play it; try re-encoding for good measure. try: filename = self.encode() except VerifyError: error("failed to encode the song") except: error("unknown error on {0}".format(self.filename)) fileobj = audiotools.open(filename) length = long(fileobj.seconds_length()) # If there was no error we now have two files, delete the old one os.remove(args.file) else: filename = self.new_path(self.uuid()) self.rename(self.filename, filename) fileobj = audiotools.open(filename) length = long(fileobj.seconds_length()) print json.dumps({ "length": length, "success": True, "filename": filename }) sys.exit(0)
def wavtoflac(filepath_wav, mode): mode = 8 #default filepath_flac = filepath_wav.replace(".wav", ".flac") audiotools.open(filepath_wav).convert( filepath_flac, audiotools.FlacAudio, compression=audiotools.FlacAudio.COMPRESSION_MODES[mode]) return filepath_flac
def convert_wav_to_flac(audio): """ Takes the path of a wav audio file, then converts it to flac 16000hz and names it voice.flac returns the name of the file generated. """ audiotools.open(audio).convert("voice.flac",audiotools.FlacAudio) os.remove(audio) return 'voice.flac'
def main(): for file1 in fileIn1: if not ('.flac' in file1): continue adopath_flac = os.path.join(path1, file1) adopath_wav = adopath_flac.replace('flac', 'wav') audiotools.open(adopath_flac).convert( adopath_wav, audiotools.WaveAudio) # sampleRate 16000Hz with wave.open(adopath_wav, 'rb') as f1: params1 = f1.getparams() nchannels1, sampwidth1, framerate1, nframes1, comptype1, compname1 = params1[: 6] f1_str_data = f1.readframes(nframes1) f1_wave_data = np.fromstring(f1_str_data, dtype=np.int16) musicID = random.randint(0, musicNum - 1) # [a, b] music_wav = os.path.join(path2, fileIn2[musicID]) # sampleRate 44100Hz with wave.open(music_wav, 'rb') as f2: params2 = f2.getparams() nchannels2, sampwidth2, framerate2, nframes2, comptype2, compname2 = params2[: 6] f2_str_data = f2.readframes(nframes2) f2_wave_data = np.fromstring(f2_str_data, dtype=np.int16) f2_wave_data = f2_wave_data[::3] # decrease sampleRate nframes2 = len(f2_wave_data) if nframes1 < nframes2: length = abs(nframes2 - nframes1) startp = random.randint(0, length) rf1_wave_data = f1_wave_data rf2_wave_data = f2_wave_data[startp:startp + nframes1] elif nframes1 > nframes2: length = abs(nframes1 - nframes2) temp_array = np.zeros(length, dtype=np.int16) rf2_wave_data = np.concatenate((f2_wave_data, temp_array)) rf1_wave_data = f1_wave_data else: rf1_wave_data = f1_wave_data rf2_wave_data = f2_wave_data new_wave_data = rf1_wave_data + rf2_wave_data new_wave = new_wave_data.tostring() out_wav = adopath_wav with wave.open(out_wav, 'wb') as wf: wf.setnchannels(1) wf.setsampwidth(2) wf.setframerate(16000) wf.writeframes(new_wave) out_flac = adopath_flac audiotools.open(out_wav).convert(out_flac, audiotools.FlacAudio)
def Flac2Wav(flac_file, wav_path): wav_path = wav_path + "/" if not os.path.exists(wav_path): os.makedirs(wav_path) file_name = os.path.basename(flac_file) wav_name = file_name.replace(".WAV", ".wav") wav_file = wav_path + wav_name audiotools.open(flac_file).convert(wav_file, audiotools.WaveAudio) return wav_file
def convert_wav_flac(self, filename = "temp.wav"): name, ext = path.splitext(filename) flac = "%s.flac" % name audiotools.open(filename).convert(flac, audiotools.FlacAudio) f = open(flac) data = f.read() f.close() return data
def _rec_googleapi(self, input): print "in googleapi func @@@@@@@@" f = open('python_api.yml') yml_dict_speech = yaml.safe_load(f) f.close() APIKEY = yml_dict_speech['google_speech']['api_key'] audiotools.open(input).convert("output.flac", audiotools.FlacAudio) headers = {"Content-Type": "audio/x-flac;rate=44100"} speechfile = open("output.flac", "r") target = "/speech-api/v2/recognize?output=json&lang=en-us&key=" + str( APIKEY) target = target.strip( ) # just in case there are leading or trailing whitespace or newlines request = self.connection.request("POST", target, speechfile, headers) hyp = "" json_result = "" try: response = self.connection.getresponse() if response.status == 200: lines = str(response.read()).splitlines() if (len(lines) > 1): json_result = lines[1] decoded = json.loads(json_result) # pretty printing of json-formatted string print json.dumps(decoded, sort_keys=True, indent=4) # hyp = decoded['result'][0]['alternative'] # [0]['transcript'] hyp = [ str(x["transcript"]) for x in decoded['result'][0]['alternative'] ] print "speech rec result: ", hyp else: print "got this response status from google: ", response.status hyp = [ "error, got this response status from google: " + response.status ] return hyp except httplib.BadStatusLine: print "got bad status line error, trying to make google request again" self.recognize(input) # call again finally: speechfile.close() os.remove("output.flac") self.connection.close()
def proces_to_google(path): # converteren naar FLAC input = audiotools.open(path) input.convert( (u"%s.flac" % path), audiotools.FlacAudio, compression=audiotools.FlacAudio.COMPRESSION_MODES[8], progress=progress, ) path = u"%s.flac" % path # versturen naar google f = open(path, "rb") flac_cont = f.read() f.close() lang_code = "en-US" googl_speech_url = ( "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&pfilter=2&lang=%s&maxresults=6" % (lang_code) ) hrs = { "User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7", "Content-type": "audio/x-flac; rate=16000", } req = urllib2.Request(googl_speech_url, data=flac_cont, headers=hrs) p = urllib2.urlopen(req) res = eval(p.read())["hypotheses"] map(os.remove, (path)) return res
def process(self): iext = None try: iext = os.path.splitext(self.master.path)[1].lower() iext = iext[1:] audiofile = audiotools.open(self.master.path) base_format = iext base_bitrate = audiofile.bits_per_sample() base_samplerate = audiofile.sample_rate() base_filesize = os.path.getsize(self.master.path) base_duration = audiofile.seconds_length() try: base_duration = float(audiofile.total_frames()) / float(audiofile.sample_rate()) * 1000 print 'frames: %s' % audiofile.total_frames() print 'rate: %s' % audiofile.sample_rate() print base_duration print except: pass self.processed = 1 except Exception, e: print e base_bitrate = None base_samplerate = None base_filesize = None base_duration = None self.processed = 2
def __init__(self, filename): from .id3 import skip_id3v2_comment self.filename = filename try: f = open(filename, "rb") self.__stream_offset__ = skip_id3v2_comment(f) from .bitstream import BitstreamReader from .text import (ERR_TTA_INVALID_SIGNATURE, ERR_TTA_INVALID_FORMAT) reader = BitstreamReader(f, True) (signature, format_, self.__channels__, self.__bits_per_sample__, self.__sample_rate__, self.__total_pcm_frames__) = reader.parse( "4b 16u 16u 16u 32u 32u 32p") if (signature != "TTA1"): raise InvalidTTA(ERR_TTA_INVALID_SIGNATURE) elif (format_ != 1): raise InvalidTTA(ERR_TTA_INVALID_FORMAT) self.__total_tta_frames__ = div_ceil( self.__total_pcm_frames__ * 245, self.__sample_rate__ * 256) self.__frame_lengths__ = list(reader.parse( "%d* 32u" % (self.__total_tta_frames__) + "32p")) except IOError, msg: raise InvalidTTA(str(msg))
def _open_file(self, filename): """Open a file for reading and wrap it in several helpers.""" # Audiotools seems to hate unicode, so we.. don't give it that if isinstance(filename, unicode): filename = filename.encode('utf8') try: reader = audiotools.open(filename) except (audiotools.UnsupportedFile): raise AudioError("Unsupported file") self.file = reader total_frames = reader.total_frames() # Wrap in a PCMReader because we want PCM reader = reader.to_pcm() # Wrap in a converter reader = audiotools.PCMConverter( reader, sample_rate=44100, channels=2, channel_mask=audiotools.ChannelMask(0x1 | 0x2), bits_per_sample=24, ) # And for file progress! reader = audiotools.PCMReaderProgress(reader, total_frames, self.progress) return reader
def fix_id3_preserve_originals(self, tempFilePath): f = file(self.filename, 'rb') #figure out where the start and end points of the FLAC file are audiotools.ID3v2Comment.skip(f) flac_start = f.tell() f.seek(-128, 2) if (f.read(3) == 'TAG'): f.seek(-3, 1) flac_end = f.tell() else: f.seek(0, 2) flac_end = f.tell() #copy the FLAC data to a temporary location temp = tempfile.TemporaryFile() f.seek(flac_start, 0) reader = audiotools.__capped_stream_reader__(f, flac_end - flac_start) audiotools.transfer_data(reader.read, temp.write) #rewrite the original FLAC with our temporary data temp.seek(0, 0) f.close() f = file(tempFilePath, 'wb') audiotools.transfer_data(temp.read, f.write) temp.close() f.close() returnValue = audiotools.open(tempFilePath) return returnValue
def testShnConversion(self): audioFile = audiotools.open(self.showPath + '/show2/CD1/1.shn') pcmReader = audioFile.to_pcm() self.antiCrashBin.append(pcmReader) alacFile = audiotools.ALACAudio.from_pcm(self.testPath + '/output/2.m4a', pcmReader) self.assertTrue(os.path.exists(self.testPath + '/output/2.m4a')) pass
def fixBadFlacFiles(self, metadata): """ Fix FLAC files that are malformed because they contain ID3 tags. Replace the audioFile paths the metadata dict with the paths to temporary fixed versions. """ parent = self.parent() for index, audioFile in enumerate(metadata['audioFiles']): if self.stopped: return self.updateProgress( 'Loading %s\n\nFixing malformed FLACs (%d\%d)' % (self.basename, index+1, len(metadata['audioFiles'])), ) audioObj = audiotools.open(audioFile) if isinstance(audioObj, tracklint.BrokenFlacAudio): tempFilePath = unicode( metadata['tempDir'].absolutePath() ) + u'/%d.flac' % index if platform.system() == 'Darwin': self.process = Process( target=self.fixFlacProcess, args=(tempFilePath, audioObj) ) self.process.start() while self.process.is_alive(): pass else: self.fixFlacProcess(tempFilePath, audioObj) metadata['audioFiles'][index] = (tempFilePath)
def __init__(self, file_path): """ :param file_path: path to audio file """ # check if the file format is supported if not os.path.isfile(file_path): raise ValueError("Could not find file {0}".format(file_path)) with open(file_path, "rb") as f: supported = audiotools.file_type(f) in audiotools.AVAILABLE_TYPES if not supported: raise UnsupportedFileType("File format not supported.") # open the audiofile and extract the pcm stream self.file = audiotools.open(file_path) if not self.file.supports_to_pcm(): raise UnsupportedFileType("Can not extract the pcm stream.") # is the file seekable self.seekable = self.file.seekable() # the total number of frames in this file self._num_frames = self.file.total_frames() # create a pcm reader pcm = self.file.to_pcm() if self.seekable: self._pcm = pcm else: # load the whole file into memory if seeking is not available self._data = self._load(pcm) # Todo: just for debugging self._num_frames = len(self._data) # save the last frame number self._next_frame_number = 0
def delete_metadata(self): """deletes the track's MetaData this removes or unsets tags as necessary in order to remove all data raises IOError if unable to write the file""" import os from audiotools import (TemporaryFile, LimitedFileReader, transfer_data) #this works a lot like update_metadata #but without any new metadata to set if (not os.access(self.filename, os.W_OK)): raise IOError(self.filename) new_mp3 = TemporaryFile(self.filename) #get the original MP3 data old_mp3 = open(self.filename, "rb") MP3Audio.__find_last_mp3_frame__(old_mp3) data_end = old_mp3.tell() old_mp3.seek(0, 0) MP3Audio.__find_mp3_start__(old_mp3) data_start = old_mp3.tell() old_mp3 = LimitedFileReader(old_mp3, data_end - data_start) #write data to file transfer_data(old_mp3.read, new_mp3.write) #commit change to disk old_mp3.close() new_mp3.close()
def process(self): iext = None try: iext = os.path.splitext(self.master.path)[1].lower() iext = iext[1:] audiofile = audiotools.open(self.master.path) base_format = iext base_bitrate = audiofile.bits_per_sample() base_samplerate = audiofile.sample_rate() base_filesize = os.path.getsize(self.master.path) base_duration = audiofile.seconds_length() try: base_duration = float(audiofile.total_frames()) / float(audiofile.sample_rate()) * 1000 print 'frames: %s' % audiofile.total_frames() print 'rate: %s' % audiofile.sample_rate() print base_duration print except: pass self.processed = 1 except Exception as e: print e base_bitrate = None base_samplerate = None base_filesize = None base_duration = None self.processed = 2 """ self.base_format = iext self.base_bitrate = base_bitrate self.base_samplerate = base_samplerate self.base_filesize = base_filesize """ self.duration = base_duration meta = None try: meta = EasyID3(self.master.path) log.debug('using EasyID3') except Exception as e: meta = MutagenFile(self.master.path) log.debug('using MutagenFile') if 'title' in meta: self.name = meta['title'][0] if 'artist' in meta: self.artist, created = Artist.objects.get_or_create(name=meta['artist'][0]) self.save() try: self.create_waveform_image() except: pass return
def process(self): iext = None try: iext = os.path.splitext(self.master.path)[1].lower() iext = iext[1:] audiofile = audiotools.open(self.master.path) base_format = iext base_bitrate = audiofile.bits_per_sample() base_samplerate = audiofile.sample_rate() base_filesize = os.path.getsize(self.master.path) base_duration = audiofile.seconds_length() try: base_duration = float(audiofile.total_frames()) / float( audiofile.sample_rate()) * 1000 print 'frames: %s' % audiofile.total_frames() print 'rate: %s' % audiofile.sample_rate() print base_duration print except: pass self.processed = 1 except Exception, e: print e base_bitrate = None base_samplerate = None base_filesize = None base_duration = None self.processed = 2
def power_chroma(file_name, hop_length=512, power=2): song = audiotools.open(file_name) sr = song.sample_rate() y, sr = librosa.load(file_name, sr=sr) # Compute the STFT matrix stft = librosa.core.stft(y, n_fft=4096, hop_length=int(hop_length)) # Decompose into harmonic and percussives stft_harm, stft_perc = librosa.decompose.hpss(stft) stft_harm = np.abs(stft_harm)**power # stft_perc = np.log(np.abs(stft_perc)) # Invert the STFTs. Adjust length to match the input. # y_harmonic = librosa.util.fix_length(librosa.core.istft(stft_harm, dtype=y.dtype), len(y)) y_percussive = librosa.util.fix_length( librosa.core.istft(stft_perc, dtype=y.dtype, hop_length=int(hop_length)), len(y)) chromagram = librosa.feature.chroma_stft(S=stft_harm, sr=sr, hop_length=int(hop_length)) tempo, beat_frames = librosa.beat.beat_track(y=y_percussive, sr=sr, hop_length=hop_length) beat_t = librosa.frames_to_time(beat_frames, sr=sr, hop_length=hop_length) # Aggregate chroma features between beat events # The median feature is used instead beat_chroma = librosa.util.sync(chromagram, beat_frames, aggregate=np.median) return chromagram, beat_chroma, beat_frames, beat_t, sr
def chroma(file_name, hop_length=512, type_='cqt', tol=0.0): # An 11 kHz sample rate is used because it was suggested in the paper. eprint('Processing file: {}'.format(file_name)) song = audiotools.open(file_name) sr = song.sample_rate() y, sr = librosa.load(file_name, sr=sr) # Separate harmonics and percussives into two waveforms y_harmonic, y_percussive = librosa.effects.hpss(y) # Beat track on the percussive signal tempo, beat_frames = librosa.beat.beat_track(y=y_percussive, sr=sr) beat_t = librosa.frames_to_time(beat_frames, sr=sr) if type_ == 'cqt': # Compute chroma features from the harmonic signal chromagram = librosa.feature.chroma_cqt(y=y_harmonic, sr=sr, hop_length=hop_length, threshold=tol) elif type_ == 'stft': chromagram = librosa.feature.chroma_stft(y=y_harmonic, sr=sr, hop_length=hop_length) else: raise Exception("Must specify chromagram type!") # Aggregate chroma features between beat events # The median feature is used instead beat_chroma = librosa.util.sync(chromagram, beat_frames, aggregate=np.median) # commented out, # chromagram, beat_chroma, target_file, target_image_file = write_chroma_info(file_name, chromagram, beat_chroma) # save_chroma_pics(chromagram, beat_chroma, beat_frames, beat_t, sr, "Please Please Me", "Please Please Me") return chromagram, beat_chroma, beat_frames, beat_t, sr
def get_text(self): if not self.text is None: return self.text print "Converting to FLAC" (_, recording_flac_filename) = tempfile.mkstemp('.flac') recording_wav = audiotools.open(self.audio.filename()) recording_wav.convert(recording_flac_filename, audiotools.FlacAudio,) #compression=audiotools.FlacAudio.COMPRESSION_MODES[8], #progress=False) # turn the audio into useful text print "Sending to Google" google_speech_url = "http://www.google.com/speech-api/v1/recognize?lang=en" headers = {'Content-Type': 'audio/x-flac; rate= %d;' % self.recordingRate} recording_flac_data = open(recording_flac_filename, 'rb').read() r = requests.post(google_speech_url, data=recording_flac_data, headers=headers) # housekeeping os.remove(recording_flac_filename) self.audio.housekeeping() # grab the response response = r.text if not 'hypotheses' in response: raise NotUnderstoodException() # we are only interested in the most likely utterance phrase = json.loads(response)['hypotheses'][0]['utterance'] print "Heard: " + phrase return str(phrase)
def open_audiotools(f_name, fmt=None): if not isinstance(f_name, str): return None try: f = audiotools.open(f_name) except audiotools.UnsupportedFile: return None sample_width = f.bits_per_sample() pcm = f.to_pcm() frag = Fragment(rate=f.sample_rate(), channels=f.channels(), length=f.total_frames()) frame_size = frag.channels * sample_width / 8 chunk_size = 65536 / frame_size rem = f.total_frames() cur = 0 while rem > 0: frames = pcm.read(chunk_size) if frames.frames == 0: # Some lossy formats like MP3 have slightly varying data length break raw_bytes = bytearray(frames.to_bytes(False, True)) frag.import_bytes(raw_bytes, frag.rate, frag.channels, 'int{:d}'.format(sample_width), cur) rem -= frames.frames cur += frames.frames return frag
def playOrStopSelected(self, rowClicked): """ Play the selected track's file using the default application for that file type. """ rowStopped = self.stopAudio() if rowStopped is rowClicked: return item = QTableWidgetItem(str(rowClicked + 1)) item.setIcon(self.stopButtonIcon) self.tracklistTableWidget.setVerticalHeaderItem(rowClicked, item) encoding = sys.getfilesystemencoding() audiofileObj = audiotools.open( self.metadata['audioFiles'][rowClicked].encode(encoding)) self.pyaudioObj = pyaudio.PyAudio() self.stream = self.pyaudioObj.open(format=pyaudio.paInt16, channels=audiofileObj.channels(), rate=audiofileObj.sample_rate(), output=True) self.playing = rowClicked self.playAudioThread = PlayAudioThread(self, audiofileObj, self.stream) self.connect(self.playAudioThread, SIGNAL('done()'), self.stopAudio) self.playAudioThread.start()
def play_ogg(self, path): file = audiotools.open(path) stream = file.to_pcm() self.context.log("play ogg using device {0}".format(self.output_device_index)) self.output_stream = self.pa.open(format = FORMAT, channels = 2, rate = RATE, input = False, output = True, output_device_index = self.output_device_index, frames_per_buffer = INPUT_FRAMES_PER_BLOCK) # read data (based on the chunk size) framelist = stream.read(CHUNK) data = framelist.to_bytes(False, True) # play stream (looping from beginning of file to the end) while data != '': # writing to the stream is what *actually* plays the sound. self.output_stream.write(data) framelist = stream.read(CHUNK) data = framelist.to_bytes(False, True) # cleanup stuff. self.output_stream.close() self.pa.terminate()
def verify(self, filename=None): if not filename: filename = self.filename audio = audiotools.open(filename) # Do verification with audiotools; this might be too strict audio.verify()
def toMp3(inputFilePath, outputFilePath): mp3 = audiotools.MP3Audio.from_pcm(outputFilePath, audiotools.open(inputFilePath).to_pcm()) os.chmod(outputFilePath, NEW_FILE_PERMISSIONS)
def any_to_wav(src, dst=None): log.info('any to wav: %s > %s' % (src, dst)) if not os.path.isfile(src): log.error('unable to access %s' % src) raise IOError('unable to access %s' % src) src_path, src_ext = os.path.splitext(src) if src_ext.lower() == '.wav': shutil.copyfile(src, dst) return dst try: audiotools.open(src).convert(dst, audiotools.WaveAudio) except Exception as e: log.info('file %s not supported by audiotools' % src) name, ext = os.path.splitext(src) if ext.lower() in [ '.mp3', ]: mp3_to_wav(src, dst) if ext.lower() in [ '.m4a', '.mp4', ]: m4a_to_wav(src, dst) # fallback version via ffmpeg if ext.lower() in [ '.flac', ]: ffmpeg_to_wav(src, dst) log.debug('to wav: %s > %s' % (src, dst)) if not os.path.exists(dst): log.warning('unable to convert {} to wav.'.format(src)) return return dst
def metadata_audiotools(self, path, media): from audiotools import MetaData import audiotools meta = MetaData() # release-level metadata if media.release and media.release.main_image: if meta.supports_images() and os.path.exists(media.release.main_image.path): opt = dict(size=(200, 200), crop=True, bw=False, quality=80) image = get_thumbnailer(media.release.main_image).get_thumbnail(opt) meta.add_image(get_raw_image(image.path, 0)) audiotools.open(path).update_metadata(meta) return
def create_waveform_image(self): if self.master: tmp_directory = tempfile.mkdtemp() src_path = self.master.path; tmp_path = os.path.join(tmp_directory, 'tmp.wav') dst_path = os.path.join(self.get_folder_path('cache'), 'waveform.png') audiotools.open(src_path).convert(tmp_path, audiotools.WaveAudio) args = (tmp_path, dst_path, None, 1800, 301, 2048) create_wave_images(*args) try: shutil.rmtree(tmp_directory) except Exception, e: print e
def insert_track(source_filename): mimetype = _magic.from_file(source_filename) hash_ = _ca_copy_contents(open(source_filename, 'rb')) source_file = audiotools.open(source_filename) metadata = source_file.get_metadata() return TrackOriginal(Blob(hash_, mimetype), metadata, metadata.artist_name, metadata.track_name)
def clean_audio_file(self): audio = self.cleaned_data['audio_file'] if audio.size > 10*1024*1024: raise ValidationError(self.error_messages['audio_exceed']) allowed_type = ['audio/mpeg', 'audio/ogg', 'audio/mp3'] if not (audio.content_type in allowed_type): raise ValidationError(self.error_messages['audio_invalid']) randomfilename = str(time.time())+''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)) path = default_storage.save('tmp/'+randomfilename, ContentFile(audio.read())) try: audiotools.open(os.path.join(settings.MEDIA_ROOT, path)) default_storage.delete('tmp/'+randomfilename) except audiotools.UnsupportedFile: default_storage.delete('tmp/'+randomfilename) raise ValidationError(self.error_messages['audio_corrupted']) return audio
def create_waveform_image(self): if self.master: tmp_directory = tempfile.mkdtemp() src_path = self.master.path tmp_path = os.path.join(tmp_directory, 'tmp.wav') dst_path = os.path.join(self.get_folder_path('cache'), 'waveform.png') audiotools.open(src_path).convert(tmp_path, audiotools.WaveAudio) args = (tmp_path, dst_path, None, 1800, 301, 2048) create_wave_images(*args) try: shutil.rmtree(tmp_directory) except Exception, e: print e
def any_to_wav(src, dst=None): log.info('any to wav: %s > %s' % (src, dst)) if not os.path.isfile(src): log.error('unable to access %s' % src) raise IOError('unable to access %s' % src) src_path, src_ext = os.path.splitext(src) if src_ext.lower() == '.wav': shutil.copyfile(src, dst) return dst try: audiotools.open(src).convert(dst, audiotools.WaveAudio) except Exception as e: log.info('file %s not supported by audiotools' % src) name, ext = os.path.splitext(src) if ext.lower() in ['.mp3',]: mp3_to_wav(src, dst) if ext.lower() in ['.m4a', '.mp4',]: m4a_to_wav(src, dst) # fallback version via ffmpeg if ext.lower() in ['.flac',]: ffmpeg_to_wav(src, dst) log.debug('to wav: %s > %s' % (src, dst)) if not os.path.exists(dst): log.warning('unable to convert {} to wav.'.format(src)) return return dst
def _rec_googleapi(self, input): print "in googleapi func @@@@@@@@" f = open('python_api.yml') yml_dict_speech = yaml.safe_load(f) f.close() APIKEY = yml_dict_speech['google_speech']['api_key'] audiotools.open(input).convert("output.flac", audiotools.FlacAudio) headers = {"Content-Type": "audio/x-flac;rate=44100"} speechfile = open("output.flac","r") target = "/speech-api/v2/recognize?output=json&lang=en-us&key=" + str(APIKEY) target = target.strip() # just in case there are leading or trailing whitespace or newlines request = self.connection.request("POST",target,speechfile,headers) hyp = "" json_result = "" try: response = self.connection.getresponse() if response.status == 200: lines = str(response.read()).splitlines() if(len(lines) > 1): json_result = lines[1] decoded = json.loads(json_result) # pretty printing of json-formatted string print json.dumps(decoded, sort_keys=True, indent=4) # hyp = decoded['result'][0]['alternative'] # [0]['transcript'] hyp = [str(x["transcript"]) for x in decoded['result'][0]['alternative']] print "speech rec result: ", hyp else: print "got this response status from google: ",response.status hyp = ["error, got this response status from google: " + response.status] return hyp except httplib.BadStatusLine: print "got bad status line error, trying to make google request again"; self.recognize(input) # call again finally: speechfile.close() os.remove("output.flac") self.connection.close()
def verify(self, progress=None): """verifies the current file for correctness returns True if the file is okay raises an InvalidFile with an error message if there is some problem with the file""" from . import verify try: f = open(self.filename, 'rb') except IOError, err: raise InvalidMP3(str(err))
def __init__(self, filename): """filename is a plain string""" AudioFile.__init__(self, filename) from .bitstream import BitstreamReader import cStringIO try: mp3file = open(filename, "rb") except IOError, msg: raise InvalidMP3(str(msg))
def update_metadata(self, metadata): """takes this track's current MetaData object as returned by get_metadata() and sets this track's metadata with any fields updated in that object raises IOError if unable to write the file """ import os from audiotools import (TemporaryFile, LimitedFileReader, transfer_data) from audiotools.id3 import (ID3v2Comment, ID3CommentPair) from audiotools.id3v1 import ID3v1Comment from audiotools.bitstream import BitstreamWriter if (metadata is None): return elif (not (isinstance(metadata, ID3v2Comment) or isinstance(metadata, ID3CommentPair) or isinstance(metadata, ID3v1Comment))): from .text import ERR_FOREIGN_METADATA raise ValueError(ERR_FOREIGN_METADATA) elif (not os.access(self.filename, os.W_OK)): raise IOError(self.filename) new_mp3 = TemporaryFile(self.filename) #get the original MP3 data old_mp3 = open(self.filename, "rb") MP3Audio.__find_last_mp3_frame__(old_mp3) data_end = old_mp3.tell() old_mp3.seek(0, 0) MP3Audio.__find_mp3_start__(old_mp3) data_start = old_mp3.tell() old_mp3 = LimitedFileReader(old_mp3, data_end - data_start) #write id3v2 + data + id3v1 to file if (isinstance(metadata, ID3CommentPair)): metadata.id3v2.build(BitstreamWriter(new_mp3, 0)) transfer_data(old_mp3.read, new_mp3.write) metadata.id3v1.build(new_mp3) elif (isinstance(metadata, ID3v2Comment)): metadata.build(BitstreamWriter(new_mp3, 0)) transfer_data(old_mp3.read, new_mp3.write) elif (isinstance(metadata, ID3v1Comment)): transfer_data(old_mp3.read, new_mp3.write) metadata.build(new_mp3) #commit change to disk old_mp3.close() new_mp3.close()
def clean_audio_file(self): audio = self.cleaned_data['audio_file'] if audio.size > 10 * 1024 * 1024: raise ValidationError(self.error_messages['audio_exceed']) allowed_type = ['audio/mpeg', 'audio/ogg', 'audio/mp3'] if not (audio.content_type in allowed_type): raise ValidationError(self.error_messages['audio_invalid']) randomfilename = str(time.time()) + ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(5)) path = default_storage.save('tmp/' + randomfilename, ContentFile(audio.read())) try: audiotools.open(os.path.join(settings.MEDIA_ROOT, path)) default_storage.delete('tmp/' + randomfilename) except audiotools.UnsupportedFile: default_storage.delete('tmp/' + randomfilename) raise ValidationError(self.error_messages['audio_corrupted']) return audio
def play(self): # audiotools.player.available_outputs() audio_file = audiotools.open( '/home/dhruv/Music/Music/Aisha (2010) ~ 320 VBR/04 - Behke Behke [DoReGaMa].mp3' ) audio_output = audiotools.player.open_output('ALSA') replay_gain = audiotools.player.RG_NO_REPLAYGAIN player = audiotools.player.Player(audio_output, replay_gain) player.open(audio_file) player.play()
def process(self, q): """This is the worker thread function. It processes files in the queue one after another. These daemon threads go into an infinite loop, and only exit when the main thread ends. """ while True: global logger compression_quality = '0' #min compression file_flac = q.get() file_wav = file_flac.replace(".flac", ".wav") try: audiotools.open(file_flac).convert(file_wav,audiotools.WavAudio, compression_quality) logger.info(''.join(["Converted ", file_flac, " to: ", file_wav])) os.remove(file_flac) q.task_done() except InvalidWave: logger.error(''.join(["Failed to open file ", file_flac, " to: ", file_wav," failed."]), exc_info=True) except Exception as e: logger.error('ExFailed to open file', exc_info=True)
def play_song(self, song=None): if song: audio_file = audiotools.open(song['filepath']) self.player.open(audio_file) self.player.play() time.sleep(0.1) self.thread = threading.Thread(target=update_timer_seek, args=(self.ui.timer_label, self.ui.seek, self.player, song['duration'])) self.thread.start() print(f'playing {song.get("filepath")}' if song else '')
def PopulateMetaData(self): audioFile = audiotools.open(self.Filepath) metaData = audioFile.get_metadata() if not metaData: self.ManualMetaData() else: self.Title = metaData.track_name self.Artist = metaData.artist_name if PREPROCESS: self.Data = self.PrepareSongData() print("Preproccessed " + self.Title)
def toNormalizedWav(inputFilePath, outputFilePath): # open file (can raise errors) orig = audiotools.open(inputFilePath) ## Decode file to raw audio (PCM) origPCM = orig.to_pcm() # If this is a surround sound file, or if there is no channel mask # we shall error for now. This will probably not happen. channel_mask = origPCM.channel_mask if channel_mask < 2 or channel_mask > 3: raise Exception ('Unsupported audio channel configuration') # Make sure bit depth is not > 16bit. # If it is, error for now. I tried to convert the file, but I # get the following error: "data chunk ends prematurely" bits_per_sample = origPCM.bits_per_sample if bits_per_sample > 16: # Uncomment this line to try changing the bit depth: #bits_per_sample = 16 # Comment this line to try changing the bit depth: raise Exception( 'Unsupported bit depth, try converting file to 16bit.' ) # Make sure sample rate is not > 44100 sample_rate = origPCM.sample_rate # If original sample rate was greater than 44100 if sample_rate > 44100: # Convert to 44100 sample_rate = 44100 # Create new PCM stream at 44100, with 2 channels normalizedPCM = audiotools.PCMConverter(origPCM, 44100, 2, channel_mask, bits_per_sample) # Output normalized audio to wav (Can raise error) wav = audiotools.WaveAudio.from_pcm(outputFilePath, normalizedPCM) os.chmod(outputFilePath, NEW_FILE_PERMISSIONS)
def create_waveform_image(self): if self.master: tmp_directory = tempfile.mkdtemp() src_path = self.master.path tmp_path = os.path.join(tmp_directory, "tmp.wav") dst_path = os.path.join(self.get_folder_path("cache"), "waveform.png") print "create waveform" print "src_path: %s" % src_path print "tmp_path: %s" % tmp_path print "dst_path: %s" % dst_path audiotools.open(src_path).convert(tmp_path, audiotools.WaveAudio) args = (tmp_path, dst_path, None, 1800, 301, 2048) create_wave_images(*args) try: shutil.rmtree(tmp_directory) except Exception, e: print e
def insert_track(source_filename_or_fh): if isinstance(source_filename_or_fh, str): source_filename_or_fh = open(source_filename_or_fh, 'rb') buf = source_filename_or_fh.read(4096) source_filename_or_fh.seek(0) mimetype = _magic.from_buffer(buf) hash_ = _ca_copy_contents(source_filename_or_fh) source_file = audiotools.open(cont_addr.addr_to_path(hash_)) metadata = source_file.get_metadata() return TrackOriginal(Blob(hash_, mimetype), metadata, metadata.artist_name, metadata.track_name)