Save an array of numpy to a wav file using the wave module

What do you want to do

I want to save the sound waveform data held by numpy to a file. However, I want to use only standard modules, not scikits.audiolab.

Clogged up and solution

Wave_write class parameters

In Wave_write class documentation The fourth number of frames among the parameters passed by setparams is "The number of frames will change when frames are written later." It says, but setting it to 0 didn't work.

How to make a binary

How to make a binary that represents the waveform to pass to writeframes of wave. It didn't work with numpy.tostring (), and after converting it to'h' with array, tostring worked fine.

code

import numpy, wave, array
filename = "wave_file_name.wav"

# save wav file
buf = <Contains numpy waveform data>
w = wave.Wave_write(filename)
w.setparams((
    1,                        # channel
    2,                        # byte width
    16000,                    # sampling rate
    len(buf),                 # number of frames
    "NONE", "not compressed"  # no compression
))
w.writeframes(array.array('h', buf).tostring())
w.close()

Recommended Posts

Save an array of numpy to a wav file using the wave module
[Python] You can save an object to a file by using the pickle module.
Create a 2D array by adding a row to the end of an empty array with numpy
Save the object to a file with pickle
I want to add silence to the beginning of a wav file for 1 second
How to save the feature point information of an image in a file and use it for matching
A magic word (?) That may save people who are addicted to building using the Intel compiler of Python + Numpy.
I want to judge the authenticity of the elements of numpy array
How to paste a CSV file into an Excel file using Pandas
Process Splunk execution results using Python and save to a file
Conditional branch due to the existence of a shell script file
How to output the output result of the Linux man command to a file
How to save only a part of a long video using OpenCV
Output search results of posts to a file using Mattermost API
I want to extract the tag information (title and artist) of a music file (flac, wav).
[Fabric] I was addicted to using boolean as an argument, so make a note of the countermeasures.
Python / numpy> list (numpy array) file save / load
Create a graph using the Sympy module
How to put a line number at the beginning of a CSV file
[Python] How to read a csv file (read_csv method of pandas module)
Edit wav files using Python's Wave module
I made a program to check the size of a file in Python
The story of making a tool to load an image with Python ⇒ save it as another name
How to sort by specifying a column in the Python Numpy array.
I made a function to see the movement of a two-dimensional array (Python)
Various ways to read the last line of a csv file in Python
Save the setting conditions as a CSV file using UDF Manager in OCTA
Attempt to launch another .exe and save the console output to a text file
I want to write an element to a file with numpy and check it.
[python] How to sort by the Nth Mth element of a multidimensional array
I made an appdo command to execute a command in the context of the app
[Numpy, scipy] How to calculate the square root of a semi-fixed definite matrix
I want to see a list of WebDAV files in the Requests module
An introduction to data analysis using Python-To increase the number of video views-
I made a script to record the active window using win32gui of Python
How to calculate the volatility of a brand
Add rows to an empty array with numpy
The shape of the one-dimensional array of numpy was complicated
[Python numpy] Dynamically specify the index of the array
[Python] A rough understanding of the logging module
Output in the form of a python array
Try using the collections module (ChainMap) of python3
How to uninstall a module installed using setup.py
An easy way to measure the processing speed of a disk recognized by Linux
How to display the modification date of a file in C language up to nanoseconds
Use Pandas to write only the specified lines of the data frame to an excel file
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
[Ansible] Example of playbook that adds a character string to the first line of the file
Every time I try to read a csv file using pandas, I get a numpy error.
How to avoid the cut-off label of the graph created by the plot module using matplotlib
I tried to extract the text in the image file using Tesseract of the OCR engine
I want to take a screenshot of the site on Docker using any font
A memo of misunderstanding when trying to load the entire self-made module with Python3
Save the text of all Evernote notes to SQLite using Beautiful Soup and SQLAlchemy
Don't take an instance of a Python exception class directly as an argument to the exception class!
Pass an array from PHP to PYTHON and do numpy processing to get the result
Various ways to extract columns in a NumPy array
How to write a GUI using the maya command
Template of python script to read the contents of the file
Get the file name in a folder using glob
Created a module to monitor file and URL updates