This is a reply to the "Stereo audio: specify L-R channel contents
independently?" thread (http://groups.google.com/group/pyglet-users/
browse_thread/thread/3a1a23100d35aa21); I tried to reply to it
directly a couple times last night but found the posts failed to
appear.
Despite making sense, Alex's solution didn't actually eliminate the
clicking I was experiencing, so I abandoned this a long while ago. I
just came across a project that requires I solve this, so I tried
attacking it again.
I found that I reduce about 98% of the clicking noise by creating a
custom _get_audio_data() function where the line "self._offset +=
bytes" is changed to "self._offset += bytes/2". However, if I wear
headphones I can still hear faint clicking sounds. When I add some
print statements to _generate_data() then plot the data it's
generating, I can indeed see occasional misalignment of the sine
waves. I must need to further tweak self._offset but I'm not sure how.
Any suggestions?
(p.s. the while loop in the last 3 lines of the script below appears
to run indefinitely and I'm not sure why exactly; it's as if
player.dispatch_events() chokes when the queued audio is over.)
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, sys, 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 _get_audio_data(self, bytes):
bytes = min(bytes, self._max_offset - self._offset)
if bytes <= 0:
return None
timestamp = float(self._offset) / self._bytes_per_second
duration = float(bytes) / self._bytes_per_second
data = self._generate_data(bytes, self._offset)
self._offset += bytes/2
is_eos = self._offset >= self._max_offset
return AudioData(data,bytes,timestamp,duration)
def _generate_data(self, bytes, offset):
channels = self.audio_format.channels
if self._bytes_per_sample == 1:
start = offset
samples = bytes
bias = 127
amplitude = 127
data = (ctypes.c_ubyte * (samples * channels))()
else:
start = offset >> 1
samples = bytes >> 1
bias = 0
amplitude = 32767
data = (ctypes.c_short * (samples * channels))()
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*2):
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(2,400,410)
player=Player()
player.queue(beep)
player.play()
t=0
while t < 2:
player.dispatch_events()
t=t+tick()
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---