# -*- coding: utf-8 -*- """ Created on Thu Feb 16 11:34:20 2017 @author: lbnc """ import numpy as np import pyaudio import matplotlib.pyplot as plt # sampling frequency fs = 44100 # in Hertz # frequency of tone ft = 440 # in Hertz # duration of sound duration = 2 # in seconds # sampling period dt = 1/fs # in seconds # number of points ns = duration*fs # time ts = np.arange(ns)*dt # setup waveform chA = np.zeros( ns ) chA += np.sin( 2.*np.pi*ft*ts ) plt.plot(ts*1e3, chA) plt.xlim(0,10) plt.xlabel('tid ['+r'$\mu$'+'s]') plt.ylabel('amplitude') # convert to int16 chA = np.int16( ( 2**15 - 10 )*chA/np.amax(np.abs(chA)) ) # instantiate PyAudio (1) p = pyaudio.PyAudio() # open stream (2), 2 is size in bytes of int16 stream = p.open(format=p.get_format_from_width(2), channels=1, rate=fs, output=True) # play stream (3), blocking call stream.write(chA) # stop stream (4) stream.stop_stream() stream.close() # close PyAudio (5) p.terminate()