If you know the sampling frequency of your signal and data is already scaled appropriately by max(abs(data))
then you can do it very easily using scipy:
from __future__
import print_function
import scipy.io.wavfile as wavf
import numpy as np
if __name__ == "__main__":
samples = np.random.randn(44100)
fs = 44100
out_f = 'out.wav'
wavf.write(out_f, fs, samples)
To write multiple-channels, use a 2-D array of shape (Nsamples, Nchannels).,Writes a simple uncompressed WAV file.,The sample rate (in samples/sec).,The bits-per-sample and PCM/float will be determined by the data-type.
>>> from scipy.io.wavfile
import write
>>>
samplerate = 44100;
fs = 100 >>>
t = np.linspace(0., 1., samplerate) >>>
amplitude = np.iinfo(np.int16).max >>>
data = amplitude * np.sin(2. * np.pi * fs * t) >>>
write("example.wav", samplerate, data.astype(np.int16))
I have audio data recorded from microphone like this : (ndarray of float),But when I play the audio it become broken, nothing but just noise… how to convert it into .wav audio file?,and maybe also convert your data to integers since I think wav files uses only integers values,replace your waveFile.writeframeswith this
> print(data)[-0.00762939 - 0.00817871 - 0.00714111...0.0265511 0.02484207 0.02377392]
while (recording):
frames.append(data)
waveFile = wave.open(WAVE_OUTPUT_FILENAME + "_" + str(COUNT_FILE) + ".wav", 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.wr(b ''.join(frames))
waveFile.close()
data = struct.pack('h' * len(frames), * frames)
waveFile.writeframes(data)
wavio.write writes a numpy array to a WAV file, optionally using a specified sample width.,wavio.read reads a WAV file and returns an object that holds the sampling rate, sample width (in bytes), and a numpy array containing the data.,The following code (also found in the docstring of wavio.write) writes a three second 440 Hz sine wave to a 24-bit WAV file:,A Python module for reading and writing WAV files using numpy arrays.
The following code (also found in the docstring of wavio.write) writes a three second 440 Hz sine wave to a 24-bit WAV file:
import numpy as np import wavio rate = 22050 # samples per second T = 3 # sample duration(seconds) f = 440.0 # sound frequency(Hz) t = np.linspace(0, T, T * rate, endpoint = False) x = np.sin(2 * np.pi * f * t) wavio.write("sine24.wav", x, rate, sampwidth = 3)
Last Updated : 24 Jan, 2021,GATE CS 2021 Syllabus
First We Need To Install ffmpeg. It Is A Free Open Source Software Project Consist of a Large Suite Of Libraries And Programs For Handling Video, Audio, And Other Multimedia Files.
sudo apt - get install ffmpeg
So First Let Us Install pydub. This is an Audio Manipulation Module. Python Provides a Module Called pydub to Work With Audio Files. pydub is a Python library to work with only .wav files.
sudo apt - get install - y python - pydub