Thanks Alex! Modifying the Sine class was a snap once I knew the
channels are interleaved. However, the code below produces some
extraneous static when played, which I'm guessing is because I haven't
figured out how to allocate a larger buffer. Help?
from pyglet.media import Source, AudioFormat, AudioData, Player
from pyglet.media.procedural import ProceduralSource, Sine
from pyglet import media
from pyglet.clock import tick
import ctypes, os, math
class stereo_Sine(ProceduralSource):
def __init__(self, duration, left, right, **kwargs):
super(stereo_Sine, self).__init__(duration, **kwargs)
self.left=left
self.right=right
self.audio_format.channels=2
def _generate_data(self, bytes, offset):
if self._bytes_per_sample == 1:
start = offset
samples = bytes
bias = 127
amplitude = 127.0
data = (ctypes.c_ubyte * samples)()
else:
start = offset >> 1
samples = bytes >> 1
bias = 0
amplitude = 32767.0
data = (ctypes.c_short * samples)()
left_step = self.left * (math.pi * 2) /
self.audio_format.sample_rate
right_step = self.right * (math.pi * 2) /
self.audio_format.sample_rate
j=0
for i in range(samples):
if i%2:
data[i] = int(math.sin(right_step * (j +
start)) * amplitude +
bias)
j=j+1
else:
data[i] = int(math.sin(left_step * (j + start))
* amplitude +
bias)
return data
beep=stereo_Sine(4,400,410)
player=Player()
player.queue(beep)
t=0
while t < 4:
if t==0:
player.play()
tick()
player.dispatch_events()
t=t+tick()
On Jan 9, 7:35 pm, "Alex Holkner" <[EMAIL PROTECTED]> wrote:
> On 1/10/08, Mike Lawrence <[EMAIL PROTECTED]> wrote:
>
>
>
> > Can pyglet create and audio object that has separate right and left
> > channels? I understand that it can playback pre-recorded stereo audio,
> > and if you create, say, a pure tone you can manipulate the 3d space,
> > but I'm trying to create two pure tones, one played to each ear.
>
> > Suggestions?
>
> Modify your tone generator class to create stereo data. The channels
> are interleaved, with the left channel first. So:
>
> [0, 1, 2, 3, 4, ...]
>
> send 0 to left ear, 1 to right ear, 2 to left ear, etc.
>
> Remember to set the channels attribute to 2 on the audio_format object
> and to allocate a buffer twice as large.
>
> Alex.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---