Discussion:
[Tutor] How to generate a pure tones and random noise using Python?
Paul Z
2015-07-25 11:15:31 UTC
Permalink
Hi All,

I try to train my listening by using python. (estimating the frequency of sound)
So... Are there some way to generate a fixed frequency sound in different waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random noise. (eg. white noise & pink noise) ?

I have search in web, some people say that I can use winsound which can generate a fixed frequency beep. However, It is Windows only. (I'm under Linux) and I think It is not a professional library to generate audio signal.

How about pygame? and some examples?
any ideas?

Thanks

Paul Z
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-07-25 20:35:24 UTC
Permalink
In a message of Sat, 25 Jul 2015 19:15:31 +0800, Paul Z writes:
>Hi All,
>
>I try to train my listening by using python. (estimating the frequency of sound)
>So... Are there some way to generate a fixed frequency sound in different waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random noise. (eg. white noise & pink noise) ?
>
>I have search in web, some people say that I can use winsound which can generate a fixed frequency beep. However, It is Windows only. (I'm under Linux) and I think It is not a professional library to generate audio signal.
>
>How about pygame? and some examples?
>any ideas?
>
>Thanks
>
>Paul Z
>_______________________________________________
>Tutor maillist - ***@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor

I think you want this:
https://zach.se/generate-audio-with-python/
https://github.com/zacharydenton/wavebender

pygame will not give you want you want.

blender might. I am not sure but worth googling for.

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Paul Z
2015-07-26 11:29:19 UTC
Permalink
Hi Laura,

Thanks for you helpful reply, and it is exactly what I want.

I'm going to learn Python from write a ears training program. ;-)
And Is there have a library to scope(print) this wave form? (real-time and non real-time)

I have already started to learn "generate audio with python", Thank you. :-)

Paul Z

----------------------------------------
> To: ***@whoosh.cn
> CC: ***@python.org; ***@openend.se
> From: ***@openend.se
> Subject: Re: [Tutor] How to generate a pure tones and random noise using Python?
> Date: Sat, 25 Jul 2015 22:35:24 +0200
>
> In a message of Sat, 25 Jul 2015 19:15:31 +0800, Paul Z writes:
>>Hi All,
>>
>>I try to train my listening by using python. (estimating the frequency of sound)
>>So... Are there some way to generate a fixed frequency sound in different waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random noise. (eg. white noise & pink noise) ?
>>
>>I have search in web, some people say that I can use winsound which can generate a fixed frequency beep. However, It is Windows only. (I'm under Linux) and I think It is not a professional library to generate audio signal.
>>
>>How about pygame? and some examples?
>>any ideas?
>>
>>Thanks
>>
>>Paul Z
>>_______________________________________________
>>Tutor maillist - ***@python.org
>>To unsubscribe or change subscription options:
>>https://mail.python.org/mailman/listinfo/tutor
>
> I think you want this:
> https://zach.se/generate-audio-with-python/
> https://github.com/zacharydenton/wavebender
>
> pygame will not give you want you want.
>
> blender might. I am not sure but worth googling for.
>
> Laura
>

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Paul Z
2015-07-26 16:10:29 UTC
Permalink
Hi Laura,

I just have read this webpage about Generate Audio with Python.
It seem writing by Python 2? Esp. itertools (izip, imap).
I try to change izip and imap to zip and map, but I get error:

beatfreq, amplitude = remainder.split('/')
ValueError: need more than 1 value to unpack

Are there some way to conver it to the code of Python 3?

I'm too anxious to hear the sound from Python though I'm a absolute beginner.

Many thanks

Paul Z

----------------------------------------
> From: ***@whoosh.cn
> To: ***@openend.se
> Date: Sun, 26 Jul 2015 19:29:19 +0800
> Subject: Re: [Tutor] How to generate a pure tones and random noise using Python?
> CC: ***@python.org
>
> Hi Laura,
>
> Thanks for you helpful reply, and it is exactly what I want.
>
> I'm going to learn Python from write a ears training program. ;-)
> And Is there have a library to scope(print) this wave form? (real-time and non real-time)
>
> I have already started to learn "generate audio with python", Thank you. :-)
>
> Paul Z
>
> ----------------------------------------
>> To: ***@whoosh.cn
>> CC: ***@python.org; ***@openend.se
>> From: ***@openend.se
>> Subject: Re: [Tutor] How to generate a pure tones and random noise using Python?
>> Date: Sat, 25 Jul 2015 22:35:24 +0200
>>
>> In a message of Sat, 25 Jul 2015 19:15:31 +0800, Paul Z writes:
>>>Hi All,
>>>
>>>I try to train my listening by using python. (estimating the frequency of sound)
>>>So... Are there some way to generate a fixed frequency sound in different waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random noise. (eg. white noise & pink noise) ?
>>>
>>>I have search in web, some people say that I can use winsound which can generate a fixed frequency beep. However, It is Windows only. (I'm under Linux) and I think It is not a professional library to generate audio signal.
>>>
>>>How about pygame? and some examples?
>>>any ideas?
>>>
>>>Thanks
>>>
>>>Paul Z
>>>_______________________________________________
>>>Tutor maillist - ***@python.org
>>>To unsubscribe or change subscription options:
>>>https://mail.python.org/mailman/listinfo/tutor
>>
>> I think you want this:
>> https://zach.se/generate-audio-with-python/
>> https://github.com/zacharydenton/wavebender
>>
>> pygame will not give you want you want.
>>
>> blender might. I am not sure but worth googling for.
>>
>> Laura
>>
>
> _______________________________________________
> Tutor maillist - ***@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Pablo Lucena
2015-07-26 21:32:57 UTC
Permalink
>
>
> I just have read this webpage about Generate Audio with Python.
> It seem writing by Python 2? Esp. itertools (izip, imap).
> I try to change izip and imap to zip and map, but I get error:
>
> ​​
> beatfreq, amplitude = remainder.split('/')
> ​​
> ValueError: need more than 1 value to unpack
>
> Are there some way to conver it to the code of Python 3?
>
> I'm too anxious to hear the sound from Python though I'm a absolute
> beginner.
>
> Many thanks
>
> Paul Z
>
>
​If you are using Python 3, replace calls to itertools.izip and
itertools.imap with simply "zip" and "map" respectively. Why not just use
Pyton2 instead, as there may be other things that will break.

​beatfreq, amplitude =
> ​​
> remainder.split('/')


​This throwing an exception means there is an issue with the data you are
trying to split, not necessarily a Py2 vs Py3 thing. What string is
assigned `remainder`?

--
*Pablo Lucena*
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.o
Alan Gauld
2015-07-26 23:48:02 UTC
Permalink
On 25/07/15 12:15, Paul Z wrote:
> Are there some way to generate a fixed frequency sound in different
> waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and
> different random noise. (eg. white noise & pink noise) ?

While there are several options in Python (check out the
Activestate recipes) the simplest way on Linux may be to
create a file with the relevant signal and then play that
through aplay using the subprocess module.

One such module(by B Walker) that I found on Activestate
is this (slightly tweaked):

def sinebeep():
header=[ 82, 73, 70, 70, 100, 31, 0, 0, 87, 65, 86, 69,
102, 109, 116, 32, 16, 0, 0, 0, 1, 0, 1, 0,
64, 31, 0, 0, 64, 31, 0, 0, 1, 0, 8, 0, 100,
97, 116, 97, 64, 31, 0, 0 ]
waveform=[ 79, 45, 32, 45, 79, 113, 126, 113 ]
wavefile=open("beep.wav", "w+")

for hdr in header:
wavefile.write(chr(hdr))
for sample in range(0, 1000, 1):
for wf in waveform:
wavefile.write(chr(wf))

wavefile.close()

if __name__ == '__main__':
sinebeep()

You can then play the resultant file with

$ aplay beep.wav #you might need sudo

Its not very clever but you could create a series of beep
files each with a different frequency.

PS.
There is also a command line program called beep that does
what you want but it didn't work for me... YMMV


HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...