forked from auracaster/pyalsaaudio
isine example: fix stereo handling (#42)
while it's usually not actually necessary to generate a stereo signal (alsa's default plughw device will happily duplicate it for us), we still do it for demo purposes, just because. a more realistic demo would actually use numpy, as that's what the library will most likely be used with, but anyway.
This commit is contained in:
8
isine.py
8
isine.py
@@ -18,12 +18,13 @@ else:
|
|||||||
|
|
||||||
from math import pi, sin, ceil
|
from math import pi, sin, ceil
|
||||||
import struct
|
import struct
|
||||||
|
import itertools
|
||||||
import alsaaudio
|
import alsaaudio
|
||||||
|
|
||||||
sampling_rate = 48000
|
sampling_rate = 48000
|
||||||
|
|
||||||
format = alsaaudio.PCM_FORMAT_S16_LE
|
format = alsaaudio.PCM_FORMAT_S16_LE
|
||||||
framesize = 2 # bytes per frame for the values above
|
pack_format = 'h' # short int, matching S16
|
||||||
channels = 2
|
channels = 2
|
||||||
|
|
||||||
def nearest_frequency(frequency):
|
def nearest_frequency(frequency):
|
||||||
@@ -47,7 +48,10 @@ def generate(frequency, duration = 0.125):
|
|||||||
sine = [ int(32767 * sin(2 * pi * frequency * i / sampling_rate)) \
|
sine = [ int(32767 * sin(2 * pi * frequency * i / sampling_rate)) \
|
||||||
for i in range(size)]
|
for i in range(size)]
|
||||||
|
|
||||||
return struct.pack('%dh' % size, *sine)
|
if channels > 1:
|
||||||
|
sine = list(itertools.chain.from_iterable(itertools.repeat(x, channels) for x in sine))
|
||||||
|
|
||||||
|
return struct.pack(str(size * channels) + pack_format, *sine)
|
||||||
|
|
||||||
|
|
||||||
class SinePlayer(Thread):
|
class SinePlayer(Thread):
|
||||||
|
|||||||
Reference in New Issue
Block a user