def decode(filename): filename = os.path.abspath(os.path.expanduser(filename)) if not os.path.exists(filename): print("File not found.", file=sys.stderr) sys.exit(1) try: with audioread.audio_open(filename) as f: print('Input file: %i channels at %i Hz; %.1f seconds.' % (f.channels, f.samplerate, f.duration), file=sys.stderr) print('Backend:', str(type(f).__module__).split('.')[1], file=sys.stderr) filename = filename.split('/') newFileName = os.path.join("../decoded-input/", filename[-1]) with contextlib.closing(wave.open(newFileName + '.wav', 'w')) as of: of.setnchannels(f.channels) of.setframerate(f.samplerate) of.setsampwidth(2) for buf in f: of.writeframes(buf) except audioread.DecodeError: print("File could not be decoded.", file=sys.stderr) sys.exit(1)
def _parse_file(filename: str): """ Add a new file to the database if it doesn't already exist and it is an audio file :param filename: The path of the file to add """ session = database.db.session file = session.query(database.Track).filter_by(location=filename).one_or_none() if file is not None: session.close() return try: with audioread.audio_open(filename) as track: length = track.duration valid = True except: length = 0.0 valid = False if valid and length > 0.0: try: file = eyed3.load(filename) artist = file.tag.artist title = file.tag.title except: artist = '' title = '' session.add(database.Track(location=filename, artist=artist, title=title, length=length)) session.commit() session.close()
def get_samplerate(path): '''Get the sampling rate for a given file. Parameters ---------- path : string, int, or file-like The path to the file to be loaded As in `load()`, this can also be an integer or open file-handle that can be processed by `soundfile`. Returns ------- sr : number > 0 The sampling rate of the given audio file Examples -------- Get the sampling rate for the included audio file >>> path = librosa.util.example_audio_file() >>> librosa.get_samplerate(path) 44100 ''' try: return sf.info(path).samplerate except RuntimeError: with audioread.audio_open(path) as fdesc: return fdesc.samplerate
def audioread_info(path): """ Return an audio info data structure that's a compatible subset of ``pysoundfile.info()`` that we need to create a ``Recording`` manifest. """ import audioread class _LibsndfileCompatibleAudioInfo(NamedTuple): channels: int frames: int samplerate: int duration: float # We just read the file and compute the number of samples # -- no other method seems fully reliable... with audioread.audio_open( path, backends=_available_audioread_backends()) as input_file: shape = _audioread_load(input_file)[0].shape if len(shape) == 1: num_samples = shape[0] else: num_samples = shape[1] return _LibsndfileCompatibleAudioInfo(channels=input_file.channels, frames=num_samples, samplerate=input_file.samplerate, duration=num_samples / input_file.samplerate)
def decode(self, fname: str, mp3_dir: str, save_dir: str): """ :param fname: name of the mp3 file that will be converted :param mp3_dir: directory of the mp3 file :param save_dir: directory of the wav file to be saved :return: """ file_path = ospathjoin(mp3_dir, fname) if not ospathexists(file_path): print("File not found.", file=sysstderr) sysexit(1) fname = self.__change_extention(fname) print('New-Filename:', fname) with audioread.audio_open(file_path) as f: print('Input file: %i channels at %i Hz; %.1f seconds.' % (f.channels, f.samplerate, f.duration), file=sysstderr) print('Backend:', str(type(f).__module__).split('.')[1], file=sysstderr) with contextlib_closing(waveopen(ospathjoin(save_dir, fname), 'w')) as of: of.setnchannels(f.channels) of.setframerate(f.samplerate) of.setsampwidth(2) for buf in f: of.writeframes(buf) return fname
def __init__(self, path): super(Audio, self).__init__(path) fh = audioread.audio_open(path) self._samplerate = fh.samplerate self._channels = fh.channels self._duration = fh.duration fh.close()
def decode(filename, out_filename): filename = os.path.abspath(os.path.expanduser(filename)) if not os.path.exists(filename): print("File not found.", file=sys.stderr) sys.exit(1) try: with audioread.audio_open(filename) as f: print('Input file: %i channels at %i Hz; %.1f seconds.' % (f.channels, f.samplerate, f.duration), file=sys.stderr) print('Backend:', str(type(f).__module__).split('.')[1], file=sys.stderr) with contextlib.closing(wave.open(out_filename, 'w')) as of: of.setnchannels(f.channels) of.setframerate(f.samplerate) of.setsampwidth(2) for buf in f: # Audio processing of buffer # TODO: default 4kB buffer is ~22ms of 44.1kHz audio. Collect `buf`s here to do processing on larger chunk, then write to output file `of`. your_processing.your_processing(buf) # Write buffer to wave file of.writeframes(buf) except audioread.DecodeError: print("File could not be decoded.", file=sys.stderr) sys.exit(1)
def convert_to_wav(sound_path: typing.Union[str, Path]) -> bytes: """Open sound file and convert to WAV format""" sound_path = str(sound_path) try: # Try as WAV first with wave.open(sound_path, "rb"): # Already a WAV file return open(sound_path, "rb").read() except Exception: # Try soundfile try: with open(sound_path, "rb") as sound_file, io.BytesIO() as wav_out: audio_data, sample_rate = soundfile.read(sound_file) soundfile.write(wav_out, audio_data, sample_rate, format="WAV") return wav_out.getvalue() except Exception: # Fall back to audioread with audioread.audio_open( sound_path ) as sound_file, io.BytesIO() as wav_io: wav_write: wave.Wave_write = wave.open(wav_io, "wb") with wav_write: wav_write.setnchannels(sound_file.channels) # type: ignore wav_write.setframerate(sound_file.samplerate) # type: ignore wav_write.setsampwidth(2) # fixed at 16-bits by audioread for sound_buffer in sound_file: wav_write.writeframes(sound_buffer) return wav_io.getvalue()
def load_and_resample_if_necessary(_config, audio_file): with audioread.audio_open(audio_file) as f: sr = f.samplerate x_np, sr1 = librosa.load(audio_file, sr=_config.sample_rate) if sr != _config.sample_rate: sf.write(audio_file, x_np, _config.sample_rate) return x_np
def detectAudioMetaInfo(audioFullPath): """ detect audio meta info: duration, channels, sampleRate """ isOk = False errMsg = "" audioMetaInfo = { "duration": 0, "channels": 0, "sampleRate": 0, } try: with audioread.audio_open(audioFullPath) as audioFp: audioMetaInfo["duration"] = audioFp.duration audioMetaInfo["channels"] = audioFp.channels audioMetaInfo["sampleRate"] = audioFp.samplerate isOk = True except OSError as osErr: errMsg = "detect audio info error: %s" % str(osErr) except EOFError as eofErr: errMsg = "detect audio info error: %s" % str(eofErr) except audioread.DecodeError as decodeErr: errMsg = "detect audio info error: %s" % str(decodeErr) if isOk: return isOk, audioMetaInfo else: return isOk, errMsg
async def fileplay(self, ctx): """Play a sound file sent in the chat.""" voice_client = ctx.voice_client if not voice_client: # If the bot is not in a voice channel, make it join a voice # channel first. await ctx.invoke(self.join) if ctx.voice_client: player = self.get_player(ctx) current_id = player.current_file_id # Make sure there is an attached file. if len(ctx.message.attachments) == 1: attachment = ctx.message.attachments[0] filename = attachment.filename # File name and extension separated. file_without_extension = filename[:filename.rfind(".")] file_extension = filename[filename.rfind(".") + 1:] # Save the file. await ctx.send(f"Saving `{filename}`...") # saved_filename = f"audio.{file_extension}" saved_filename = os.path.join(DOWNLOADS_FOLDER, str(ctx.guild.id), f"audio{current_id}.{file_extension}") file_data = await attachment.save(saved_filename) # Audio length. try: with audioread.audio_open(saved_filename) as f: audio_duration = int(f.duration) player.current_file_id += 1 await ctx.send("Saved the audio file.") except audioread.NoBackendError: await ctx.send("That is not au audio file.") if os.path.exists(saved_filename): os.remove(saved_filename) return except FileNotFoundError: await ctx.send("The saved audio file could not be found.") return else: await ctx.send( "You have to attach an audio file to this command.") return source = FileSource(discord.FFmpegPCMAudio( executable=FFMPEG, source=saved_filename), ctx.author, saved_filename, file_without_extension, file_extension, audio_duration) await player.queue.put(source)
def print_data(): with audioread.audio_open("output.wav") as f: print(f.channels, f.samplerate, f.duration) data = [] for buf in f: data.append(buf) print(data)
def slice_audio(files, channels, outformat, width, rate, slice_length, slide): outformat = outformat.replace('.','').lower() #Allow the user to see their x-bit selection with this dictionary. width_translator = {1:'8-bit', 2:'16-bit', 4:'32-bit'} #For every file in the input list do processing. for file in files: fileName, fileExtension = os.path.splitext(file) #Print to screen the processing parameters. with audioread.audio_open(file) as f: #Store the file in RAM. sound = AudioSegment.from_file(file, fileExtension.replace('.','').lower()) #Print the 'x-bit' conversion parameters. #Implement the user-selected or default (if nothing selected) parameters for processing. sound = sound.set_frame_rate(int(rate)) sound = sound.set_sample_width(int(width)) sound = sound.set_channels(int(channels)) length_sound_ms = len(sound) length_slice_ms = int(slice_length) slice_start = 0 counter = 0 #create audiosegment object #Begin slicing at the start of the file. while slice_start + length_slice_ms < length_sound_ms: sound_slice = sound[slice_start:slice_start+length_slice_ms] sound_slice.export(fileName+'_'+str(counter)+'.'+outformat, format=outformat) slice_start = slice_start + int(slide) - 1 counter+=1 #When the slice is abutting the end of the file, output that slice too.' if slice_start + length_slice_ms >= length_sound_ms: sound_slice = sound[slice_start:length_sound_ms] sound_slice.export(fileName+'_'+str(counter)+'.'+outformat, format=outformat)
def load_audio(filename, trace=0): """ load wav file using audioread. This is not available in python x,y. """ try: import audioread except ImportError: print 'python module "audioread" is not installed.' return load_wave(filename, trace) data = np.array([]) with audioread.audio_open(filename) as af: tracen = af.channels if trace >= tracen: print 'number of traces in file is', tracen quit() data = np.zeros(np.ceil(af.samplerate * af.duration), dtype="<i2") index = 0 for buffer in af: fulldata = np.fromstring(buffer, dtype='<i2').reshape(-1, af.channels) n = fulldata.shape[0] if index + n > len(data): n = len(data) - index if n > 0: data[index:index + n] = fulldata[:n, trace] index += n else: break return af.samplerate, data / 2.0 ** 15, 'a.u.'
def match_file(apikey, path, metadata=None): """Uses the audioread library to decode an audio file and match it. """ import audioread with audioread.audio_open(path) as f: return match(apikey, iter(f), f.samplerate, int(f.duration), f.channels, metadata)
def read_samples(args, audio_params, file_idx, file_sample_count): """Read samples from audio file and mix stereo to mono. - MP3 has 1152 samples per audio channel per block. - For MP3, read_blocks() returns 4608 bytes - = 2304 16-bit words - = two channels with 1152 samples each """ if audio_params.channel_count not in (1, 2): raise WakeupChirpsError( f"Input audio file must have one or two channels (mono or stereo). " f"This file has {audio_params.channel_count} channels" ) fmt_str = "h" * audio_params.channel_count offset_sample_count = file_idx * file_sample_count with audioread.audio_open(args.audio_in_path) as f: sample_idx = 0 for block in f.read_blocks(): for sample_list in struct.iter_unpack(fmt_str, block): if sample_idx >= offset_sample_count: if sample_idx >= offset_sample_count + file_sample_count: break mono_sample = sum(sample_list) / len(sample_list) yield mono_sample sample_idx += 1
def get_sr(path: Union[str, Path]) -> int: try: with sf.SoundFile(path) as f: return int(f.samplerate) except RuntimeError: with audioread.audio_open(path) as f: return int(f.samplerate)
def main(): parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) data_dir = os.path.join(parent_dir, 'data') audio_dir = os.path.join(data_dir, 'conversational/audio') # 'scripted/audio') audio_files = os.listdir(audio_dir) audio_files = [f for f in audio_files if f.endswith('.sph')] audio_paths = [os.path.join(audio_dir, f) for f in audio_files] wav_dir = os.path.join(data_dir, 'wav') if not os.path.exists(wav_dir): os.makedirs(wav_dir) for i, p in enumerate(audio_paths): curr_audio_file = audio_files[i] print(curr_audio_file) curr_wav_file = curr_audio_file[:-4] + '.wav' curr_wav_path = os.path.join(wav_dir, curr_wav_file) with audioread.audio_open(p) as f: with contextlib.closing(wave.open(curr_wav_path, 'w')) as of: of.setnchannels(f.channels) of.setframerate(f.samplerate) of.setsampwidth(2) for buf in f: of.writeframes(buf)
def handle_file(path, out_path, channels): with au.audio_open(path) as f: chan = channels or f.channels rate = f.samplerate duration = f.duration * 1000 def reducer(tbm, buf): print(tbm[-1]) return tbm + [bm.next_hits(buf, rate, tbm[-1][1], tbm[-1][0])] #TODO: it might make sense to concat the buffers, depending on the time/buffer... note_states = reduce(reducer, bm.flat_map_file(f), [(None, [None for i in range(chan)])]) just_notes = [n[1] for n in note_states] sample_len = duration / len(just_notes) print(sample_len) print(1/sample_len) time = sample_len / 2 hits = [] down_hits = [None for i in range(chan)] for notes in just_notes[1:]: for i, n in enumerate(notes): #this heavily assumes that something isn't held down twice "in a row" #for different notes. if down_hits[i] is None and n is not None: down_hits[i] = int(time) elif down_hits[i] is not None and n is None: hits.append((i, down_hits[i], int(time))) down_hits[i] = None time += sample_len col_width = 512/chan beatmap_hits = map(lambda x: (int(x[0] * col_width + col_width / 2), 0, x[1], 128, 0, x[2]), hits) hit_objs = bm.CSVPart("Hit Objects", *beatmap_hits) default = bm.mk_default_metadata(path, 0, 3, "Auto beatmap for " + path, "", "", "") bm.mk_beatmap(out_path, *(default + [hit_objs]))
def read_wav_cd(wavName,begin,end) : wav_bary = bytearray() with ar.audio_open(wavName) as f: if f.duration*f.samplerate < end-begin: print("サンプル数が音声信号の長さを超えています。") exit() print("ファイル名 : ",wavName,"\nチャネル数: {0}[channel] \nサンプリング周波数 : {1}[Hz]\nフレーム数 : {2}" .format(f.channels, f.samplerate, f.duration*f.samplerate)) # "block_samples"で指定されたチャンクサイズずつ処理する(デフォルト1024) for buf in f: wav_bary.extend(buf) wav_ary = np.frombuffer(wav_bary, dtype=np.int16) # 常時16bitで読み込まれる wav_l = wav_ary[0::2] wav_r = wav_ary[1::2] #print(wav_l.shape) #print(wav_r.shape) # shortをfloat64に変換 wav_float_l = pcm2float(wav_l[begin:end]) #配列が大きいとメモリエラー wav_float_r = pcm2float(wav_r[begin:end]) #読み込んだ波形の一部を描画 #import pylab as pl #pl.plot(wav_float_l[begin:end]) #pl.show() return {"amp_l":wav_float_l, "amp_r":wav_float_r}
def decode(filename): filename = os.path.abspath(os.path.expanduser(filename)) if not os.path.exists(filename): print >>sys.stderr, "File not found." sys.exit(1) try: with audioread.audio_open(filename) as f: print >>sys.stderr, "Input file: %i channels at %i Hz; %.1f seconds." % ( f.channels, f.samplerate, f.duration, ) print >>sys.stderr, "Backend:", str(type(f).__module__).split(".")[1] with contextlib.closing(wave.open(filename + ".wav", "w")) as of: of.setnchannels(f.channels) of.setframerate(f.samplerate) of.setsampwidth(2) for buf in f: of.writeframes(buf) except audioread.DecodeError: print >>sys.stderr, "File could not be decoded." sys.exit(1)
def decode(filename): filename = os.path.abspath(os.path.expanduser(filename)) if not os.path.exists(filename): print("File not found.", file=sys.stderr) sys.exit(1) try: with audioread.audio_open(filename) as f: print('Input file: %i channels at %i Hz; %.1f seconds.' % \ (f.channels, f.samplerate, f.duration), file=sys.stderr) print('Backend:', str(type(f).__module__).split('.')[1], file=sys.stderr) with contextlib.closing(wave.open(filename + '.wav', 'w')) as of: of.setnchannels(f.channels) of.setframerate(f.samplerate) of.setsampwidth(2) for buf in f: of.writeframes(buf) except audioread.DecodeError: print("File could not be decoded.", file=sys.stderr) sys.exit(1)
def load(path, sr=22050, mono=True): """Load an audio file into a single, long time series Arguments: path -- (string) path to the input file sr -- (int > 0) target sample rate 'None' uses the native sampling rate mono -- (boolean) convert to mono Returns (y, sr): y -- (ndarray) audio time series sr -- (int) sampling rate of y """ with audioread.audio_open(os.path.realpath(path)) as input_file: sr_native = input_file.samplerate y = [np.frombuffer(frame, '<i2').astype(float) / float(1<<15) for frame in input_file] y = np.concatenate(y) if input_file.channels > 1: if mono: y = 0.5 * (y[::2] + y[1::2]) else: y = y.reshape( (-1, 2)).T if sr is not None: y = resample(y, sr_native, sr) else: sr = sr_native return (y, sr)
def load_audio(filename, sr, mono): # output is (1, n_samples, n_channels) if os.path.isfile(filename): if os.path.getsize(filename) > 0: # file info af_info = audioread.audio_open(filename) n_channels = af_info.channels if not mono else 1 duration_sec = af_info.duration duration_smp = int(duration_sec * sr) duration_smp = int(numpy.ceil(duration_smp / sr - 1 / sr) * sr) # load audio x, fs = librosa.core.load(filename, sr=sr, mono=mono) x = librosa.util.fix_length(x, duration_smp) x = x.reshape((n_channels, duration_smp, 1)).T else: print('\n\nSize of file {} is {}.\n'.format( os.path.basename(filename), os.path.getsize(filename))) return None, None else: raise IOError('File not found {}'.format(filename)) return x, fs
def run(self): rate_conversion_state = None # Open audio file with Audioread module. This may crash if proper decoders are not installed! with audioread.audio_open(self.filename) as dec: self.seconds_duration = dec.duration bps = 2 * dec.channels * dec.samplerate self.ready = True for buf in dec: # Wait if there is no need to fill the buffer while self.mumble.sound_output.get_buffer_size( ) > 2.0 and self._run: time.sleep(0.01) if not self._run: return # Update position self.bytes_position += len(buf) self.seconds_position = self.bytes_position / bps # Convert audio if necessary. We want precisely 16bit 48000Hz mono audio for mumble. if dec.channels != 1: buf = audioop.tomono(buf, 2, 0.5, 0.5) if dec.samplerate != 48000: buf, rate_conversion_state = audioop.ratecv( buf, 2, 1, dec.samplerate, 48000, rate_conversion_state) if self.volume: buf = audioop.mul(buf, 2, self.volume) # Insert to mumble outgoing buffer self.mumble.sound_output.add_sound(buf)
def save(self): self.pub_date_year = self.pub_date.date() self.slug = slugify(self.title) if len(self.slug) > 50: self.slug = self.slug[:49] if self.media: import mimetypes self.mimetype = mimetypes.guess_type(self.media.name) if 'audio' in self.mimetype: import audioread self.duration = int(audioread.audio_open(self.media.path).duration) elif ('video' in self.mimetype) and self.media: import subprocess process = subprocess.Popen(['/usr/bin/ffmpeg', '-i', self.media.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, stderr = process.communicate() matches = re.search(r"Duration:\s{1}(?P\d+?):(?P\d+?):(?P\d+\.\d+?),", stdout, re.DOTALL).groupdict() hours = Decimal(matches['hours']) minutes = Decimal(matches['minutes']) seconds = Decimal(matches['seconds']) total = 0 total += 60 * 60 * hours total += 60 * minutes total += seconds self.duration = total return super(Post, self).save()
def _open(self) -> None: """ Open the file at the start """ self._file = audioread.audio_open(self._path) self._file_iter = iter(self._file) self._blocks_sent = 0
def read_blocks(file_path, start=0.0, end=float('inf'), buffer_size=5760000): """ Read an audio file block after block. The blocks are yielded one by one. Args: file_path (str): Path to the file to read. start (float): Start in seconds to read from. end (float): End in seconds to read to. ``inf`` means to the end of the file. buffer_size (int): Number of samples to load into memory at once and return as a single block. The exact number of loaded samples depends on the block-size of the audioread library. So it can be of x higher, where the x is typically 1024 or 4096. Returns: Generator: A generator yielding the samples for every block. """ buffer = [] n_buffer = 0 n_samples = 0 with audioread.audio_open(file_path) as input_file: n_channels = input_file.channels sr_native = input_file.samplerate start_sample = int(np.round(sr_native * start)) * n_channels end_sample = end if end_sample != np.inf: end_sample = int(np.round(sr_native * end)) * n_channels for block in input_file: block = librosa.util.buf_to_float(block) n_prev = n_samples n_samples += len(block) if n_samples < start_sample: continue if n_prev > end_sample: break if n_samples > end_sample: block = block[:end_sample - n_prev] if n_prev <= start_sample <= n_samples: block = block[start_sample - n_prev:] n_buffer += len(block) buffer.append(block) if n_buffer >= buffer_size: yield process_buffer(buffer, n_channels) buffer = [] n_buffer = 0 if len(buffer) > 0: yield process_buffer(buffer, n_channels)
def _audio_decoder(path2file, sample_rate=8000, write_path=None): """ Input: path2file - название файла и путь до него sample_rate - желаемая на выходе частота дискертизации (по умолчанию 8кГц) write_path - путь и название сохраняемого файла, по умолчанию "None" Output: np.array() """ audio = np.array([]) with audioread.audio_open(path2file) as bytes_file: n_channels = bytes_file.channels sr = bytes_file.samplerate duration = bytes_file.duration for buf in bytes_file: part = np.frombuffer(buf, dtype=np.int16) audio = np.concatenate((audio, part)) audio = audio / abs(audio).max() if sr != sample_rate: new_samps = int(duration * sample_rate) audio = scipy.signal.resample(audio, new_samps) if write_path: wavfile.write(write_path, rate=sample_rate, data=audio) return audio
def generate_metadata(file): extension = os.path.splitext(file)[1].lower() file_name = os.path.basename(file).replace(file_prefix, '') file_name = file_name.replace(extension, '') metadata = {} metadata['name'] = file_name metadata['file'] = file metadata['path'] = os.path.dirname(file) metadata['extension'] = extension metadata['folder'] = os.path.basename(os.path.dirname(file)) metadata['collection'] = os.path.basename( os.path.dirname(os.path.dirname(file))) normalized_name = reduce((lambda x, y: x.replace(y, '')), [file_name] + replace_tokens) normalized_name = re.sub(r'[^a-zA-Z0-9]+', '', normalized_name).lower() metadata['normalized_name'] = normalized_name try: with audioread.audio_open(file) as f: metadata['channels'] = f.channels metadata['sample_rate'] = f.samplerate metadata['duration'] = f.duration metadata = spotify_metadata(file_name, metadata) except: pass metadata_file = os.path.join(os.path.dirname(file), '%s_metadata.json' % file_name) with open(metadata_file, 'w') as fp: json.dump(metadata, fp) print('Generated metafile: %s' % metadata_file)
def _load_mp3(filename): """ Decode a mp3 file from disk. """ y = [] try: with audioread.audio_open(filename) as input_file: sr_native = input_file.samplerate n_channels = input_file.channels if sr_native != 44100 or n_channels != 2: return np.array([]) for frame in input_file: frame = buf_to_float(frame) y.append(frame) y = np.concatenate(y) # reshape for stereo before parsing it to mono y = y.reshape((-1, n_channels)).T y = np.mean(y, axis=0) y = _resample(y, 44100, 16000) return y except Exception as e: print(filename, e) return np.array([])
def read(path, offset=0.0, dtype=np.float32): with audioread.audio_open(os.path.realpath(path)) as audio_file: samplerate = audio_file.samplerate n_channels = audio_file.channels s_start = int(np.round(samplerate * offset)) * n_channels s_end = np.inf n = 0 wave = list() for frame in audio_file: frame = buf2num(frame, dtype=dtype) n_prev = n n += len(frame) if n < s_start: # offset is after the current frame # keep reading continue if s_end < n_prev: # we're off the end. stop reading break if s_end < n: # the end is in this frame. crop. frame = frame[:s_end - n_prev] if n_prev <= s_start <= n: # beginning is in this frame frame = frame[(s_start - n_prev):] # tack on the current frame wave.append(frame) if wave: wave = np.concatenate(wave) if n_channels > 1: wave = wave.reshape((-1, n_channels)).T wave = mono(wave) wave = np.ascontiguousarray(wave, dtype=dtype) return Audio(wave, samplerate)
def save(self): self.pub_date_year = self.pub_date.date() self.slug = slugify(self.title) if len(self.slug) > 50: self.slug = self.slug[:49] if self.media: import mimetypes self.mimetype = mimetypes.guess_type(self.media.name) if 'audio' in self.mimetype: import audioread self.duration = int(audioread.audio_open(self.media.path).duration) elif ('video' in self.mimetype) and self.media: import subprocess process = subprocess.Popen( ['/usr/bin/ffmpeg', '-i', self.media.path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, stderr = process.communicate() matches = re.search( r"Duration:\s{1}(?P\d+?):(?P\d+?):(?P\d+\.\d+?),", stdout, re.DOTALL).groupdict() hours = Decimal(matches['hours']) minutes = Decimal(matches['minutes']) seconds = Decimal(matches['seconds']) total = 0 total += 60 * 60 * hours total += 60 * minutes total += seconds self.duration = total return super(Post, self).save()
def get_item_shape(self): for f in self.files: # af_info = AudioFile(filename=f).info af_info = audioread.audio_open(f) self.n_channels = af_info.channels if not self.mono else 1 duration_sec = af_info.duration # fs = af_info.samplerate self.duration_smp = int(duration_sec * self.desired_fs) self.duration_smp = int( numpy.ceil(self.duration_smp / self.desired_fs - 1 / self.desired_fs) * self.desired_fs) break # TODO: check if all files have same duration if self.segment: # compute number of frames self.n_frames = int( numpy.ceil(self.duration_smp / self.frame_size_smp0)) # compute final duration of each frame self.frame_size_smp = int(self.duration_smp / self.n_frames) return self.n_frames, self.frame_size_smp, self.n_channels else: return 1, self.duration_smp, self.n_channels
def decode(filename): filename = os.path.abspath(os.path.expanduser(filename)) if not os.path.exists(filename): print >> sys.stderr, "File not found." sys.exit(1) try: with audioread.audio_open(filename) as f: print >>sys.stderr, \ 'Input file: %i channels at %i Hz; %.1f seconds.' % \ (f.channels, f.samplerate, f.duration) print >>sys.stderr, 'Backend:', \ str(type(f).__module__).split('.')[1] with contextlib.closing(wave.open(filename + '.wav', 'w')) as of: of.setnchannels(f.channels) of.setframerate(f.samplerate) of.setsampwidth(2) for buf in f: of.writeframes(buf) except audioread.DecodeError: print >> sys.stderr, "File could not be decoded." sys.exit(1)
def play(self, filename, translate=False): # pragma: no cover ''' Plays the sounds. :filename: The input file name :translate: If True, it runs it through audioread which will translate from common compression formats to raw WAV. ''' # FIXME: Use platform-independent and async audio-output here # PyAudio looks most promising, too bad about: # --allow-external PyAudio --allow-unverified PyAudio if translate: with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f: fname = f.name with audioread.audio_open(filename) as f: with contextlib.closing(wave.open(fname, 'w')) as of: of.setnchannels(f.channels) of.setframerate(f.samplerate) of.setsampwidth(2) for buf in f: of.writeframes(buf) filename = fname if winsound: winsound.PlaySound(str(filename), winsound.SND_FILENAME) else: cmd = ['aplay', str(filename)] self._logger.debug('Executing %s', ' '.join([pipes.quote(arg) for arg in cmd])) subprocess.call(cmd) if translate: os.remove(fname)
def dataset_stat_per_file(root_path, datasetfile_path, audioroot_relpath='audio', annroot_relpath='annotations', get_duration=True): """Counts files and (optionally) sum file durations per label in dataset Args: root_path: root path of the audio and (optionally) annotation files. datasetfile_path: file containing a list of audio file paths relative to root_path audioroot_relpath: root path of the audio files, relative to root_path annroot_relpath: root path of the annotation files, relative to root_path get_duration: set to True to get total duration per label """ d_num = defaultdict(int) d_dur = defaultdict(float) filenames, labels = read_dataset_file(root_path, datasetfile_path, audioroot_relpath=audioroot_relpath, annroot_relpath=annroot_relpath) for filename, label in tqdm(zip(filenames, labels)): if get_duration: audio = audioread.audio_open(os.path.join(root_path, filename)) for l in label: d_num[l] += 1 if get_duration: d_dur[l] += audio.duration return d_num, d_dur
def set_remixing(self, stream): if self.remixing_blob is None: self.remixing_blob = Blob() with self.remixing_blob.open("w") as saveto: shutil.copyfileobj(stream, saveto) # cache duration for use in progress self.remixing_duration = audioread.audio_open( self.remixing_blob._p_blob_uncommitted).duration
def extract_samples2(filepath, offset=20): with audio_open(filepath) as f: if f.duration < 120.0: return None, None data, sample_rate = librosa.load(filepath, sr=32000, offset=offset, duration=60.0) data_max, data_min = max(data) - min(data), min(data) data = (data - data_min)/(data_max) return data, sample_rate
def _fingerprint_file_audioread(path,return_raw=False): """Fingerprint a file by using audioread and chromaprint.""" try: with audioread.audio_open(path) as f: duration = f.duration fp = fingerprint(f.samplerate, f.channels, iter(f),return_raw=return_raw) except audioread.DecodeError: raise FingerprintGenerationError("audio could not be decoded") return duration, fp
def Load(self, filename): with audioread.audio_open(filename) as f: self.duration = int(f.duration * 1000) self.samplerate = f.samplerate self.channels = f.channels mixer.quit() mixer.init(frequency=self.samplerate, channels=self.channels) music.load(filename) self.pos = 0 self.has_music = True
def test_audioread_full(audiofile): """Read the audio data from the file.""" with audioread.audio_open(audiofile.path) as a: assert int(a.duration) == int(audiofile.duration) assert a.channels == audiofile.channels assert a.samplerate == audiofile.samplerate # Now read all the data and assert that it's the correct type. for block in a: assert type(block) == bytes
def process_file(self, filename): # Check for an existing RecordingFile digest = hashlib.sha256() with open(filename, 'rb') as data: done = False while not done: chunk = data.read(65536) digest.update(chunk) done = bool(chunk) rf_id = urlsafe_b64encode(digest.digest()) try: rf = RecordingFile.objects.get(sha256=rf_id) if rf.process_version >= PROCESS_VERSION and not self.force: return except RecordingFile.DoesNotExist: rec = Recording() rec.save() rf = RecordingFile( sha256=rf_id, recording=rec, ) rf.process_version = PROCESS_VERSION mfile = mutagen.File(filename) if mfile is None: # mutagen doesn't understand the format return if not (set(mfile.mime) & self.ALLOWED_TYPES): print '\tNone of these are allowed: ' + u' '.join(mfile.mime) return rf.mimetypes = mfile.mime rf.info_data = serialize_info(mfile.info) rf.tag_data = serialize_tags(mfile.tags) rf.parsed_data = parse_data(rf, mfile.tags) # dont compute the sound hash if this is an existing file. if not rf.pk: sound_hash = hashlib.sha256() L.debug("Computing sound_hash.") with audioread.audio_open(filename) as pcm: for block in pcm: sound_hash.update(block) rf.sound_hash = urlsafe_b64encode(sound_hash.digest()) L.debug("sound_hash: %s", rf.sound_hash) rf.save() sfs = rf.serverfile_set.filter(path=filename) if not sfs.exists(): ServerFile.objects.create( recording_file=rf, path=filename, ) create_data(rf, rf.parsed_data)
def __audioread_load(path, offset, duration, dtype): '''Load an audio buffer using audioread. This loads one block at a time, and then concatenates the results. ''' y = [] with audioread.audio_open(path) as input_file: sr_native = input_file.samplerate n_channels = input_file.channels s_start = int(np.round(sr_native * offset)) * n_channels if duration is None: s_end = np.inf else: s_end = s_start + (int(np.round(sr_native * duration)) * n_channels) n = 0 for frame in input_file: frame = util.buf_to_float(frame, dtype=dtype) n_prev = n n = n + len(frame) if n < s_start: # offset is after the current frame # keep reading continue if s_end < n_prev: # we're off the end. stop reading break if s_end < n: # the end is in this frame. crop. frame = frame[:s_end - n_prev] if n_prev <= s_start <= n: # beginning is in this frame frame = frame[(s_start - n_prev):] # tack on the current frame y.append(frame) if y: y = np.concatenate(y) if n_channels > 1: y = y.reshape((-1, n_channels)).T else: y = np.empty(0, dtype=dtype) return y, sr_native
def test_audioread_early_exit(audiofile): """Abort the read before it is completed. This test guards against regressions such as https://github.com/beetbox/audioread/pull/78 """ with audioread.audio_open(audiofile.path) as a: assert int(a.duration) == int(audiofile.duration) assert a.channels == audiofile.channels assert a.samplerate == audiofile.samplerate
def get_info(filename): '''Get the information of the track''' audiofile = eyed3.load(filename) audio = audioread.audio_open(filename) info = dict( artist = audiofile.tag.artist, title = audiofile.tag.title, channels = audio.channels, samplerate = audio.samplerate, duration = audio.duration, filename = filename, ) return info
def _stream_audio(self): self._file = audioread.audio_open( os.path.join(os.path.dirname(__file__), "test.mp3" )) print "playing,%d,%d" % (self._file.channels, self._file.samplerate) self.send_all("playing,%d,%d" % (self._file.channels, self._file.samplerate)) for buff in self._file: for sink in self.sinks: sink.sendall(buff) self._file.close() self.lock.acquire() self.is_playing = False self.lock.release()
def load_audio_from_file(filename): # load the audio data from the file temp = bytearray() sr = 0 with audio_open(filename) as audio: sr = audio.samplerate nchannels = audio.channels # chunk size can be specified with 'block_samples' (default 1024): for chunk in audio.read_data(): temp.extend(chunk) data = frombuffer(temp, dtype='<i2').reshape(-1, nchannels)[:,0] return AudioObject(data, sample_rate=sr)
def add_file(self, fil): a = QtGui.QTreeWidgetItem(self.ui.playlist) a.setFlags(a.flags() & ~QtCore.Qt.ItemIsDropEnabled) id3r = id3reader.Reader(str(fil)) if id3r.getValue("performer") != None: a.setText(0, id3r.getValue("performer")) else: a.setText(0, "---") if id3r.getValue("title") != None: a.setText(1, id3r.getValue("title")) else: a.setText(1, "---") time = int(audioread.audio_open(str(fil)).duration) a.setText(2, "%i:%i (%is)" % (time / 60, time % 60, time)) a.setText(3, "---") a.setText(4, fil)
def parse_mp3(): while True: if PARSE_Q.empty() and threading.active_count() == 1: print "Finish parse feed" return if PARSE_Q.empty(): time.sleep(3) continue task = PARSE_Q.get() print 'Parse %s' % task['file'] try: with audioread.audio_open(task['file']) as mp3: task['duration'] = int(mp3.duration) except: continue save_result(task)
def _loadNextFile(self): self.currentFilename = next(self.filenameIter) queuedCallbacks.extend(self.onSongChanged) self.file = audioread.audio_open(self.currentFilename) blockSize = self.framesPerChunk * self.file.channels * bytes_per_frame_per_channel try: # MAD (pymad) self.sampleIter = self.file.read_blocks(blockSize) except AttributeError: try: # FFMpeg (command line) self.sampleIter = self.file.read_data(blockSize) except AttributeError: # gstreamer (pygst) self.sampleIter = iter(self.file)
def match(apikey, path, meta=DEFAULT_META, parse=True): """Look up the metadata for an audio file. If ``parse`` is true, then ``parse_lookup_result`` is used to return an iterator over small tuple of relevant information; otherwise, the full parsed JSON response is returned. """ path = os.path.abspath(os.path.expanduser(path)) try: with audioread.audio_open(path) as f: duration = f.duration fp = fingerprint(f.samplerate, f.channels, iter(f)) except audioread.DecodeError: raise FingerprintGenerationError("audio could not be decoded") response = lookup(apikey, fp, duration, meta) if parse: return parse_lookup_result(response) else: return response
def loadNextFile(self): self.currentFilename = next(self.filenameIter).encode("utf-8") if self.currentFilename is None: mainLoop.currentProcess.queuedCallbacks.append(self.nextChunk) return print("Loading file {!r}.".format(self.currentFilename)) tags = hsaudiotag.auto.File(self.currentFilename) if not tags.valid: print("Couldn't read tags!") else: print( json.dumps({"artist": tags.artist, "album": tags.album, "title": tags.title, "duration": tags.duration}) ) self.tags = tags self.file = audioread.audio_open(self.currentFilename) songInfo = {"channels": self.channels, "samplerate": self.samplerate, "duration": self.duration} ansi.info( "Loaded song {!r}; channels: {}; samplerate: {}; duration: {} (duration from tags: {})", self.currentFilename, self.channels, self.samplerate, self.duration, tags.duration, ) mainLoop.currentProcess.queueCall(self.onSongChanged, tags, songInfo) blockSize = self.framesPerChunk * self.file.channels * self.bytes_per_frame_per_channel try: # MAD (pymad) self.sampleIter = self.file.read_blocks(blockSize) except AttributeError: try: # FFMpeg (command line) self.sampleIter = self.file.read_data(blockSize) except AttributeError: # gstreamer (pygst) self.sampleIter = iter(self.file)
def get_audio_duration(current_file): """Return audio file duration Parameters ---------- current_file : dict Dictionary given by pyannote.database. Returns ------- duration : float Audio file duration. """ path = current_file['audio'] with audioread.audio_open(path) as f: duration = f.duration return duration
def set_unmixed(self, stream): if self.dry_blob is None: self.dry_blob = Blob() if self.remixing_blob is None: self.remixing_blob = Blob() with self.dry_blob.open('w') as dry: with self.remixing_blob.open('w') as remixing: while True: data = stream.read(1<<19) # 512K if not data: break dry.write(data) remixing.write(data) # cache duration for use in progress duration = audioread.audio_open( self.dry_blob._p_blob_uncommitted).duration # cache duration for use in progress self.dry_duration = duration self.remixing_duration = duration
def getSongInfo(filepath): if has_stagger: try: tag = stagger.read_tag(filepath) except Exception: tag = MockTag() else: tag = MockTag() if has_audioread: try: with audioread.audio_open(filepath) as f: audiolength = f.duration except Exception: log.e('audioread failed! (%s)', filepath) audiolength = 0 else: audiolength = 0 return Metainfo(tag.artist, tag.album, tag.title, tag.track, audiolength)
def get_audio_sample_rate(current_file): """Return audio file sampling rate Parameters ---------- current_file : dict Dictionary given by pyannote.database. Returns ------- sample_rate : int Sampling rate """ path = current_file['audio'] with audioread.audio_open(path) as f: sample_rate = f.samplerate return sample_rate
def read_file_audioread(filename): import audioread # taken from librosa.util.utils def convert_buffer_to_float(buf, n_bytes = 2, dtype = np.float32): # Invert the scale of the data scale = 1./float(1 << ((8 * n_bytes) - 1)) # Construct the format string fmt = '<i{:d}'.format(n_bytes) # Rescale and format the data buffer out = scale * np.frombuffer(buf, fmt).astype(dtype) return out with audioread.audio_open(filename) as f: total_frames = 0 for buf in f: samples = convert_buffer_to_float(buf) samples = samples.reshape(f.channels, -1) total_frames += samples.shape[1] return total_frames, f.samplerate