Python-generate a WAV file

Source: Internet
Author: User

1. Download and install scipy

Scipy download link: http://www.scipy.org/Download#head-0dfc04e10313d2e70988c6cb3bef7a9e09860c8f

You can also download instructions link http://docs.scipy.org/doc/

2. Wav file write operations

3. Signal. chirp function instructions

4. click the button to generate the signal. click the button to display the signal.

5. PythonCode

 

#! Usr/bin/ENV Python # code = utf-8from tkinter import * import waveimport numpy as npimport scipy. signal as signalimport matplotlib. pyplot as pltimport syssys. define (1000000) # define the Params of wavechannels = 1 sampwidth = 2 framerate = 9600file_name = 'sweep.wav 'frequency _ begin = coding = 100 # define the time of wavetime = 1def generate_wav (): # generate the time Bart = NP. arange (0, time, 1.0/framerate) # generate the chirp signal from 300 to 3300hzwave_data = signal. chirp (T, frequency_begin, time, frequency_end, method = 'linear ') * 1000 # cast to the type of shortwave_data = wave_data.astype (NP. short) # open a WAV documentf = wave. open (file_name, "WB") # Set WAV paramsf. setnchannels (channels) F. setsampwidth (sampwidth) F. setframerate (framerate) # Turn the data to stringf. writeframes (wave_data.tostring () F. close () def my_button (root, label_text, button_text, button_func): ''' function of Creat label and button ''' # label details label = label (Root) label ['text'] = label_text label. pack () # label details button = button (Root) button ['text'] = button_text button ['command'] = button_func button. pack () def read_wave_data (file_path): # open a wave file, and return a wave_read object F = wave. open (file_path, "rb") # Read the wave's format infomation, and return a tuple Params = f. getparams () # get the info nchannels, sampwidth, framerate, nframes = Params [: 4] # reads and returns nframes of audio, as a string of bytes. str_data = f. readframes (nframes) # Close the stream F. close () # Turn the wave's data to array wave_data = NP. fromstring (str_data, dtype = NP. short) time = NP. arange (0, nframes) * (1.0/framerate) return wave_data, timedef plot_wav (): wave_data, time = read_wave_data (file_name) PLT. plot (time, wave_data) PLT. grid (true) PLT. show () def main (): Root = TK () my_button (root, 'generate a sweep WAV ', 'generate', generate_wav) my_button (root, 'plot the wav ', 'plot', plot_wav) root. mainloop () If _ name _ = "_ main _": Main ()

 

 

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.