Python calls the sound card to record sound

Source: Internet
Author: User

# _*_ Coding:utf-8 _*_
__author__ = ' Pythonwu '
__date__ = "2018/7/19 17:39"

Import Pyaudio
Import Wave #使用wave模式

CHUNK = 1024x768 #缓存区设置 1024 bytes
format = pyaudio.paint16 # Quantitative formatting of sampled values
CHANNELS = 2
Rate = 44100
Record_seconds = 5
Wave_output_filename = "Output.wav"
p = Pyaudio. Pyaudio ()
stream = P.open (Format=format,
Channels=channels,
Rate=rate,
Input=true,
Frames_per_buffer=chunk)
Print ("* recording")
frames = []
For I in range (0, int (rate/chunk * record_seconds)):
data = Stream.read (CHUNK)
Frames.append (data)
Print ("* Done recording")
Stream.stop_stream ()
Stream.Close ()
P.terminate ()
WF = Wave.open (wave_output_filename, ' WB ')
Wf.setnchannels (CHANNELS)
Wf.setsampwidth (P.get_sample_size (FORMAT))
Wf.setframerate (Rate)
Wf.writeframes (b '. Join (frames))
Wf.close ()

Python calls the sound card to record sound

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.