當前位置: 首頁>>代碼示例>>Python>>正文


Python wavfile.read方法代碼示例

本文整理匯總了Python中scipy.io.wavfile.read方法的典型用法代碼示例。如果您正苦於以下問題:Python wavfile.read方法的具體用法?Python wavfile.read怎麽用?Python wavfile.read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在scipy.io.wavfile的用法示例。


在下文中一共展示了wavfile.read方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: wavefile_to_waveform

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
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 
開發者ID:jordipons,項目名稱:sklearn-audio-transfer-learning,代碼行數:20,代碼來源:utils.py

示例2: plotstft

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="gray", channel=0, name='tmp.png', alpha=1, offset=0):
    samplerate, samples = wav.read(audiopath)
    samples = samples[:, channel]
    s = stft(samples, binsize)

    sshow, freq = logscale_spec(s, factor=1, sr=samplerate, alpha=alpha)
    sshow = sshow[2:, :]
    ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel
    timebins, freqbins = np.shape(ims)
    
    ims = np.transpose(ims)
    # ims = ims[0:256, offset:offset+768] # 0-11khz, ~9s interval
    ims = ims[0:256, :] # 0-11khz, ~10s interval
    #print "ims.shape", ims.shape
    
    image = Image.fromarray(ims) 
    image = image.convert('L')
    image.save(name) 
開發者ID:YerevaNN,項目名稱:Spoken-language-identification,代碼行數:20,代碼來源:create_spectrograms.py

示例3: read_file

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def read_file(filename):
    """
    Read wave file as mono.

    Args:
        - filename (str) : wave file / path.

    Returns:
        tuple of sampling rate and audio data.
    """
    fs, sig = read(filename=filename)
    if (sig.ndim == 1):
        samples = sig
    else:
        samples = sig[:, 0]
    return fs, samples 
開發者ID:SuperKogito,項目名稱:pydiogment,代碼行數:18,代碼來源:io.py

示例4: triangle

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def triangle(t, randfunc=np.random.rand, t0_fac=None): # ramp up then down
    height = (0.4 * randfunc() + 0.4) * np.random.choice([-1,1])
    width = randfunc()/4 * t[-1]     # half-width actually
    t0 = 2*width + 0.4 * randfunc()*t[-1] if t0_fac is None else t0_fac*t[-1]
    x = height * (1 - np.abs(t-t0)/width)
    x[np.where(t < (t0-width))] = 0
    x[np.where(t > (t0+width))] = 0
    amp_n = (0.1*randfunc()+0.02)   # add noise
    return x + amp_n*pinknoise(t.shape[0])


# Prelude to read_audio_file
# Tried lots of ways of doing this.. most are slow.
#signal, rate = librosa.load(filename, sr=sr, mono=True, res_type='kaiser_fast') # Librosa's reader is incredibly slow. do not use
#signal, rate = torchaudio.load(filename)#, normalization=True)   # Torchaudio's reader is pretty fast but normalization is a problem
#signal = signal.numpy().flatten()
#reader = io_methods.AudioIO   # Stylios' file reader. Haven't gotten it working yet
#signal, rate = reader.audioRead(filename, mono=True)
#signal, rate = sf.read('existing_file.wav') 
開發者ID:drscotthawley,項目名稱:signaltrain,代碼行數:21,代碼來源:audio.py

示例5: __init__

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def __init__(self, path, sr=44100, ):
        super(FileEffect, self).__init__()
        print("  FileEffect: path = ",path)
        if (path is None) or (not glob.glob(path+"/Train/target*")) \
            or (not glob.glob(path+"/Val/target*")) or ((not glob.glob(path+"/effect_info.ini"))):
            print(f"Error: can't file target output files or effect_info.ini in path = {path}")
            sys.exit(1)   # Yea, this is fatal

        self.sr = sr
        # read the effect info config file  "effect_info.ini"
        config = configparser.ConfigParser()
        config.read(path+'/effect_info.ini')
        self.name = config['effect']['name']+"(files)"   # tack on "(files)" to the effect name
        #TODO: note that use of 'eval' below could be a potential security issue
        self.knob_names = eval(config.get("effect","knob_names"))
        self.knob_ranges = np.array(eval(config.get("effect","knob_ranges")))
        try:
            self.is_inverse = (True == bool(config['effect']['inverse']) )
            self.name = "De-"+self.name
        except:
            pass   # Ignore errors we don't require that 'inverse' be defined anywhere in the file 
開發者ID:drscotthawley,項目名稱:signaltrain,代碼行數:23,代碼來源:audio.py

示例6: wavread

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def wavread(fn):
    """Emulate the parts of the matlab wavread function that we need.

        y, Fs = wavread(fn)

    y is the vector of audio samples, Fs is the frame rate.

    Matlab's wavread is used by voicesauce to read in the wav files for
    processing.  As a consequence, all the translated algorithms assume the
    data from the wav file is in matlab form, which in this case means a double
    precision float between -1 and 1.  The corresponding scipy function returns
    the actual integer PCM values from the file, which range between -32768 and
    32767.  (matlab's wavread *can* return the integers, but does not by
    default and voicesauce uses the default).  Consequently, after reading the
    data using scipy's io.wavfile, we convert to float by dividing each integer
    by 32768.

    """
    # For reference, I figured this out from:
    # http://mirlab.org/jang/books/audiosignalprocessing/matlab4waveRead.asp?title=4-2%20Reading%20Wave%20Files
    # XXX: if we need to handle 8 bit files we'll need to detect them and
    # special case them here.
    Fs, y = wavfile.read(fn)
    return y/numpy.float64(32768.0), Fs 
開發者ID:voicesauce,項目名稱:opensauce-python,代碼行數:26,代碼來源:helpers_legacy.py

示例7: test_read_1

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def test_read_1():
    for mmap in [False, True]:
        warn_ctx = WarningManager()
        warn_ctx.__enter__()
        try:
            warnings.simplefilter('ignore', wavfile.WavFileWarning)
            rate, data = wavfile.read(datafile('test-44100-le-1ch-4bytes.wav'),
                                      mmap=mmap)
        finally:
            warn_ctx.__exit__()

        assert_equal(rate, 44100)
        assert_(np.issubdtype(data.dtype, np.int32))
        assert_equal(data.shape, (4410,))

        del data 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:18,代碼來源:test_wavfile.py

示例8: fbank

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def fbank(wav_path, flat=True):
    """ Currently grabs log Mel filterbank, deltas and double deltas."""

    (rate, sig) = wav.read(wav_path)
    if len(sig) == 0:
        logger.warning("Empty wav: {}".format(wav_path))
    fbank_feat = python_speech_features.logfbank(sig, rate, nfilt=40)
    energy = extract_energy(rate, sig)
    feat = np.hstack([energy, fbank_feat])
    delta_feat = python_speech_features.delta(feat, 2)
    delta_delta_feat = python_speech_features.delta(delta_feat, 2)
    all_feats = [feat, delta_feat, delta_delta_feat]
    if not flat:
        all_feats = np.array(all_feats)
        # Make time the first dimension for easy length normalization padding
        # later.
        all_feats = np.swapaxes(all_feats, 0, 1)
        all_feats = np.swapaxes(all_feats, 1, 2)
    else:
        all_feats = np.concatenate(all_feats, axis=1)

    # Log Mel Filterbank, with delta, and double delta
    feat_fn = wav_path[:-3] + "fbank.npy"
    np.save(feat_fn, all_feats) 
開發者ID:persephone-tools,項目名稱:persephone,代碼行數:26,代碼來源:feat_extract.py

示例9: detect_MIDI_notes

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def detect_MIDI_notes(self):
        """
            The algorithm for calculating midi notes from a given wav file.
        """

        (framerate, sample) = wav.read(self.wav_file)
        # We need to change the 2 channels into one because STFT works only
        # for 1 channel. We could also do STFT for each channel separately.
        monoChannel = sample.mean(axis=1)
        duration = getDuration(self.wav_file)
        midi_notes = []

        # Consider only files with a duration longer than 0.2 seconds.
        if duration > 0.18:
            frequency_power = self.calculateFFT(duration, framerate, monoChannel)
            filtered_frequencies = [f for (f, p) in frequency_power]
            #self.plot_power_spectrum(frequency_power)
            #self.plot_power_spectrum_dB(frequency_power)
            f0_candidates = self.get_pitch_candidates_remove_highest_peak(frequency_power)
            midi_notes = self.matchWithMIDINotes(f0_candidates)
        return midi_notes 
開發者ID:Agerrr,項目名稱:Automated_Music_Transcription,代碼行數:23,代碼來源:highest_peak_method.py

示例10: get_data

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def get_data():
    """The data files come from the TSP dataset 16k set"""
    _, man = wavfile.read('./wav_data/MA02_04.wav')
    rate, woman = wavfile.read('./wav_data/FA01_03.wav')
    man = man.astype('float32')
    woman = woman.astype('float32')
    man_max = np.max(man)
    woman_max = np.max(woman)
    man /= man_max
    woman /= woman_max
    shortest = min(len(man), len(woman))
    woman = woman[:shortest]
    man = man[:shortest]
    np.random.seed(101)
    noise = np.random.uniform(-1, 1, len(man))
    sources = np.stack((woman, man, noise))
    A = np.random.uniform(-1, 1, (3, 3))
    linear_mix = np.dot(A, sources)
    pnl_mix = linear_mix.copy()
    pnl_mix[0] = np.tanh(pnl_mix[0])
    pnl_mix[1] = (pnl_mix[1] + pnl_mix[1]**3) / 2
    pnl_mix[2] = np.exp(pnl_mix[2])
    return linear_mix, pnl_mix, A, sources 
開發者ID:pbrakel,項目名稱:anica,代碼行數:25,代碼來源:audio.py

示例11: mainParkinson

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def mainParkinson():
	general_feature_list = []
	general_label_list = []
	folder = raw_input('Give the name of the folder that you want to read data: ')
	if(folder == 'PD'):
		healthyCases = os.listdir('/home/gionanide/Theses_2017-2018_2519/Gkagkos/Audio_Files/PD')
		for x in healthyCases:
			wav = '/'+folder+'/'+str(x)
			mfcc_features,inputWav = mfcc_features_extraction(wav)
			mean_features(mfcc_features,inputWav,folder,general_feature_list,general_label_list)
		folder = raw_input('Give the name of the folder that you want to read data: ')
		if(folder == 'HC'):
			parkinsonCases = os.listdir('/home/gionanide/Theses_2017-2018_2519/Gkagkos/Audio_Files/HC')
			for x in parkinsonCases:
				wav = '/'+folder+'/'+str(x)
				mfcc_features,inputWav = mfcc_features_extraction(wav)
				mean_features(mfcc_features,inputWav,folder,general_feature_list,general_label_list)
		#print general_feature_list, general_label_list
		#writeFeatures(general_feature_list,general_label_list,wav,folder)
		classifyPHC(general_feature_list,general_label_list) 
開發者ID:gionanide,項目名稱:Speech_Signal_Processing_and_Classification,代碼行數:22,代碼來源:mfcc_pca_feature.py

示例12: wavfile_to_examples

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def wavfile_to_examples(wav_file):
    """Convenience wrapper around waveform_to_examples() for a common WAV format.

    Args:
      wav_file: String path to a file, or a file-like object. The file
      is assumed to contain WAV audio data with signed 16-bit PCM samples.

    Returns:
      See waveform_to_examples.
    """
    try:
        wav_file = BytesIO(wav_file)
        sr, wav_data = wavfile.read(wav_file)
    except IOError:
        print("Error reading WAV file!")
        print("The specified WAV file type is not supported by scipy.io.wavfile.read()")
        sys.exit(1)

    if wav_data.dtype != np.int16:
        raise TypeError('Bad sample type: %r' % wav_data.dtype)
    samples = wav_data / 32768.0  # Convert to [-1.0, +1.0]
    return waveform_to_examples(samples, sr) 
開發者ID:IBM,項目名稱:MAX-Audio-Classifier,代碼行數:24,代碼來源:vggish_input.py

示例13: load_wav

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def load_wav(input_wav_file):
    # Load the inputs that we're given
    fs, audio = wav.read(input_wav_file)
    assert fs == 16000
    print('source dB', db(audio))
    return audio 
開發者ID:rtaori,項目名稱:Black-Box-Audio,代碼行數:8,代碼來源:run_audio_attack.py

示例14: load

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def load(path):
        bps, data = wav.read(path)
        if len(data.shape) != 1:
            data = data[:,0] + data[:,1]
        return bps, data 
開發者ID:pstuvwx,項目名稱:Deep_VoiceChanger,代碼行數:7,代碼來源:gla_gpu.py

示例15: load

# 需要導入模塊: from scipy.io import wavfile [as 別名]
# 或者: from scipy.io.wavfile import read [as 別名]
def load(path):
    bps, data = wav.read(path)
    if len(data.shape) != 1:
        data = data[:,0] + data[:,1]
    return bps, data 
開發者ID:pstuvwx,項目名稱:Deep_VoiceChanger,代碼行數:7,代碼來源:dataset.py


注:本文中的scipy.io.wavfile.read方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。