示例#1
0
class Record:
    def __init__(self,
                 outfile,
                 backend=None,
                 input_device=None,
                 sample_rate=None,
                 block_size=None,
                 channels=None):
        self.wav_file = sf.SoundFile(outfile,
                                     mode='w',
                                     channels=channels,
                                     samplerate=sample_rate)
        self.pysoundio = PySoundIo(backend=backend)
        self.pysoundio.start_input_stream(device_id=input_device,
                                          channels=channels,
                                          sample_rate=sample_rate,
                                          block_size=block_size,
                                          dtype=SoundIoFormatFloat32LE,
                                          read_callback=self.callback)

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

    def callback(self, data, length):
        self.wav_file.buffer_write(data, dtype='float32')
示例#2
0
文件: play.py 项目: m13253/pysoundio
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
示例#3
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
示例#4
0
    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('')
示例#5
0
def main():
    args = get_args()
    pysoundio = PySoundIo(backend=args.backend)
    input_devices, output_devices = pysoundio.list_devices()
    print('\n' + '-' * 20 + ' Input Devices ' + '-' * 20 + '\n')
    print_devices(input_devices)

    print('\n' + '-' * 20 + ' Output Devices ' + '-' * 20 + '\n')
    print_devices(output_devices)

    pysoundio.close()
示例#6
0
 def __init__(self,
              outfile,
              backend=None,
              input_device=None,
              sample_rate=None,
              block_size=None,
              channels=None):
     self.wav_file = sf.SoundFile(outfile,
                                  mode='w',
                                  channels=channels,
                                  samplerate=sample_rate)
     self.pysoundio = PySoundIo(backend=backend)
     self.pysoundio.start_input_stream(device_id=input_device,
                                       channels=channels,
                                       sample_rate=sample_rate,
                                       block_size=block_size,
                                       dtype=SoundIoFormatFloat32LE,
                                       read_callback=self.callback)
示例#7
0
    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)
示例#8
0
文件: play.py 项目: m13253/pysoundio
    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)
示例#9
0
            [str(d) for d in device['formats']['available']])))
        print('\tlayouts: {}'.format(device['layouts']['current']['name']))
        print('\t available: {}'.format(', '.join(
            [str(d['name']) for d in device['layouts']['available']])))
        print('\tsoftware latency:')
        print('\t min: {}s, max: {}s, current: {}s'.format(
            round(device['software_latency_min'], 4),
            round(device['software_latency_max'], 4),
            round(device['software_latency_current'], 4)))
        print("")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='PySoundIo list devices example',
        epilog='List the available input and output devices')
    parser.add_argument('--backend',
                        type=int,
                        help='Backend to use (optional)')
    args = parser.parse_args()

    pysoundio = PySoundIo(backend=args.backend)
    input_devices, output_devices = pysoundio.list_devices()
    print('\n' + '-' * 20 + ' Input Devices ' + '-' * 20 + '\n')
    print_devices(input_devices)

    print('\n' + '-' * 20 + ' Output Devices ' + '-' * 20 + '\n')
    print_devices(output_devices)

    pysoundio.close()