# -*- 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 # frequency of tone ft = 440 # duration of sound duration = 2 # sampling frequency dt = 1/fs # number of points ns = duration*fs # time ts = np.arange(ns)*dt # amplitudes namps = 10 amps = np.ones(namps) # phase #phase = np.random.uniform(0,1,namps)*np.pi phase = np.zeros( namps ) # setup waveform chA = np.zeros( ns ) for a in range(namps): chA += amps[a]*np.sin( 2.*np.pi*(a+1)*ft*ts + phase[a] ) 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()