Pyglet Audio Synthesis

136 views
Skip to first unread message

makemachine

unread,
Nov 16, 2011, 12:41:28 AM11/16/11
to pyglet...@googlegroups.com
Is it possible to use any of the audio capabilities of pyglet to perform real-time audio synthesis? I am not looking to write anything extremely complex, but I would like to be able to generate waveforms ( sine, square, triangle ) in real-time and hopefully perform some simple dsp operations. I have enough knowledge of dsp to write a simple API for these sorts of things, but I'm not sure where to begin with Pyglet or if I should find some other library that provides basic audio buffer/playback capabilities.

Thoughts?

Thanks,
~J

Crispin Wellington

unread,
Nov 16, 2011, 1:14:26 AM11/16/11
to pyglet...@googlegroups.com
Yes. I started a project doing this a long time ago and never really
completed it. It has a pyglet example and some pyrex coded waveform
generators. It's quite feature incomplete, but should give you all
examples you need.

Code is here:

http://code.google.com/p/poise-sfx/

Check out PygletSource.py and test-pyglet.py for the general approach.

Regards

Crispin

> --
> You received this message because you are subscribed to the Google Groups
> "pyglet-users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pyglet-users/-/-yP1ovxcgfAJ.
> To post to this group, send email to pyglet...@googlegroups.com.
> To unsubscribe from this group, send email to
> pyglet-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pyglet-users?hl=en.
>

Adam Bark

unread,
Nov 16, 2011, 6:35:46 AM11/16/11
to pyglet...@googlegroups.com

I'm just using numpy with a modified Source so I can change the sound on
the fly. If you just wanted plain waves you could produce the arrays and
set the StaticSource()._data manually.

HTH,
Adam.

Message has been deleted

makemachine

unread,
Nov 16, 2011, 5:48:48 PM11/16/11
to pyglet...@googlegroups.com
This looks like an interesting start. Was your choice to use Cython because of some test that showed you needed the optimization or is there some other reason? I'll take a closer look at this over the course of the couple of days and see where I can take it. 

Thanks,
~J

makemachine

unread,
Nov 16, 2011, 5:50:35 PM11/16/11
to pyglet...@googlegroups.com
This sounds like a simpler approach. Is there somewhere that I can see an example of what you mean by using a modified source?

Thanks, 
~J

Adam Bark

unread,
Nov 17, 2011, 7:02:23 AM11/17/11
to pyglet...@googlegroups.com

Well the simplest way, as I mentioned before, is just to create a
StaticSource and change the _data attribute ie:

music = pyglet.media.load("/some/file.ogg", streaming=False)
music._data = data_string # data_string is your new audio data

If you want to modify a streamed source on the fly then load your file
and pass it to a new Source class ie:

music = pyglet.media.load("/some/file.ogg")

class MySource(pyglet.media.Source):
def __init__(self, music):
self.music = music
def get_audio_data(self, bytes):
data = self.music.get_audio_data(bytes)
# Do your filtering here
return pyglet.media.AudioData(data, duration...)

As you can see you just need to reimplement get_audio_data really. If
you just want to make your own sounds on the fly just skip the bit about
passing in some music file and reimplement get_audio_data so that it
passes out your sinusoid or whatever wave you like.

HTH,

Adam.

Crispin Wellington

unread,
Nov 17, 2011, 9:29:15 PM11/17/11
to pyglet...@googlegroups.com
Initially I didn't use Cython for the buffer generation. But the
amount of modules you could wrap was quite small before you had drop
out. Cython just increased that number and allowed more things to be
done in the same time delta.

Actually the very first cut was generators that yielded sample values.
But the call overhead was just so great there was no way you could
generate 44100 meaningful values in a second.

Crispin

> --
> You received this message because you are subscribed to the Google Groups
> "pyglet-users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/pyglet-users/-/OldRC8WaL0gJ.

Reply all
Reply to author
Forward
0 new messages