2

Possible Duplicate:
Playing a sound from a wave form stored in an array

I'm trying to generate a vector (or similar data structure) in Python that contains a sine wave and play it without recording anything to disk.

Sort of like the following MATLAB code:

t = 0:1/8000:1;              % Generate a 1 second vector at a sampling rate of 8000 Hz
wave = sin(2*pi*440*t);      % Store a 440 Hz sine wave
sound(wave, 8000);           % Play the waveform

Thanks in advance!

Community
  • 1
  • 1
Tezi Konj
  • 123
  • 1
  • 5

1 Answers1

1

You can use the built-in map function to create the waveform;

import math
t = range(8001)
wave = map(lambda x: math.sin(2 * math.pi * 440 * x), t)

For playing sounds, I'd suggest using ossaudiodev or winsound, depending on your platform.

You would probably have to scale the wave array to a bytearray or a suitable type of numpy array for it to be digestable for the audio devices.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94