Esempio n. 1
0
def play(sound: str,
         x: float = MIDDLE,
         y: float = MIDDLE,
         gain: float = 1.0) -> Source:
    """Play a sound at the given position."""
    source = Buffer(sound).play()
    source.spatialize = True
    source.position = x, -y, 0
    source.gain = gain
    return source
Esempio n. 2
0
def test_source_force_stopped(ogg):
    """Test the handling of source force stopped message."""
    with Device() as device, Context(device) as context:
        context.message_handler = mock('source_force_stopped')
        # TODO: test source preempted by a higher-prioritized one
        with Buffer(ogg) as buffer:
            source = buffer.play()
        context.message_handler.source_force_stopped.assert_called_with(source)
        with SourceGroup() as group, Buffer(ogg) as buffer:
            source.group = group
            buffer.play(source)
            group.stop_all()
        context.message_handler.source_force_stopped.assert_called_with(source)
        source.destroy()
Esempio n. 3
0
def play(device: str, waveform: str, duration: float,
         frequency: float) -> None:
    """Play waveform at the given frequency for given duration."""
    with Device(device) as dev, Context(dev):
        print('Opened', dev.name)
        dec = ToneGenerator(waveform, duration, frequency)
        print(f'Playing {waveform} signal at {frequency} Hz for {duration} s')
        with Buffer.from_decoder(dec, 'tonegen') as buf, buf.play():
            sleep(duration)
Esempio n. 4
0
def test_source_stopped(wav):
    """Test the handling of source stopped message."""
    with Device() as device, Context(device) as context, Buffer(wav) as buffer:
        context.message_handler = mock('source_stopped')
        with buffer.play() as source:
            while source.playing:
                pass
            context.update()
            context.message_handler.source_stopped.assert_called_with(source)
Esempio n. 5
0
def test_buffer_loading(aiff):
    """Test the handling of buffer loading message."""
    with Device() as device, Context(device) as context:
        context.message_handler = mock('buffer_loading')
        with Buffer(aiff), aifc.open(aiff, 'r') as f:
            args, kwargs = context.message_handler.buffer_loading.call_args
            name, channel_config, sample_type, sample_rate, data = args
            assert name == aiff
            assert channel_config == channel_configs[f.getnchannels() - 1]
            assert sample_type == sample_types[f.getsampwidth() - 1]
            assert sample_rate == f.getframerate()
Esempio n. 6
0
def play(files: Iterable[str], device: str) -> None:
    """Load and play files on the given device."""
    with Device(device) as dev, Context(dev) as ctx:
        print('Opened', dev.name)
        ctx.message_handler = EventHandler()
        for filename in files:
            try:
                buffer = Buffer(filename)
            except RuntimeError:
                stderr.write(f'Failed to open file: {filename}\n')
                continue
            with buffer:
                src = buffer.play()
                while src.playing:
                    print(f' {pretty_time(src.offset_seconds)} /'
                          f' {pretty_time(buffer.length_seconds)}',
                          end='\r', flush=True)
                    sleep(PERIOD)
                print()
                ctx.update()
Esempio n. 7
0
def test_offset(context, ogg):
    """Test read-write property offset."""
    with Buffer(ogg) as buffer, buffer.play() as source:
        assert source.offset == 0
        length = buffer.length
        source.offset = length >> 1
        assert source.offset == length >> 1
        with raises(RuntimeError):
            source.offset = length
        with raises(OverflowError):
            source.offset = -1
Esempio n. 8
0
def play(files: Iterable[str], device: str) -> None:
    """Load and play files on the given device."""
    with Device(device) as dev, Context(dev):
        print('Opened', dev.name)
        for filename in files:
            try:
                buffer = Buffer(filename)
            except RuntimeError:
                stderr.write(f'Failed to open file: {filename}\n')
                continue
            with buffer, buffer.play() as src:
                print(f'Playing {filename} ({buffer.sample_type},',
                      f'{buffer.channel_config}, {buffer.frequency} Hz)')
                while src.playing:
                    print(
                        f' {pretty_time(src.offset_seconds)} /'
                        f' {pretty_time(buffer.length_seconds)}',
                        end='\r',
                        flush=True)
                    sleep(PERIOD)
                print()
Esempio n. 9
0
def test_control(context, flac):
    """Test calling control methods."""
    with Buffer(flac) as buffer, buffer.play() as source:
        assert source.playing
        assert not source.paused
        source.pause()
        assert source.paused
        assert not source.playing
        source.resume()
        assert source.playing
        assert not source.paused
        source.stop()
        assert not source.playing
        assert not source.paused
        with raises(AttributeError):
            source.playing = True
        with raises(AttributeError):
            source.paused = True
Esempio n. 10
0
def test_buffer_loading(mp3):
    """Test implication of context during buffer loading."""
    with Device() as device, Context(device):
        with Buffer(mp3): pass
    with raises(RuntimeError):
        with Buffer(mp3): pass
Esempio n. 11
0
def test_fade_out_to_stop(context, mp3):
    """Test calling method fade_out_to_stop."""
    with Buffer(mp3) as buffer, buffer.play() as source:
        source.fade_out_to_stop(5 / 7, buffer.length >> 1)
        with raises(ValueError):
            source.fade_out_to_stop(0.42, -1)
Esempio n. 12
0
def test_latency_seconds(context, mp3):
    """Test read-only property latency_seconds."""
    with Buffer(mp3) as buffer, buffer.play() as source:
        assert isinstance(source.latency_seconds, float)
        with raises(AttributeError):
            source.latency_seconds = buffer.length_seconds / 2
Esempio n. 13
0
def test_latency(context, aiff):
    """Test read-only property latency."""
    with Buffer(aiff) as buffer, buffer.play() as source:
        assert isinstance(source.latency, int)
        with raises(AttributeError):
            source.latency = 42
Esempio n. 14
0
def test_offset_seconds(context, flac):
    """Test read-only property offset_seconds."""
    with Buffer(flac) as buffer, buffer.play() as source:
        assert isinstance(source.offset_seconds, float)
        with raises(AttributeError):
            source.offset_seconds = buffer.length_seconds / 2