def saveTo(self, file): with ZipFile(file, 'w') as zip: song_file = configparser.ConfigParser() song_file['DEFAULT'] = {'volume': self.volume, 'bpm': self.bpm, 'beat_per_bar': self.beat_per_bar, 'width': self.width, 'height': self.height} for clip in self.clips: clip_file = {'name': clip.name, 'volume': str(clip.volume), 'frame_offset': str(clip.frame_offset), 'beat_offset': str(clip.beat_offset), 'beat_diviser': str(clip.beat_diviser), 'audio_file': basename( clip.audio_file)} if clip_file['audio_file'] is None: clip_file['audio_file'] = 'no-sound' song_file["%s/%s" % (clip.x, clip.y)] = clip_file buffer = StringIO() song_file.write(buffer) zip.writestr('metadata.ini', buffer.getvalue()) for member in self.data: buffer = BytesIO() sf.write(self.data[member], buffer, self.samplerate[member], subtype=sf.default_subtype('WAV'), format='WAV') zip.writestr(member, buffer.getvalue()) self.file_name = file
def __init__(self, output_path, script_path=None, audio_format='wav', subtype=None, scp_sep=' '): self.output_path = output_path self.script_path = script_path self.audio_format = audio_format self.scp_sep = scp_sep assert '.' + self.audio_format in valid_ext if subtype is None: self.subtype = sf.default_subtype(self.audio_format) else: self.subtype = subtype assert sf.check_format(self.audio_format, self.subtype) if not os.path.exists(output_path): os.makedirs(output_path) if script_path is not None: self.f_script = open(script_path, 'w') else: self.f_script = None
def resample_file(input_path, output_audio_path, output_rate): output_path = input_to_output_path(input_path, output_audio_path) wav_or, sr = sf.read(input_path) wav_rs = librosa.resample(wav_or, sr, output_rate) sf.write(output_path, wav_rs, output_rate, sf.default_subtype("wav")) file_size = os.path.getsize(output_path) return file_size
def onExportClip(self): if self.last_clip and self.last_clip.audio_file: audio_file = self.last_clip.audio_file file_name, a = self.getSaveFileName( 'Export Clip : %s' % self.last_clip.name, 'WAVE (*.wav)') if file_name: file_name = verify_ext(file_name, 'wav') sf.write(self.song.data[audio_file], file_name, self.song.samplerate[audio_file], subtype=sf.default_subtype('WAV'), format='WAV')
def test_default_subtype(): assert sf.default_subtype('FLAC') == 'PCM_16' assert sf.default_subtype('RAW') is None with pytest.raises(ValueError) as excinfo: sf.default_subtype('nonsense') assert str(excinfo.value) == "Unknown format: 'nonsense'" with pytest.raises(TypeError) as excinfo: sf.default_subtype(666) assert str(excinfo.value) == "Invalid format: 666"
def saveTo(self, file): with ZipFile(file, 'w') as zip: song_file = configparser.ConfigParser() song_file['DEFAULT'] = { 'volume': self.volume, 'annotation': self.annotation, 'bpm': self.bpm, 'beat_per_bar': self.beat_per_bar, 'width': self.width, 'height': self.height, 'scenes': json.dumps(self.scenes) } if self.initial_scene is not None: song_file['DEFAULT']['initial_scene'] = self.initial_scene for clip in self.clips: clip_file = { 'name': clip.name, 'volume': str(clip.volume), 'frame_offset': str(clip.frame_offset), 'beat_offset': str(clip.beat_offset), 'beat_diviser': str(clip.beat_diviser), 'output': clip.output, 'mute_group': str(clip.mute_group), 'one_shot': str(clip.one_shot), 'lock_rec': str(clip.lock_rec), 'always_play': str(clip.always_play), 'audio_file': basename(clip.audio_file) } if clip_file['audio_file'] is None: clip_file['audio_file'] = 'no-sound' song_file["%s/%s" % (clip.x, clip.y)] = clip_file buffer = StringIO() song_file.write(buffer) zip.writestr('metadata.ini', buffer.getvalue()) for member in self.data: buffer = BytesIO() sf.write(buffer, self.data[member], self.samplerate[member], subtype=sf.default_subtype('WAV'), format='WAV') zip.writestr(member, buffer.getvalue()) self.file_name = file
def write_audio(path, audio, fs, bitdepth=None): ext = os.path.splitext(path)[1].lower() if bitdepth==None: sf.write(file=path, data=audio, samplerate=fs) # whatever is default in soundfile if bitdepth==24 and (ext=='.wav' or ext=='.flac'): #print('Writing 24 bits pcm, yay!') sf.write(file=path, data=audio, samplerate=fs, subtype='PCM_24') elif bitdepth==32: if ext=='.wav': sf.write(file=path, data=audio, samplerate=fs, subtype='PCM_32') else: print('Writing into {} format with bit depth {} is not supported, reverting to the default {}'.format( ext, bitdepth, sf.default_subtype(ext[1:]))) sf.write(file=path, data=audio, samplerate=fs) elif bitdepth==16: sf.write(file=path, data=audio, samplerate=fs, subtype='PCM16') else: raise IOError('Unexpected bit depth {}'.format(bitdepth))
def saveTo(self, file): with ZipFile(file, 'w') as zip: song_file = configparser.ConfigParser() port_list = list(self.outputsPorts) song_file['DEFAULT'] = {'volume': self.volume, 'bpm': self.bpm, 'beat_per_bar': self.beat_per_bar, 'width': self.width, 'height': self.height, 'outputs': json.dumps(port_list), 'scenes': json.dumps(self.scenes)} if self.initial_scene is not None: song_file['DEFAULT']['initial_scene'] = self.initial_scene for clip in self.clips: clip_file = {'name': clip.name, 'volume': str(clip.volume), 'frame_offset': str(clip.frame_offset), 'beat_offset': str(clip.beat_offset), 'beat_diviser': str(clip.beat_diviser), 'output': clip.output, 'mute_group': str(clip.mute_group), 'audio_file': basename( clip.audio_file)} if clip_file['audio_file'] is None: clip_file['audio_file'] = 'no-sound' song_file["%s/%s" % (clip.x, clip.y)] = clip_file buffer = StringIO() song_file.write(buffer) zip.writestr('metadata.ini', buffer.getvalue()) for member in self.data: buffer = BytesIO() sf.write(buffer, self.data[member], self.samplerate[member], subtype=sf.default_subtype('WAV'), format='WAV') zip.writestr(member, buffer.getvalue()) self.file_name = file
def saveTo(self, file): with ZipFile(file, 'w') as zip: song_file = configparser.ConfigParser() song_file['DEFAULT'] = { 'volume': self.volume, 'bpm': self.bpm, 'beat_per_bar': self.beat_per_bar, 'width': self.width, 'height': self.height } for clip in self.clips: clip_file = { 'name': clip.name, 'volume': str(clip.volume), 'frame_offset': str(clip.frame_offset), 'beat_offset': str(clip.beat_offset), 'beat_diviser': str(clip.beat_diviser), 'audio_file': basename(clip.audio_file) } if clip_file['audio_file'] is None: clip_file['audio_file'] = 'no-sound' song_file["%s/%s" % (clip.x, clip.y)] = clip_file buffer = StringIO() song_file.write(buffer) zip.writestr('metadata.ini', buffer.getvalue()) for member in self.data: buffer = BytesIO() sf.write(self.data[member], buffer, self.samplerate[member], subtype=sf.default_subtype('WAV'), format='WAV') zip.writestr(member, buffer.getvalue()) self.file_name = file
import tornado.httpserver import json import numpy as np import sys import os import pickle import requests import re from operator import itemgetter from tornado.options import define, options import librosa import soundfile as sf import glob from time import gmtime, strftime sf.default_subtype('WAV') sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) from Speaker_Verification.src.utils.infer_process import InferPrep # config update CONFIG_FILE = os.path.abspath(os.path.join(os.pardir,'Speaker_Verification','config', 'config.json')) with open(CONFIG_FILE, 'rb') as fid: config = json.load(fid) config_update = { "preemphasis_coef": 0.97, "fft_window_fn": "hann", "fft_is_center": True, "is_legacy": False,
def mk_reader_and_writer(sr: int, format='RAW', subtype='PCM_16', dtype='int16', channels: int = 1, endian=None, always_2d=False): """Makes a (bijective) pair of numerical arrays serializer and deserializer functions. A function returning bijective panel data reader and writer functions with simple interfaces (all parametrizations are fixed): `read(source)` and `write(source, data)`. The writer and reader are essentially matrix (or array) serializers and deserializers respectively, using the same serialization protocols as waveform (into PCM, WAV, etc.) Args: sr: Sample rate. When the serialization format handles this information (e.g. WAV format) the sample rate is actually written (in WAV, in the header bytes), and asserted on reads (that is, if you read a WAV file that doesn't have that exact sample rate in it's header, a WrongSampleRate error will be raised. When the serialization format doesn't (e.g. RAW format (a.k.a. PCM)), it is ignored both on reads and writes format: 'RAW', 'WAV' and others (see soundfile.available_formats() for a full list) subtype: 'FLOAT', 'PCM_16' and others (see soundfile.available_subtypes() for a full list) dtype: 'float64', 'float32', 'int32', 'int16' channels: Number of channels (should equal the number of columns of the data matrices that will be serialized -- or 1 if the data is flat) endian: see soundfile documentation ({'FILE', 'LITTLE', 'BIG', 'CPU'}, sometimes optional) always_2d: By default, reading a mono sound file will return a one-dimensional array. With always_2d=True, data is always returned as a two-dimensional array, even if the data has only one channel. Returns: read(k), write(k, v) functions >>> n_channels, dtype = 1, 'float64' >>> read, write = mk_reader_and_writer(sr=44100, channels=n_channels, subtype='FLOAT', format='RAW', dtype=dtype) >>> data = _random_matrix(n_channels=n_channels, dtype=dtype) >>> _test_data_write_read(data, writer=write, reader=read) >>> n_channels, dtype = 4, 'int16' >>> read, write = mk_reader_and_writer(sr=2, channels=n_channels, subtype='PCM_16', format='RAW', dtype=dtype) >>> data = _random_matrix(n_channels=n_channels, dtype=dtype) >>> _test_data_write_read(data, writer=write, reader=read) """ if not sf.check_format(format, subtype, endian): raise WrongSerializationParams( f"Not a valid combo: format={format}, subtype={subtype}, endian={endian}" ) subtype = subtype or sf.default_subtype(format) if format == 'RAW': def read(k): wf, _ = sf.read(k, samplerate=sr, channels=channels, format=format, subtype=subtype, dtype=dtype, endian=endian, always_2d=always_2d) return wf else: def read(k): wf, sr_read = sf.read(k, dtype=dtype, always_2d=always_2d) if sr != sr_read: raise WrongSampleRate( f"Sample rate was {sr_read}: Expected {sr}") return wf def write(k, v): return sf.write(k, v, samplerate=sr, format=format, subtype=subtype, endian=endian) # add some attributes to the functions, for diagnosis purposes read.sr = sr read.dtype = dtype read.format = format write.sr = sr write.format = format write.subtype = subtype return read, write
import soundfile as sf if __name__ == '__main__': print(sf.default_subtype('WAV')) print(sf.available_subtypes('WAV')) # DATA, SAMPLERATE = sf.read(file='test_in_32bit_float.wav', dtype='float32') # sf.write(file='test_out_32bit_float.wav', data=DATA, samplerate=SAMPLERATE, subtype='PCM_16') # read int -> float # DATA, SAMPLERATE = sf.read(file='test_in_16bit_int.wav', dtype='float32') # sf.write(file='test_out_32bit_float.wav', data=DATA, samplerate=SAMPLERATE, subtype='FLOAT') # read float -> int # DATA, SAMPLERATE = sf.read(file='test_in_32bit_float.wav', dtype='int16') # sf.write(file='test_out_16bit_int.wav', data=DATA, samplerate=SAMPLERATE, subtype='PCM_16') # write int -> float # DATA, SAMPLERATE = sf.read(file='test_in_16bit_int.wav', dtype='int16') # sf.write(file='test_out_32bit_float.wav', data=DATA, samplerate=SAMPLERATE, subtype='FLOAT') # write float -> int print(sf.info('test_in_32bit_float.wav', False)) DATA, SAMPLERATE = sf.read(file='test_in_32bit_float.wav', dtype='float32') sf.write(file='test_out_16bit_int.wav', data=DATA, samplerate=SAMPLERATE, subtype='PCM_16')