コード例 #1
0
def read_wav_file(wav_file):
    # Try to read file with wavio
    try:
        return wavio.read(wav_file)
    # May have failed due to wav being floating-point encoded
    except WavError as e:
        pass
    except Exception as e:
        print("\tERR: Couldn't read data: {}".format(e))
        return None

    # Use scipy.io.wavfile as fallback
    try:
        rate, data = wavfile.read(wav_file)
        return wavio.Wav(data, rate, dtype_to_sampwidth(data.dtype))
    except Exception as e:
        print("\tERR: Couldn't read data: {}".format(e))
        return None
コード例 #2
0
def load_audio(file: Any) -> np.ndarray:
    """
    Args:
        file: Audio filename or file object
    Returns:
        samples: Sample rate and audio samples from 0..1
    """
    import wavio
    try:
        wav = wavio.read(file)
    except EOFError:
        wav = wavio.Wav(np.array([[]], dtype=np.int16), 16000, 2)
    if wav.data.dtype != np.int16:
        raise InvalidAudio('Unsupported data type: ' + str(wav.data.dtype))
    if wav.rate != pr.sample_rate:
        raise InvalidAudio('Unsupported sample rate: ' + str(wav.rate))

    data = np.squeeze(wav.data)
    return data.astype(np.float32) / float(np.iinfo(data.dtype).max)
コード例 #3
0
import os # This lets us deal with filesystems (to read in wav files)
from scipy.signal import spectrogram


# In[5]:


# Grab all of the files in the "Wav" directory
allFiles = os.listdir("Wav/")

# Now we only take the files which have a the extension ".wav"
wavFiles = ["Wav/" + str(i) for i in allFiles if i[-3:len(i)] == "wav"]
wavFiles.sort()

# Now our array of data, which is blank at first (empty Wav objects)
readData = [wavio.Wav(0, 0, 0) for i in range(len(wavFiles))]
# Read in the data
for i in range(len(wavFiles)):
    readData[i] = wavio.read(wavFiles[i])
    
# Create a dictionary
# This means that we access each individual set of data using the file name instead
# of an index like a regular array
wavData = dict(zip(wavFiles, readData))

# And we also want to read in the background noise
background = wavio.read("background.wav")
avgBackground = np.average(background.data.flatten())


# In[10]: