def test_available_subtypes(): subtypes = sf.available_subtypes() assert 'PCM_24' in subtypes assert 'FLOAT' in subtypes assert 'VORBIS' in subtypes subtypes = sf.available_subtypes('WAV') assert 'PCM_24' in subtypes assert 'FLOAT' in subtypes assert 'VORBIS' not in subtypes subtypes = sf.available_subtypes('nonsense') assert subtypes == {}
def test_available_subtypes(): subtypes = sf.available_subtypes() assert 'PCM_24' in subtypes assert 'FLOAT' in subtypes assert 'VORBIS' in subtypes subtypes = sf.available_subtypes('WAV') assert 'PCM_24' in subtypes assert 'FLOAT' in subtypes assert 'VORBIS' not in subtypes subtypes = sf.available_subtypes('nonsense') assert subtypes == {}
def cli(input_file, input_text, speed, tone, output_file, output_format, output_subtype, list_formats, list_subtypes, letter_space_factor): if list_formats: for key, value in soundfile.available_formats().items(): click.echo('\x1b[1m{:<10}\x1b[0m {}'.format(key, value)) return if list_subtypes: for key, value in soundfile.available_subtypes(list_subtypes).items(): click.echo('\x1b[1m{:<10}\x1b[0m {}'.format(key, value)) return if not input_text: input_text = input_file.read() options = dict(samplerate=44100, channels=1, subtype=output_subtype, format=output_format) with soundfile.SoundFile(output_file, 'w', **options) as fp: stream_morse_code(fp, input_text, wpm=speed, tone=tone, letter_space_factor=letter_space_factor)
def find_soundfile_subtype(depth, default=16): """ Find wav subtype for given bit depth""" try: subs = sf.available_subtypes("wav") r = next(k for k in subs if k.startswith("PCM") and k.endswith(str(depth))) except StopIteration: print("depth not available, default {} bits selected".format(default)) r = "PCM_{}".format(default) return r
def change_format_and_subtype(audio_path): audio, sr = sf.read(audio_path) audio_info = sf.info(audio_path) formats = ['WAV', 'FLAC'] if audio_info.format in formats: formats.remove(audio_info.format) _format = random.choice(formats) subtypes = sf.available_subtypes(_format) accepted_subtypes = ['PCM_16', 'PCM_32', 'PCM_24', 'FLOAT', 'DOUBLE'] subtypes = [s for s in subtypes.keys() if s in accepted_subtypes] if audio_info.subtype in subtypes: subtypes.remove(audio_info.subtype) _subtype = random.choice(subtypes) sf.write(audio_path, audio, sr, subtype=_subtype, format=_format)
import wave import librosa import soundfile as sf import numpy as np x, _ = librosa.load('./8bit_8000hz.WAV', sr=8000) print(len(x)) sf.write('tmp.wav', x, 8000, 'PCM_U8') # wave.open('tmp.wav','r') # sf.write('stereo_file.flac', np.random.randn(10), 8000, 'PCM_S8') print("fac types", sf.available_subtypes('FLAC')) print("wav types", sf.available_subtypes('WAV'))
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')