Generating sin/square waves sound

Peter Billam peter at
Mon Jan 2 05:05:43 EST 2012

On 2012-01-02, Paulo da Silva <p_s_d_a_s_i_l_v_a at> wrote:
> Em 30-12-2011 11:23, mblume escreveu:
>> Am Fri, 30 Dec 2011 07:17:13 +0000 schrieb Paulo da Silva:
>> Alternatively you might just generate (t,signal) samples, write
>> them to a file and convert them using "sox" (under Linux, might
>> also be available under Windows) to another format.

That's how I'd do it.
Sox can cope with raw samples, provided you tell it stuff like
the sample-rate, sample-size, channels etc.  E.g. from "man sox"
  sox -r 16k -e signed -b 8 -c 1 voice-memo.raw voice-memo.wav
(Once it's in wav form the wav header contains that information.)

> As much as I could understand at a 1st look you are
> writing to a wav file and then play the file.
> It would be nice if I could play directly the samples.

"-d" as an output-file means the "default" output,
which means it plays to your sound card. E.g.:

  sox -r 48k -e float -b 32 -c 2 input.raw -d

The input file can be "-" so you can pipe directly to it:

  sox -r 44100 -e float -b 32 -c 2 - -d

It should also run under windows and macos, and can generate lots
of formats, see "man soxformat".  It also has a "synth" effect
which can generate simple waveforms easily. Sox is useful.

Regards,  Peter

Peter Billam

More information about the Python-list mailing list