Ejemplo n.º 1
0
class Player(object):
    def __init__(self,
                 freq=None,
                 backend=None,
                 output_device=None,
                 sample_rate=None,
                 block_size=None):
        self.pysoundio = PySoundIo(backend=None)

        self.freq = float(freq)
        self.seconds_offset = 0.0
        self.radians_per_second = self.freq * 2.0 * math.pi
        self.seconds_per_frame = 1.0 / sample_rate

        self.pysoundio.start_output_stream(device_id=output_device,
                                           channels=1,
                                           sample_rate=sample_rate,
                                           block_size=block_size,
                                           dtype=SoundIoFormatFloat32LE,
                                           write_callback=self.callback)

    def close(self):
        self.pysoundio.close()

    def callback(self, data, length):
        indata = ar.array('f', [0] * length)
        for i in range(0, length):
            indata[i] = math.sin(
                (self.seconds_offset + i * self.seconds_per_frame) *
                self.radians_per_second)
        data[:] = indata.tostring()
        self.seconds_offset += self.seconds_per_frame * length
Ejemplo n.º 2
0
class Player(object):
    def __init__(self,
                 infile,
                 backend=None,
                 output_device=None,
                 block_size=None):

        data, rate = sf.read(infile, dtype='float32', always_2d=True)
        self.data = [d[0] for d in data]
        self.block_size = block_size

        self.pysoundio = PySoundIo(backend=None)
        self.pysoundio.start_output_stream(device_id=output_device,
                                           channels=1,
                                           sample_rate=rate,
                                           block_size=self.block_size,
                                           dtype=SoundIoFormatFloat32LE,
                                           write_callback=self.callback)
        self.cb = 0
        self.total_blocks = len(self.data)
        self.timer = self.total_blocks / float(rate)

    def close(self):
        self.pysoundio.close()

    def callback(self, data, length):
        dlen = (self.block_size if self.cb +
                self.block_size <= self.total_blocks else self.total_blocks -
                self.cb)
        data[:] = struct.pack('%sf' % dlen, *self.data[self.cb:self.cb + dlen])
        self.cb += dlen
Ejemplo n.º 3
0
class Player:
    def __init__(self,
                 infile,
                 backend=None,
                 output_device=None,
                 block_size=None):

        data, rate = sf.read(infile, dtype='float32', always_2d=True)
        self.idx = 0
        self.stream = data.tobytes()
        self.block_size = block_size

        self.total_blocks = len(data)
        self.timer = self.total_blocks / float(rate)

        self.num_channels = data.shape[1]
        self.sample_size = data.dtype.itemsize

        self.pysoundio = PySoundIo(backend=backend)
        self.pysoundio.start_output_stream(device_id=output_device,
                                           channels=self.num_channels,
                                           sample_rate=rate,
                                           block_size=self.block_size,
                                           dtype=SoundIoFormatFloat32LE,
                                           write_callback=self.callback)

        print('%s:\n' % infile)
        print(' Channels: %d' % data.shape[1])
        print(' Sample rate: %dHz' % rate)
        print('')

    def close(self):
        self.pysoundio.close()

    def callback(self, data, length):
        num_bytes = length * self.sample_size * self.num_channels
        data[:] = self.stream[self.idx:self.idx + num_bytes]
        self.idx += num_bytes