Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18198 > unrolled thread

Generating sin/square waves sound

Started byPaulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt>
First post2011-12-30 07:17 +0000
Last post2012-01-02 18:09 +0000
Articles 11 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Generating sin/square waves sound Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-30 07:17 +0000
    Re: Generating sin/square waves sound 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-30 01:51 -0800
    Re: Generating sin/square waves sound Dave Angel <d@davea.name> - 2011-12-30 05:05 -0500
      Re: Generating sin/square waves sound Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2012-01-02 07:24 +0000
        Re: Generating sin/square waves sound Dave Angel <d@davea.name> - 2012-01-02 12:12 -0500
    Re: Generating sin/square waves sound mblume <foobar@invalid.invalid> - 2011-12-30 11:23 +0000
      Re: Generating sin/square waves sound Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2012-01-02 07:31 +0000
        Re: Generating sin/square waves sound Peter Billam <peter@www.pjb.com.au> - 2012-01-02 10:05 +0000
    Re: Generating sin/square waves sound K Richard Pixley <rich@noir.com> - 2011-12-30 17:19 -0800
      Re: Generating sin/square waves sound Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2012-01-02 07:34 +0000
    Re: Generating sin/square waves sound Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2012-01-02 18:09 +0000

#18198 — Generating sin/square waves sound

FromPaulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt>
Date2011-12-30 07:17 +0000
SubjectGenerating sin/square waves sound
Message-ID<jdjohn$ttr$1@speranza.aioe.org>
Hi,
Sorry if this is a FAQ, but I have googled and didn't find any
satisfatory answer.

Is there a simple way, preferably multiplataform (or linux), of
generating sinusoidal/square waves sound in python?

Thanks for any answers/suggestions.

[toc] | [next] | [standalone]


#18200

From88888 Dihedral <dihedral88888@googlemail.com>
Date2011-12-30 01:51 -0800
Message-ID<15235180.160.1325238666252.JavaMail.geo-discussion-forums@prlk31>
In reply to#18198
Please check PYGAME and Simple Directmedia library. 

Python is used as the director like role and functions in SDL 
do most of the jobs in Pygame. 

[toc] | [prev] | [next] | [standalone]


#18201

FromDave Angel <d@davea.name>
Date2011-12-30 05:05 -0500
Message-ID<mailman.4239.1325239510.27778.python-list@python.org>
In reply to#18198
On 12/30/2011 02:17 AM, Paulo da Silva wrote:
> Hi,
> Sorry if this is a FAQ, but I have googled and didn't find any
> satisfatory answer.
>
> Is there a simple way, preferably multiplataform (or linux), of
> generating sinusoidal/square waves sound in python?
>
> Thanks for any answers/suggestions.
If you're willing to be Linux-only, then I believe you can do it without 
any extra libraries.

You build up a string (8 bit char, on Python 2.x)  of samples, and write 
it to  "/dev/audio".  When i experimented, I was only interested in a 
few seconds, so a single write was all I needed.

Note that the samples are 8 bits, and they are offset by 128.  So a zero 
signal would be a string of 128 values.  A very quiet square wave might 
be a bunch of 126, followed by a bunch of 130.  and so on.  And the 
loudest might be a bunch of 2's followed by a bunch of 253's.

You'll have to experiment with data rate;   The data is sent out at a 
constant rate from your string, but I don't know what that rate is.


-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#18314

FromPaulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt>
Date2012-01-02 07:24 +0000
Message-ID<jdrm2g$le0$1@speranza.aioe.org>
In reply to#18201
Em 30-12-2011 10:05, Dave Angel escreveu:
> On 12/30/2011 02:17 AM, Paulo da Silva wrote:
>> Hi,
>> Sorry if this is a FAQ, but I have googled and didn't find any
>> satisfatory answer.
>>
>> Is there a simple way, preferably multiplataform (or linux), of
>> generating sinusoidal/square waves sound in python?
>>
>> Thanks for any answers/suggestions.
> If you're willing to be Linux-only, then I believe you can do it without
> any extra libraries.
> 
> You build up a string (8 bit char, on Python 2.x)  of samples, and write
> it to  "/dev/audio".  When i experimented, I was only interested in a
> few seconds, so a single write was all I needed.
> 
> Note that the samples are 8 bits, and they are offset by 128.  So a zero
> signal would be a string of 128 values.  A very quiet square wave might
> be a bunch of 126, followed by a bunch of 130.  and so on.  And the
> loudest might be a bunch of 2's followed by a bunch of 253's.
> 
> You'll have to experiment with data rate;   The data is sent out at a
> constant rate from your string, but I don't know what that rate is.
> 
> 
This sounds nice, but then is 8 bits the limit for /dev/audio? What
about stereo? I don't need this one ... just for curiosity.
Thanks.

[toc] | [prev] | [next] | [standalone]


#18333

FromDave Angel <d@davea.name>
Date2012-01-02 12:12 -0500
Message-ID<mailman.4315.1325524362.27778.python-list@python.org>
In reply to#18314
On 01/02/2012 02:24 AM, Paulo da Silva wrote:
> Em 30-12-2011 10:05, Dave Angel escreveu:
>> On 12/30/2011 02:17 AM, Paulo da Silva wrote:
>>> Hi,
>>> Sorry if this is a FAQ, but I have googled and didn't find any
>>> satisfatory answer.
>>>
>>> Is there a simple way, preferably multiplataform (or linux), of
>>> generating sinusoidal/square waves sound in python?
>>>
>>> Thanks for any answers/suggestions.
>> If you're willing to be Linux-only, then I believe you can do it without
>> any extra libraries.
>>
>> You build up a string (8 bit char, on Python 2.x)  of samples, and write
>> it to  "/dev/audio".  When i experimented, I was only interested in a
>> few seconds, so a single write was all I needed.
>>
>> Note that the samples are 8 bits, and they are offset by 128.  So a zero
>> signal would be a string of 128 values.  A very quiet square wave might
>> be a bunch of 126, followed by a bunch of 130.  and so on.  And the
>> loudest might be a bunch of 2's followed by a bunch of 253's.
>>
>> You'll have to experiment with data rate;   The data is sent out at a
>> constant rate from your string, but I don't know what that rate is.
>>
>>
> This sounds nice, but then is 8 bits the limit for /dev/audio? What
> about stereo? I don't need this one ... just for curiosity.
> Thanks.
I don't even remember where I got the information, but it sufficed for 
my needs.  I wanted to be able to generate arbitrary tones at a high 
volume, bypassing any volume control system settings.  I assume there's 
some way (ioctl ?) to tell the device to interpret differently.  It's 
even possible that it's already stereo (left/right samples stored in 
adjacent 16bit locations -- it wouldn't change the effect I hear, except 
for the frequency being cut in half.

Interesting link from1991, it's from Guido, but doesn't mention Python
http://wiretap.area.com/Gopher/Library/Techdoc/Misc/audiofor.faq
Also, a lot has changed since then.

Now I wonder whether I should be sending u-law values, rather than 
linear ones.  I'm generating sine waves, and they don't really sound 
flutelike.

This link probably has all our answers.
http://www.faqs.org/docs/Linux-HOWTO/Sound-HOWTO.html

I suspect that my approach will simply use the latest setup values (eg. 
sampling rate) and thus runs the risk of sometimes not working.  That 
hasn't been my experience,  The only problem I see is that sometimes the 
device is "busy".  When that happens, other programs don't seem to work 
either.  I need to find the rogue process which is hogging the device, 
using fuser.

-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#18204

Frommblume <foobar@invalid.invalid>
Date2011-12-30 11:23 +0000
Message-ID<jdk706$2qi$1@news.albasani.net>
In reply to#18198
Am Fri, 30 Dec 2011 07:17:13 +0000 schrieb Paulo da Silva:

> Hi,
> Sorry if this is a FAQ, but I have googled and didn't find any
> satisfatory answer.
> 
> Is there a simple way, preferably multiplataform (or linux), of
> generating sinusoidal/square waves sound in python?
> 
> Thanks for any answers/suggestions.

Have a look at the wave module, available under Windows and Linux,
which operates on .WAV files. The following snippet might get you going:

#!/usr/bin/python

import math, wave, struct



def signal(t, freq):

	return math.sin(2.0*math.pi*freq*t)

		

		

wout      = wave.open("sample.wav", "wb")

nchan     = 1

sampwidth = 2

framerate = 8000

nframes   = 7 * framerate

comptype  = "NONE"

compname  = "no compression"		





wout.setparams((nchan, sampwidth, framerate, nframes, comptype, compname))



ts = 1.0 / framerate

t  = 0.0

n  = 0

data = []

vals = []

while n < nframes:

	vals.append(signal(t, 517.0))

	n = n + 1

	t = t + ts

	

mx   = max((abs(x) for x in vals))

vals = [ x/mx for x in vals ]	

data = ""

for v in vals:

	data = data + struct.pack("<h", int(v*32766.0))

wout.writeframes(data)

wout.close()


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.

HTH
Martin

[toc] | [prev] | [next] | [standalone]


#18315

FromPaulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt>
Date2012-01-02 07:31 +0000
Message-ID<jdrmg0$m62$2@speranza.aioe.org>
In reply to#18204
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.
> 
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.
Anyway I'll take a look at the wave module.
Thanks

[toc] | [prev] | [next] | [standalone]


#18318

FromPeter Billam <peter@www.pjb.com.au>
Date2012-01-02 10:05 +0000
Message-ID<slrnjg30bn.55d.peter@box8.pjb.com.au>
In reply to#18315
On 2012-01-02, Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> 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    www.pjb.com.au    www.pjb.com.au/comp/contact.html

[toc] | [prev] | [next] | [standalone]


#18240

FromK Richard Pixley <rich@noir.com>
Date2011-12-30 17:19 -0800
Message-ID<fqtLq.44455$8O1.547@newsfe07.iad>
In reply to#18198
On 12/29/11 23:17 , Paulo da Silva wrote:
> Hi,
> Sorry if this is a FAQ, but I have googled and didn't find any
> satisfatory answer.
>
> Is there a simple way, preferably multiplataform (or linux), of
> generating sinusoidal/square waves sound in python?
>
> Thanks for any answers/suggestions.

I just posted on this elsewhere.  Look for a thread titled: "Which 
library for audio playback ?"

--rich

[toc] | [prev] | [next] | [standalone]


#18316

FromPaulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt>
Date2012-01-02 07:34 +0000
Message-ID<jdrmlv$mk8$1@speranza.aioe.org>
In reply to#18240
Em 31-12-2011 01:19, K Richard Pixley escreveu:
> On 12/29/11 23:17 , Paulo da Silva wrote:
>> Hi,
>> Sorry if this is a FAQ, but I have googled and didn't find any
>> satisfatory answer.
>>
>> Is there a simple way, preferably multiplataform (or linux), of
>> generating sinusoidal/square waves sound in python?
>>
>> Thanks for any answers/suggestions.
> 
> I just posted on this elsewhere.  Look for a thread titled: "Which
> library for audio playback ?"
> 
> --rich

Thank you. I have just seen it using google and saved the bookmark of
the link. It's too late now but I'll read it tomorrow.

[toc] | [prev] | [next] | [standalone]


#18335

FromPaulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt>
Date2012-01-02 18:09 +0000
Message-ID<jdsrs0$fju$1@speranza.aioe.org>
In reply to#18198
Em 30-12-2011 07:17, Paulo da Silva escreveu:
> Hi,
> Sorry if this is a FAQ, but I have googled and didn't find any
> satisfatory answer.
> 
> Is there a simple way, preferably multiplataform (or linux), of
> generating sinusoidal/square waves sound in python?
> 
> Thanks for any answers/suggestions.

Thank you very much to all who responded.
There is enough material here to spend some time searching for the best
solution for me.
In the meanwhile I also found http://www.speech.kth.se/snack.
May be it could also be useful.

Thank you.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web