Python soundfile.write() Examples

The following are 30 code examples of soundfile.write(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module soundfile , or try the search function .
Example #1
Source File: rapSpeaker.py    From rapLyrics with MIT License 11 votes vote down vote up
def doFileStuff(line,isSlow):
    myobj = gTTS(text=line, lang='en', slow=isSlow) 
    myobj.save("placeholder.mp3")
    
    y, sr = librosa.load("placeholder.mp3")
    data = librosa.resample(y, sr, SAMPLE_RATE)
    librosa.output.write_wav('placeholder.wav', data, SAMPLE_RATE)
    d, sr = sf.read('placeholder.wav')
    sf.write('placeholder.wav', d, sr)

    y, sr = librosa.load("placeholder.mp3")
    lowData = librosa.resample(y, sr, SAMPLE_RATE*LOW_FACTOR)
    librosa.output.write_wav('lowPlaceholder.wav', lowData, SAMPLE_RATE)
    d, sr = sf.read('lowPlaceholder.wav')
    sf.write('lowPlaceholder.wav', d, sr)

    return data 
Example #2
Source File: audio.py    From parallel-wavenet-vocoder with MIT License 8 votes vote down vote up
def prepro_audio(source_path, target_path, format=None, sr=None, db=None):
    """
    Read a wav, change sample rate, format, and average decibel and write to target path.
    :param source_path: source wav file path
    :param target_path: target wav file path
    :param sr: sample rate.
    :param format: output audio format.
    :param db: decibel.
    """
    sound = AudioSegment.from_file(source_path, format)
    if sr:
        sound = sound.set_frame_rate(sr)
    if db:
        change_dBFS = db - sound.dBFS
        sound = sound.apply_gain(change_dBFS)
    sound.export(target_path, 'wav') 
Example #3
Source File: cli_writers.py    From espnet with Apache License 2.0 6 votes vote down vote up
def get_num_frames_writer(write_num_frames: str):
    """get_num_frames_writer

    Examples:
        >>> get_num_frames_writer('ark,t:num_frames.txt')
    """
    if write_num_frames is not None:
        if ":" not in write_num_frames:
            raise ValueError(
                'Must include ":", write_num_frames={}'.format(write_num_frames)
            )

        nframes_type, nframes_file = write_num_frames.split(":", 1)
        if nframes_type != "ark,t":
            raise ValueError(
                "Only supporting text mode. "
                "e.g. --write-num-frames=ark,t:foo.txt :"
                "{}".format(nframes_type)
            )

    return open(nframes_file, "w", encoding="utf-8") 
Example #4
Source File: audio.py    From amen with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def output(self, filename, format=None):
        """
        Write the samples out to the given filename.

        Parameters
        ----------
        filename : str
            The path to write the audio on disk.
            This can be any format supported by `pysoundfile`, including
            `WAV`, `FLAC`, or `OGG` (but not `mp3`).

        format : str
            If provided, explicitly set the output encoding format.
            See `soundfile.available_formats`.
        """
        sf.write(filename, self.raw_samples.T, int(self.sample_rate), format=format) 
Example #5
Source File: cli_writers.py    From adviser with GNU General Public License v3.0 6 votes vote down vote up
def get_num_frames_writer(write_num_frames: str):
    """get_num_frames_writer

    Examples:
        >>> get_num_frames_writer('ark,t:num_frames.txt')
    """
    if write_num_frames is not None:
        if ':' not in write_num_frames:
            raise ValueError('Must include ":", write_num_frames={}'
                             .format(write_num_frames))

        nframes_type, nframes_file = write_num_frames.split(':', 1)
        if nframes_type != 'ark,t':
            raise ValueError(
                'Only supporting text mode. '
                'e.g. --write-num-frames=ark,t:foo.txt :'
                '{}'.format(nframes_type))

    return open(nframes_file, 'w', encoding='utf-8') 
Example #6
Source File: utils.py    From sklearn-audio-transfer-learning with ISC License 6 votes vote down vote up
def wavefile_to_waveform(wav_file, features_type):
    data, sr = sf.read(wav_file)
    if features_type == 'vggish':
        tmp_name = str(int(np.random.rand(1)*1000000)) + '.wav'
        sf.write(tmp_name, data, sr, subtype='PCM_16')
        sr, wav_data = wavfile.read(tmp_name)
        os.remove(tmp_name)
        # sr, wav_data = wavfile.read(wav_file) # as done in VGGish Audioset
        assert wav_data.dtype == np.int16, 'Bad sample type: %r' % wav_data.dtype
        data = wav_data / 32768.0  # Convert to [-1.0, +1.0]
  
    # at least one second of samples, if not repead-pad
    src_repeat = data
    while (src_repeat.shape[0] < sr): 
        src_repeat = np.concatenate((src_repeat, data), axis=0)
        data = src_repeat[:sr]

    return data, sr 
Example #7
Source File: audio.py    From voice-vector with MIT License 6 votes vote down vote up
def prepro_audio(source_path, target_path, format=None, sr=None, db=None):
    """
    Read a wav, change sample rate, format, and average decibel and write to target path.
    :param source_path: source wav file path
    :param target_path: target wav file path
    :param sr: sample rate.
    :param format: output audio format.
    :param db: decibel.
    """
    sound = AudioSegment.from_file(source_path, format)
    if sr:
        sound = sound.set_frame_rate(sr)
    if db:
        change_dBFS = db - sound.dBFS
        sound = sound.apply_gain(change_dBFS)
    sound.export(target_path, 'wav') 
Example #8
Source File: test_beamformers.py    From beamformers with MIT License 6 votes vote down vote up
def test_mwf(self):

        true_mwf, fs = sf.read('wavs/mwf.wav')

        out_mwf = MWF_Oracle(self.mix, self.nn, self.spk, frame_len=self.frame_len, frame_step=self.frame_step)
        sf.write('wavs/test_out.wav', out_mwf, self.fs)
        out_mwf, fs = sf.read('wavs/test_out.wav')

        np.testing.assert_equal(out_mwf, true_mwf)

    # def test_bfi(self):
    #
    #     out_bfi = BeamformIt(self.mix).astype('float64')
    #
    #     sf.write('wavs/tout.wav', out_bfi, 8000)
    #
    #     true_bfi, fs = sf.read('wavs/bfi.wav')
    #     out_bfi, fs = sf.read('wavs/tout.wav')
    #
    #     np.testing.assert_equal(out_bfi, true_bfi) 
Example #9
Source File: test_sound_scp.py    From espnet with Apache License 2.0 6 votes vote down vote up
def test_SoundScpReader_normalize(tmp_path: Path):
    audio_path1 = tmp_path / "a1.wav"
    audio1 = np.random.randint(-100, 100, 16, dtype=np.int16)
    audio_path2 = tmp_path / "a2.wav"
    audio2 = np.random.randint(-100, 100, 16, dtype=np.int16)

    audio1 = audio1.astype(np.float64) / (np.iinfo(np.int16).max + 1)
    audio2 = audio2.astype(np.float64) / (np.iinfo(np.int16).max + 1)

    soundfile.write(audio_path1, audio1, 16)
    soundfile.write(audio_path2, audio2, 16)

    p = tmp_path / "dummy.scp"
    with p.open("w") as f:
        f.write(f"abc {audio_path1}\n")
        f.write(f"def {audio_path2}\n")

    desired = {"abc": (16, audio1), "def": (16, audio2)}
    target = SoundScpReader(p, normalize=True)

    for k in desired:
        rate1, t = target[k]
        rate2, d = desired[k]
        assert rate1 == rate2
        np.testing.assert_array_equal(t, d) 
Example #10
Source File: audio.py    From ZASR_tensorflow with Apache License 2.0 6 votes vote down vote up
def to_wav_file(self, filepath, dtype='float32'):
        """Save audio segment to disk as wav file.
        
        :param filepath: WAV filepath or file object to save the
                         audio segment.
        :type filepath: basestring|file
        :param dtype: Subtype for audio file. Options: 'int16', 'int32',
                      'float32', 'float64'. Default is 'float32'.
        :type dtype: str
        :raises TypeError: If dtype is not supported.
        """
        samples = self._convert_samples_from_float32(self._samples, dtype)
        subtype_map = {
            'int16': 'PCM_16',
            'int32': 'PCM_32',
            'float32': 'FLOAT',
            'float64': 'DOUBLE'
        }
        soundfile.write(
            filepath,
            samples,
            self._sample_rate,
            format='WAV',
            subtype=subtype_map[dtype]) 
Example #11
Source File: test_dataset.py    From espnet with Apache License 2.0 6 votes vote down vote up
def pipe_wav(tmp_path):
    p = tmp_path / "wav.scp"
    soundfile.write(
        tmp_path / "a.wav",
        np.random.randint(-100, 100, (160000,), dtype=np.int16),
        16000,
    )
    soundfile.write(
        tmp_path / "b.wav",
        np.random.randint(-100, 100, (80000,), dtype=np.int16),
        16000,
    )
    with p.open("w") as f:
        f.write(f"a {tmp_path / 'a.wav'}\n")
        f.write(f"b {tmp_path / 'b.wav'}\n")
    return str(p) 
Example #12
Source File: prepare_data.py    From music_transcription_MAPS with MIT License 6 votes vote down vote up
def compute_scaler(args):
    """Compute and write out scaler from already packed feature file. Using 
    scaler in training neural network can speed up training. 
    """
    workspace = args.workspace
    feat_type = args.feat_type
    
    # Load packed features. 
    t1 = time.time()
    packed_feat_path = os.path.join(workspace, "packed_features", feat_type, "train.p")
    [x_list, _, _] = cPickle.load(open(packed_feat_path, 'rb'))
    
    # Compute scaler. 
    x_all = np.concatenate(x_list)
    scaler = preprocessing.StandardScaler(with_mean=True, with_std=True).fit(x_all)
    print(scaler.mean_)
    print(scaler.scale_)
    
    # Save out scaler. 
    out_path = os.path.join(workspace, "scalers", feat_type, "scaler.p")
    create_folder(os.path.dirname(out_path))
    pickle.dump(scaler, open(out_path, 'wb'))
    print("Compute scaler finished! %s s" % (time.time() - t1,)) 
Example #13
Source File: normalise_level.py    From ophelia with Apache License 2.0 6 votes vote down vote up
def process(wavefile, outdir, pattern=''):
    _, base = os.path.split(wavefile)
    
    if pattern:
        if pattern not in base:
            return

    # print base

    raw_in = os.path.join(outdir, base.replace('.wav','.raw'))
    raw_out = os.path.join(outdir, base.replace('.wav','_norm.raw'))
    logfile = os.path.join(outdir, base.replace('.wav','.log'))
    wav_out = os.path.join(outdir, base)
    
    data, samplerate = sf.read(wavefile, dtype='int16')
    sf.write(raw_in, data, samplerate, subtype='PCM_16')
    os.system('%s -log %s -q -lev -26.0 -sf %s %s %s'%(sv56, logfile, samplerate, raw_in, raw_out))
    norm_data, samplerate = sf.read(raw_out, dtype='int16', samplerate=samplerate, channels=1, subtype='PCM_16')
    sf.write(wav_out, norm_data, samplerate)

    os.system('rm %s %s'%(raw_in, raw_out)) 
Example #14
Source File: copy_synth_SSRN_GL.py    From ophelia with Apache License 2.0 6 votes vote down vote up
def copy_synth_SSRN_GL(hp, outdir):

    safe_makedir(outdir)

    dataset = load_data(hp, mode="synthesis") 
    fnames, texts = dataset['fpaths'], dataset['texts']
    bases = [basename(fname) for fname in fnames]
    mels = [np.load(os.path.join(hp.coarse_audio_dir, base + '.npy')) for base in bases]
    lengths = [a.shape[0] for a in mels]
    mels = list2batch(mels, 0)

    g = SSRNGraph(hp, mode="synthesize"); print("Graph (ssrn) loaded")

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        ssrn_epoch = restore_latest_model_parameters(sess, hp, 'ssrn')

        print('Run SSRN...')
        Z = synth_mel2mag(hp, mels, g, sess)

        for i, mag in enumerate(Z):
            print("Working on %s"%(bases[i]))
            mag = mag[:lengths[i]*hp.r,:]  ### trim to generated length             
            wav = spectrogram2wav(hp, mag)
            soundfile.write(outdir + "/%s.wav"%(base), wav, hp.sr) 
Example #15
Source File: libaudio.py    From magphase with Apache License 2.0 6 votes vote down vote up
def write_audio_file(filepath, v_signal, fs, norm=0.98):
    '''
    norm: If None, no normalisation is applied. If it is a float number,
          it is the target value (absolute) for the normalisation.
    '''
    
    # Normalisation:
    if norm is not None:
        v_signal = norm * v_signal / np.max(np.abs(v_signal)) # default
        
    # Write:    
    sf.write(filepath, v_signal, fs)
    
    return

#------------------------------------------------------------------------------
# data_type: 'magnitude', 'phase' or 'zeros' (for zero padding), 'complex' 
Example #16
Source File: test_iterable_dataset.py    From espnet with Apache License 2.0 6 votes vote down vote up
def pipe_wav(tmp_path):
    p = tmp_path / "wav.scp"
    soundfile.write(
        tmp_path / "a.wav",
        np.random.randint(-100, 100, (160000,), dtype=np.int16),
        16000,
    )
    soundfile.write(
        tmp_path / "b.wav",
        np.random.randint(-100, 100, (80000,), dtype=np.int16),
        16000,
    )
    with p.open("w") as f:
        f.write(f"a {tmp_path / 'a.wav'}\n")
        f.write(f"b {tmp_path / 'b.wav'}\n")
    return str(p) 
Example #17
Source File: sound_scp.py    From espnet with Apache License 2.0 6 votes vote down vote up
def __setitem__(self, key: str, value):
        rate, signal = value
        assert isinstance(rate, int), type(rate)
        assert isinstance(signal, np.ndarray), type(signal)
        if signal.ndim not in (1, 2):
            raise RuntimeError(f"Input signal must be 1 or 2 dimension: {signal.ndim}")
        if signal.ndim == 1:
            signal = signal[:, None]

        wav = self.dir / f"{key}.{self.format}"
        wav.parent.mkdir(parents=True, exist_ok=True)
        soundfile.write(str(wav), signal, rate)

        self.fscp.write(f"{key} {wav}\n")

        # Store the file path
        self.data[key] = str(wav) 
Example #18
Source File: ntt_wpe.py    From nara_wpe with MIT License 5 votes vote down vote up
def main(path_to_pkg, files, output_dir, taps=10, delay=3, iterations=5):
    """
    A small command line wrapper around the NTT-WPE matlab file.
    http://www.kecl.ntt.co.jp/icl/signal/wpe/
    """

    if len(files) > 1:
        signal_list = [
            sf.read(str(file))[0]
            for file in files
        ]
        y = np.stack(signal_list, axis=0)
        sampling_rate = sf.read(str(files[0]))[1]
    else:
        y, sampling_rate = sf.read(files)

    wrapper = NTTWrapper(path_to_pkg)
    x = wrapper(y, delay, iterations, taps,
                sampling_rate, stft_size=512, stft_shift=128
                )

    if len(files) > 1:
        for i, file in enumerate(files):
            sf.write(
                str(Path(output_dir) / Path(file).name),
                x[i],
                samplerate=sampling_rate
            )
    else:
        sf.write(
            str(Path(output_dir) / Path(files).name),
            x,
            samplerate=sampling_rate
        ) 
Example #19
Source File: test_iterable_dataset.py    From espnet with Apache License 2.0 5 votes vote down vote up
def csv_float(tmp_path):
    p = tmp_path / "shape.txt"
    with p.open("w") as f:
        f.write("a 1.4,3.4\n")
        f.write("b 0.9,9.3\n")
    return str(p) 
Example #20
Source File: test_iterable_dataset.py    From espnet with Apache License 2.0 5 votes vote down vote up
def text_float(tmp_path):
    p = tmp_path / "shape.txt"
    with p.open("w") as f:
        f.write("a 1.4 3.4\n")
        f.write("b 0.9 9.3\n")
    return str(p) 
Example #21
Source File: io_utils.py    From adviser with GNU General Public License v3.0 5 votes vote down vote up
def create_dataset(self, name, shape=None, data=None, **kwds):
        f = io.BytesIO()
        array, rate = data
        soundfile.write(f, array, rate, format=self.format)
        self.file.create_dataset(name, shape=shape,
                                 data=np.void(f.getvalue()), **kwds) 
Example #22
Source File: clip.py    From superboucle with GNU General Public License v3.0 5 votes vote down vote up
def writeData(self, clip, channel, offset, data):
        if clip.audio_file is None:
            raise Exception("No audio buffer available")

        if offset + data.shape[0] > self.length(clip):
            data = data[0:data.shape[0] - 2]
            # raise Exception(("attempt to write data outside of buffer"
            #                 ": %s + %s > %s ")
            #                % (offset, data.shape[0], self.length(clip)))

        self.data[clip.audio_file][offset:offset + data.shape[0],
        channel] = data
        # print("Write %s bytes at offset %s to channel %s" % (data.shape[0],
        # offset,
        # channel)) 
Example #23
Source File: test_iterable_dataset.py    From espnet with Apache License 2.0 5 votes vote down vote up
def text(tmp_path):
    p = tmp_path / "text"
    with p.open("w") as f:
        f.write("a hello world\n")
        f.write("b foo bar\n")
    return str(p) 
Example #24
Source File: test_iterable_dataset.py    From espnet with Apache License 2.0 5 votes vote down vote up
def shape_file(tmp_path):
    p = tmp_path / "shape.txt"
    with p.open("w") as f:
        f.write("a 100,80\n")
        f.write("b 150,80\n")
    return str(p) 
Example #25
Source File: cli_writers.py    From adviser with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        self.writer[key] = value
        if self.writer_nframe is not None:
            self.writer_nframe.write(f'{key} {len(value)}\n') 
Example #26
Source File: cli_writers.py    From adviser with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        self.writer.create_dataset(key, data=value, **self.kwargs)

        if self.writer_scp is not None:
            self.writer_scp.write(f'{key} {self.filename}:{key}\n')
        if self.writer_nframe is not None:
            self.writer_nframe.write(f'{key} {len(value)}\n') 
Example #27
Source File: cli_writers.py    From adviser with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        assert_scipy_wav_style(value)
        # Change Tuple[int, ndarray] -> Tuple[ndarray, int]
        # (scipy style -> soundfile style)
        value = (value[1], value[0])
        self.writer.create_dataset(key, data=value)

        if self.writer_scp is not None:
            self.writer_scp.write(f'{key} {self.filename}:{key}\n')
        if self.writer_nframe is not None:
            self.writer_nframe.write(f'{key} {len(value[0])}\n') 
Example #28
Source File: io.py    From sms_wsj with MIT License 5 votes vote down vote up
def dump_audio(obj, file, samplerate=8000, mkdir=True, normalize=True):
    if normalize:
        # Correction, because the allowed values are in the range [-1, 1).
        # => "1" is not a vaild value
        correction = (2**15 - 1) / (2**15)
        obj = obj * (correction / np.amax(np.abs(obj)))

    if isinstance(file, Path):
        file = str(file)
    try:
        soundfile.write(
            file=file,
            data=obj,
            samplerate=samplerate,
        )
    except RuntimeError:
        if mkdir:
            # Assume mkdir is rarely nessesary, hence first try write
            Path(file).parent.mkdir(
                parents=True,
                exist_ok=True,  # Allow concurrent mkdir
            )
            soundfile.write(
                file=file,
                data=obj,
                samplerate=samplerate,
            )
        else:
            raise 
Example #29
Source File: cli_writers.py    From espnet with Apache License 2.0 5 votes vote down vote up
def __setitem__(self, key, value):
        assert_scipy_wav_style(value)
        # Change Tuple[int, ndarray] -> Tuple[ndarray, int]
        # (scipy style -> soundfile style)
        value = (value[1], value[0])
        self.writer.create_dataset(key, data=value)

        if self.writer_scp is not None:
            self.writer_scp.write(f"{key} {self.filename}:{key}\n")
        if self.writer_nframe is not None:
            self.writer_nframe.write(f"{key} {len(value[0])}\n") 
Example #30
Source File: clip.py    From superboucle with GNU General Public License v3.0 5 votes vote down vote up
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