def load(self, filepath, start_sample=0, stop_sample=None): x, sr = self._load_source(filepath, start_sample, stop_sample) if self.target_sample_rate != sr: x = samplerate.resample( x.T, self.target_sample_rate / sr, "sinc_fastest" ).T return x
def generate_octaves(octave_count): pygame.mixer.init() # Create a new Sound object sound = pygame.mixer.Sound("res/c.wav") # Create an array and copy sample sound sound_array = pygame.sndarray.array(sound) ratio_dict = { 'C': 1, 'C#': .944, 'D': .891, 'D#': .841, 'E': .794, 'F': .749, 'F#': .707, 'G': .667, 'G#': .63, 'A': .594, 'A#': .561, 'B': .53 } scale_dict = {} for octave in range(1, octave_count + 1): for key, value in ratio_dict.items(): key = key + '-' + str(octave) scale_dict[key] = pygame.sndarray.make_sound( resample(sound_array, value * octave, "sinc_fastest").astype(sound_array.dtype)) return scale_dict
def read_file(self, filepath, start_sample=0, stop_sample=None): if isinstance(filepath, (list, tuple)): start_sample = start_sample \ if isinstance(start_sample, (list, tuple)) \ else len(filepath) * [start_sample] stop_sample = stop_sample \ if isinstance(stop_sample, (list, tuple)) \ else len(filepath) * [stop_sample] return np.concatenate([ self.read_file(filepath_, start_, stop_) for filepath_, start_, stop_ in zip( filepath, start_sample, stop_sample ) ], axis=-1) filepath = str(filepath) x, sr = soundfile.read( filepath, start=start_sample, stop=stop_sample, always_2d=True ) if self.source_sample_rate is not None: assert sr == self.source_sample_rate, (self.source_sample_rate, sr) if self.target_sample_rate != sr: x = samplerate.resample( x, self.target_sample_rate / sr, "sinc_fastest" ) return x.T
def load_reduced_wav(path): rate, wav = wavfile.read(path) # loads as int16 # take first SECONDS and convert to [-1.0, 1.0] wav = _slice_and_rescale(wav, rate) wav_downsampled = samplerate.resample(wav, 1 / DOWN_SAMPLING_FACTOR) rate = rate / DOWN_SAMPLING_FACTOR return rate, wav_downsampled
def resample(self, targetrate=8000, converter_type="sinc_best"): """Use the python bindings for the Secret Rabbit Code library (aka libsamplerate) to perform sample rate conversion. Converts **IN PLACE** https://pypi.org/project/samplerate/ http://www.mega-nerd.com/SRC/index.html """ # From API docs: # src_ratio : Equal to output_sample_rate / input_sample_rate. ratio = targetrate / self.fs self._logger.debug("source: %.2f destination: %.2f ratio %f", self.fs, targetrate, ratio) # We can use the simple API here since we always operate on the whole # audio clip. See http://www.mega-nerd.com/SRC/api_simple.html self.samples = samplerate.resample(self.samples, ratio, converter_type=converter_type) # The number of samples have changed, that is the whole point of # this operation. Get the new values and calculate the new duration, # hopefully that shouldn't change too much. self.nofsamples, self.ch = self.samples.shape self.fs = targetrate self._set_duration()
def main(argv): """ Main routine to calculate SAcC from wav file """ if len(argv) != 3: raise NameError( ("Usage: ", argv[0], " inputsound.wav outputpitchtrack.txt")) inwavfile = argv[1] outptfile = argv[2] # Setup config config = default_config() # Configure sacc_extractor = SAcC(config) # If we were really being consistent, we'd use librosa.load here like we did in intensity_measures.py instead of audioread, # but if it ain't broke. Anyways, I'm not sure if librosa supports python 2 data, srate = audioread(inwavfile) if srate != config['SBF_sr']: data = samplerate.resample(data, config['SBF_sr'] / srate, 'sinc_best') features = sacc_extractor.sacc(data, config['SBF_sr']) # Write the data out np.savetxt(outptfile, features, fmt='%.3f', delimiter=' ', newline='\n')
def playson(tabson, ratio): # resampler pour augmenter la fréquence du son et donc le pitcher resampleson = samplerate.resample(tabson, ratio, "sinc_fastest").astype(tabson.dtype) # jouer le son obtenue sortie = pygame.sndarray.make_sound(resampleson) sortie.play()
def change_ult_frame_rate(self, new_frame_rate): if not self.params['ult_frame_rate_changed']: ratio = new_frame_rate / self.params['ult_fps'] temp = np.apply_along_axis( lambda x: samplerate.resample(x, ratio, 'linear'), 0, self.ult) self.ult = temp.round().astype(int) self.params['ult_fps'] = new_frame_rate self.params['ult_frame_rate_changed'] = True
def resample(self, data: np.ndarray, original_sample_rate: int, new_sample_rate: int) -> None: ratio = new_sample_rate / original_sample_rate if ratio == 1: return data else: return samplerate.resample(data, ratio, 'sinc_best')
def resampling(self, new_sampling_rate): new_df = DataFrame() for i, column in enumerate(self.data.columns): new_df[column] = Series( samplerate.resample(self.data.iloc[:, i], new_sampling_rate / self.sampling_rate, 'sinc_best')) self.sampling_rate = new_sampling_rate self.data = new_df
def resample(self, samplerate, resample_type='sinc_best'): ''' Returns a resampled version of the sound. ''' if not have_resample: raise ImportError('Need samplerate package for resampling') y = np.array(resample(self, float(samplerate / self.samplerate), resample_type), dtype=np.float64) return Sound(y, samplerate=samplerate)
def _normalize(signal, samplerate): # Mix down to a single mono channel. if hasattr(signal, 'ndim') and signal.ndim > 1: print " mix to mono" signal = signal.mean(axis=1).astype(np.float) # Resample down to 22050 Hz. if samplerate > 22050.0: print " downsample to 22050 Hz" signal = resample(signal, 22050.0 / samplerate, 'sinc_fastest') samplerate = 22050.0 return signal, samplerate
def record(self, signals, fs): ''' This simulates the recording of the signals by the microphones and the transformation from sound to light. Parameters ---------- signals: An ndarray with as many lines as there are microphones. fs: the sampling frequency of the signals. ''' # running average of power pwr = np.diff(signals) ** 2 #pwr = signals ** 2 if fs != self.fs: ratio = self.fs / fs # libsamplerate is limited to ratio in [1/256, 256] it seems # proceed in multiple stages counter = 0 while ratio < (1 / 256): ratio *= 2 counter += 1 signals = pwr.T signals = samplerate.resample(signals, ratio, 'sinc_best') for c in range(counter): signals = samplerate.resample(signals, 0.5, 'sinc_best') self.signals = signals.T else: self.signals = pwr self.signals = 10 * np.log10(np.abs(self.signals))
def resampleAudio(f): tuning = f[2] splt = f[1].split('/')[-2:] makeDir(os.path.join('tuned_audio', f[0], splt[0])) source = os.path.join('original_audio', f[0], splt[0], splt[1]) target = os.path.join('tuned_audio', f[0], splt[0], splt[1][:-3] + 'wav') a, sr = load( source, res_type='kaiser_fast') # TODO: write load function without librosa if tuning != 0: a = resample(a, tuning, 'sinc_fastest') soundfile.write(target, a, sr)
def resample(self, data: np.ndarray, original_sample_rate: int, target_sample_rate: int) -> np.ndarray: # If the ratio is 1 then we don't do anything cause new/old = 1, just return the input data if target_sample_rate == original_sample_rate: return data # Use libsamplerate for resampling the audio otherwise return samplerate.resample(data, ratio=(target_sample_rate / original_sample_rate), converter_type='sinc_fastest')
def resample_dset(X, npts, fourier): """Consistent API for resampling force curves with Fourier or interpolation""" X = np.atleast_2d(X) if fourier: ratio = npts / len(X[0]) trend = np.copy(X[:, [0]]) # Should be 2D X = X - trend return np.squeeze( np.stack([resample(x, ratio, "sinc_fastest") for x in X]) + trend) else: tnew = np.linspace(0, 1, npts, endpoint=False) told = np.linspace(0, 1, X.shape[-1], endpoint=False) return np.stack([np.interp(tnew, told, x) for x in X]).squeeze()
def resample(x, ratio, method=None): """ The common resampling function. Each module can use their own resampling function, but this one will default to the best available option. If `samplerate <https://github.com/tuxu/python-samplerate>`_ is installed, it will be used. Otherwise, the :py:func:`scipy.signal.resample` function is used. :param x: The input sound as a numpy array. :param ratio: The ratio of new frequency / old frequency. :param method: 'scipy', 'samplerate', 'samplerate-fast'. Defaults to 'samplerate' if the module is available, and 'scipy' otherwise. """ if method is None: method = DEFAULT_RESAMPLING_METHOD if method == 'samplerate': return samplerate.resample(x, ratio, 'sinc_best') elif method == 'samplerate-fast': return samplerate.resample(x, ratio, 'sinc_fastest') elif method == 'scipy': return sg.resample(x, int(y.shape[0] * ratio))
def stretch(a2d): p = parameters() try: newSize = int(len(resample(a2d[0,:],p.stretchFactor,'sinc_best'))) out = np.zeros([a2d.shape[0],newSize]) for i in range(a2d.shape[0]): # out[i,:] = resample(a2d[i,:],newSize) out[i,:] = resample(a2d[i,:],p.stretchFactor,'sinc_best') except: newSize = int(len(resample(a2d[:],p.stretchFactor,'sinc_best'))) out = np.zeros([1,newSize]) for i in range(a2d.shape[0]): # out[i,:] = resample(a2d[i,:],newSize) out = resample(a2d[:],p.stretchFactor,'sinc_best') return(out)
def callback(outdata, frames, time, status): nonlocal position frames_to_read = math.ceil(frames / self.tempo) data = self.audio_file.read(frames_to_read, fill_value=0) * self.volume data = sr.resample(data, self.tempo, 'sinc_best')[:self.blocksize] self.buffer[:data.shape[0], :] = data outdata[:] = self.buffer self.loudness = np.average(np.abs(self.buffer))
def resample_segment(self, segment): """ Resample the incoming audio stream to match 16kHz. :param segment: segment of the acoustic signal :return: 1D numpy array of the resampled signal """ segment = samplerate.resample(segment, 1 / self.downsampling_factor, 'sinc_fastest') if len(segment) < len(self.window_function): idx = len(self.window_function) - len(segment) tmp = np.zeros((len(self.window_function), 1)) tmp[idx:] = segment segment = tmp return segment.flatten()
def sound_processing(): global audio_process audio_process = True while audio_process: item = q_in.get() wav = data_in[:, 0] wav = normalize_wave_minmax(wav) wav = pre_emphasize(wav, 0.95) pwav = torch.FloatTensor(wav).view(1, 1, -1) g_wav, g_c = segan.generate(pwav) g_wav = denormalize_wave_minmax(g_wav) data_temp[:, 0] = g_wav audio = (data_temp.astype('float32') - offset) / abs_max audio = samplerate.resample(audio, ratio_out, 'sinc_best') if q_out.empty() and audio_process: q_out.put(audio) data_out[:] = audio
def record(self, signals, fs): '''定义接口,给仿真程序调用,仿真程序会麦克模拟声音,并保存在self.signal''' if fs != self.fs: try: import samplerate fs_ratio = self.fs / float(fs) newL = int(fs_ratio * signals.shape[1]) - 1 self.signals = np.zeros((self.M, newL)) # samplerate resample function considers columns as channels (hence the transpose) for m in range(self.M): self.signals[m] = samplerate.resample( signals[m], fs_ratio, 'sinc_best') except ImportError: return ImportError("To Be Done") else: self.signals = signals
def center_wave(wav_fn, vol_range=0, displacement=0, shift=0, p_transform=0): if wav_fn == "silence": bg_wave = random.choice(bg_waves) bg_start = np.random.randint(len(bg_wave) - maxlen) wave = bg_wave[bg_start:bg_start + maxlen] else: wave = soph_scaler(wav.read(wav_fn)[1]) if np.random.rand() < shift: wave = samplerate.resample(wave, np.random.randint(14400, 17600) / 16000, "sinc_fastest") # wave = librosa.core.resample(wave, # orig_sr=16000, # target_sr=np.random.randint(14400,17600), # res_type='kaiser_fast') if len(wave) > 16000: wave_start = np.random.randint(len(wave) - maxlen) wave = wave[wave_start:wave_start + maxlen] elif len(wave) < 16000: left_pad = (maxlen - wave.shape[0]) // 2 right_pad = maxlen - wave.shape[0] - left_pad wave = np.pad(wave, (left_pad, right_pad), 'constant', constant_values=0) if vol_range > 0: if np.random.rand() < p_transform: bg_vol = vol_range * np.random.rand(1) wave = (1 - bg_vol) * wave + bg_vol * \ center_wave("silence", maxlen) wave = np.clip(wave, -1, 1) return wave
def load_audio(self, path): """ :param path:source path :return: samplerate, signal """ signal, sr = sf.read(path,always_2d=True) if signal.ndim > 1: signal = np.mean(signal, axis=1) ratio = self.standart_sr / sr if sr > self.standart_sr: converter = 'sinc_best' reasample_signal = samplerate.resample(signal, ratio, converter) elif sr < self.standart_sr: assert False, "sample rate must be greater or equal then target_sr" else: reasample_signal = signal return int(sr * ratio), reasample_signal
def nread(data, samples, sample_rate, shuffle, aug=None): # a faster implementation of normalize read with augmentation if isinstance(data, str): aud, sr = sf.read(data) elif isinstance(data, np.ndarray): aud = data.copy() sr = sample_rate else: raise Exception( 'Wrong read format! Only supporting filename/numpy.ndarray.') # reduce to mono channel if aud.ndim > 1: aud = aud.mean(axis=1) # augmentation if aug: aud = aug(aud, sr) # resample if sr != sample_rate: aud = resample(aud, sample_rate / sr) _size = aud.shape[0] # crop if _size > samples: idx = np.random.randint(_size - samples) if shuffle else (_size - samples) // 2 aud = aud[idx:(idx + samples)] # pad if _size < samples: pad_left = np.random.randint(samples - _size) if shuffle else (samples - _size) // 2 pad_right = samples - _size - pad_left aud = np.pad(aud, (pad_left, pad_right), 'constant') # normalize normalize_factor = max(1e-6, np.max(np.abs(aud))) aud /= normalize_factor return aud
def record(self, signals, fs): ''' This simulates the recording of the signals by the microphones. In particular, if the microphones and the room simulation do not use the same sampling frequency, down/up-sampling is done here. Parameters ---------- signals: An ndarray with as many lines as there are microphones. fs: the sampling frequency of the signals. ''' if signals.shape[0] != self.M: raise NameError( 'The signals array should have as many lines as there are microphones.' ) if signals.ndim != 2: raise NameError('The signals should be a 2D array.') if fs != self.fs: try: import samplerate fs_ratio = self.fs / float(fs) newL = int(fs_ratio * signals.shape[1]) - 1 self.signals = np.zeros((self.M, newL)) # samplerate resample function considers columns as channels (hence the transpose) for m in range(self.M): self.signals[m] = samplerate.resample( signals[m], fs_ratio, 'sinc_best') except ImportError: raise ImportError( 'The samplerate package must be installed for resampling of the signals.' ) else: self.signals = signals
def resample_records(records, fs, verbose=False): """ Resample records at the desired sampling rate. @param records: Array of wfdb recordings. @param fs: The desired sampling rate. @param verbose: Whether to show a progress bar. @return: Array of resampled signals. """ if verbose: records = tqdm(records, desc='Resampling records') signals = [] for record in records: signal = record.p_signal if fs != record.fs: fs_ratio = fs / record.fs signal = samplerate.resample(signal, fs_ratio) signals.append(signal) return signals
def read_audio(self, example): if example['audio_length'] > 30.: raise FilterException audio_path = example["audio_path"] start_sample = 0 if "audio_start_samples" in example: start_sample = example["audio_start_samples"] stop_sample = None if "audio_stop_samples" in example: stop_sample = example["audio_stop_samples"] audio, sr = soundfile.read(audio_path, start=start_sample, stop=stop_sample, always_2d=True) assert sr == self.input_sample_rate if self.target_sample_rate != sr: audio = samplerate.resample(audio, self.target_sample_rate / sr, "sinc_fastest") audio = audio.T example["audio_data"] = audio return example
def setWaveData(self, waveData, info): if info.samplerate != self.parent().sampleRate: #ratio is output/input waveData = samplerate.resample( waveData, self.parent().sampleRate / info.samplerate, self.parent().sampleRateConversion) if info.channels == 1: waveData = waveData.repeat(2, axis=1) / 2 elif info.channels == 2: pass elif info.channels == 3: front = waveData[:, [0, 1]] / 1.5 center = waveData[:, [2]].repeat(2, axis=1) / 2 waveData = front + center elif info.channels == 4: front = waveData[:, [0, 1]] / 2 rear = waveData[:, [2, 3]] / 2 waveData = front + rear elif info.channels == 5: front = waveData[:, [0, 1]] / 2.5 rear = waveData[:, [2, 3]] / 2.5 center = waveData[:, [4]].repeat(2, axis=1) / 2 waveData = front + rear + center elif info.channels == 6: front = waveData[:, [0, 1]] / 3 rear = waveData[:, [2, 3]] / 3 center = waveData[:, [4]].repeat(2, axis=1) / 2 sub = waveData[:, [5]].repeate(2, axis=1) / 2 waveData = front + rear + center + sub if self.parent().sampleSize == 16: waveData = (waveData * 32767).astype('int16') self.waveData = waveData self.byteArray.clear() self.byteArray.append(waveData.tostring()) self.open(QtCore.QIODevice.ReadOnly)
################################# # Create/Read the sound signals # # The calibration signal is a random binary sequence T_calib = 10. # seconds calib_seq = np.random.choice([-1., 1.], size=int(T_calib * fs)) # Now import the speech source_signals = [] for fn, pwr in zip(source_files, source_powers): fs_local, sig = wavfile.read(fn) sig = sig.astype(np.float) sig *= np.sqrt(pwr) / np.std(sig) if fs_local != fs: sig = samplerate.resample(sig, fs / fs_local, 'sinc_best') source_signals.append(sig) ##################### # Setup of the Room # # Create the room (target RT60 is 0.5 s) room = pra.ShoeBox( [9.9, 7.5, 3.1], fs=fs, absorption=0.25, max_order=25, sigma2_awgn=1e-5, ) # Read in the pyramic microphone locations
def resample(frames, ratio): new = samplerate.resample(frames, ratio, 'sinc_best') return numpy.array(new, dtype='float64')
def load_reduced_wav(path): rate, wav = wavfile.read(path) # loads as int16 # take first SECONDS and convert to [-1.0, 1.0] wav = _slice_and_rescale(wav, rate) wav_downsampled = samplerate.resample(wav, 1 / DOWN_SAMPLING_FACTOR) rate = rate / DOWN_SAMPLING_FACTOR return rate, wav_downsampled def load_wav(path): rate, wav = wavfile.read(path) # loads as int16 wav = _slice_and_rescale(wav, rate) return rate, wav if __name__ == '__main__': import simpleaudio as sa wav_path = '/home/edvard/SharedWithVirtualBox/genres/blues/blues.00001.wav' wav_downsampled = load_reduced_wav(wav_path) wav = samplerate.resample(wav_downsampled, DOWN_SAMPLING_FACTOR) wav = (wav * INT16_MAX).astype(np.int16) print('original size: {}'.format(wav.shape)) print('downsampled size: {}'.format(wav_downsampled.shape)) play_obj = sa.play_buffer(wav, 1, 2, 22050) # rate returned by wavfile.read play_obj.wait_done()