#Copyright 2017 Yann Weber
#
#This file is part of SndPipe.
#
#SndPipe is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#any later version.
#
#SndPipe is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with SndPipe. If not, see .
import math
import sys
import struct
BITMAX = lambda bsz: (1< 1 but %d given" % channels)
if len(args) == 0:
raise ValueError("You must give at least one generator")
if len(args) > 1 and channels != len(args):
msg = "If more than one generator given, the count must match the \
channel argument. But %d generator given and channels is %d"
raise ValueError(msg % (len(args), channels))
if len(args) == 1:
if channels == 1:
nxt_sample = lambda waves: [next(waves[0])]
else:
nxt_sample = lambda waves: [next(waves[0])]*2
else:
nxt_sample = lambda waves: map(next, waves)
if bitsz != 8 or signed:
nxt_arg = lambda: nxt_sample(args)
gen = _output_sampler_multibits(nxt_arg, channels, samples, freq, bitsz,
endianess, signed)
while True:
yield next(gen)
raise StopIteration()
return
buff = bytearray([0 for _ in range(samples*channels)])
while True:
for i in range(0,samples*channels, channels):
buff[i:i+channels] = nxt_sample(args)
yield buff
def _output_sampler_multibits(nxt_sample, channels, samples, freq, bitsz,
endianess, signed):
if bitsz not in (8,16,32):
raise ValueError("Bitsize must be in (8,16,32) but %d given" % bitsz)
Bsz = bitsz//8
buff = bytearray([0 for _ in range(samples * channels * Bsz)])
while True:
for i in range(0, samples*channels*Bsz, channels*Bsz):
nxt = nxt_sample()
for j in range(channels):
st = i + (j*Bsz)
buff[st:st+Bsz] = nxt[j].to_bytes(Bsz, endianess,
signed=signed)
yield buff