Record with Python → Save file (sounddevice + wave)

Introduction

There is a scene to record in Chapter 2 of the book Sound Source Separation Learned with Python, but I'll make a note of it because it's a little jammed.

environment

sounddevice requires pip install sounddevice. wave is built-in and is not needed.

macOS 10.15.6 Python 3.7.8 sounddevice 0.4.1

Record + save

import numpy as np
import sounddevice as sd
import wave


FILE_NAME = './test.wav'  #File name to save
wave_length = 2  #Recording length (seconds)
sample_rate = 16_000  #Sampling frequency

#Start recording (wave_length Record for seconds. Wait until the recording is finished with wait)
data = sd.rec(int(wave_length * sample_rate), sample_rate, channels=1)
sd.wait()

#Normalize. Since it is recorded with 16 bits of quantization bit, it is maximized in the range of int16.
data = data / data.max() * np.iinfo(np.int16).max

# float -> int
data = data.astype(np.int16)

#Save file
with wave.open(FILE_NAME, mode='wb') as wb:
    wb.setnchannels(1)  #monaural
    wb.setsampwidth(2)  # 16bit=2byte
    wb.setframerate(sample_rate)
    wb.writeframes(data.tobytes())  #Convert to byte string

I forgot to convert it to an integer type, and whatever I recorded, it sounded like white noise. We hope for your reference.

reference

Recommended Posts

Record with Python → Save file (sounddevice + wave)
Draw netCDF file with python
Download csv file with python
Extract the xz file with python
[Python] Write to csv file with Python
Save the binary file in Python
[Automation with python! ] Part 1: Setting file
Output to csv file with Python
Create an Excel file with Python3
[Automation with python! ] Part 2: File operation
Send image with python, save with php
Creating a simple PowerPoint file with Python
Exclusive control with lock file in Python
Read CSV file with python (Download & parse CSV file)
Save video frame by frame with Python OpenCV
Check the existence of the file with python
Quickly create an excel file with Python #python
Python / numpy> list (numpy array) file save / load
Save / load in-memory DB with python sqlite3
Let's read the RINEX file with Python ①
Create Excel file with Python + similarity matrix
I made a configuration file with Python
[Automation] Read mail (msg file) with Python
Split mol2 file with python (-> 2016.04.17 Also supports sdf file)
nginxparser: Try parsing nginx config file with Python
Script python file
How to read a CSV file with Python 2/3
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
[Python] How to read excel file with pandas
Convert svg file to png / ico with Python
Twilio with Python
Read table data in PDF file with Python
Integrate with Python
Play with 2016-Python
Python file processing
AES256 with python
Tested with Python
Develop Windows apps with Python 3 + Tkinter (exe file)
python starts with ()
with syntax (Python)
[Python] Summary of S3 file operations with boto3
Bingo with python
Zundokokiyoshi with python
Extract zip with Python (Japanese file name support)
Create a Photoshop format file (.psd) with python
Convert the character code of the file with Python3
Wav file generation from numeric text with python
Read line by line from a file with Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)
Excel with Python
Microcomputer with Python
Cast with python
How to measure mp3 file playback time with python
Read QR code from image file with Python (Mac)
How to convert JSON file to CSV file with Python Pandas
Read json file with Python, format it, and output json