def __init__( self, buffer: SynthizerSoundBuffer, context: synthizer.Context, ) -> None: super().__init__(buffer=buffer) self._context = context self._generator = synthizer.BufferGenerator(self._context) self._generator.buffer = cast(SynthizerSoundBuffer, self._buffer)._buffer self._source = synthizer.Source3D(self._context) self._last_position = 0.0 self._playing = False
def load(self, filename): if self.handle != None: self.close() if isinstance(filename, str): # Asume path on disk. self.generator = synthizer.BufferGenerator(self.context) self.buffer = gsbm.buffer(filename) self.length = self.buffer.get_length_in_seconds() self.generator.buffer = self.buffer if self.type == "3d": self.source = synthizer.Source3D(self.context) elif self.type == "direct": self.source = synthizer.DirectSource(self.context) elif self.type == "panned": self.source = synthizer.PannedSource(self.context) if self.is_active: self.filename = filename return True return False
For best results, use a music file or something that's long enough to play as long as the sleeps at the end of this file.""" import synthizer import time import random import sys import math with synthizer.initialized(log_level=synthizer.LogLevel.DEBUG, logging_backend=synthizer.LoggingBackend.STDERR): # Normal source setup from a CLI arg. ctx = synthizer.Context() gen = synthizer.BufferGenerator(ctx) buffer = synthizer.Buffer.from_file(sys.argv[1]) gen.buffer = buffer src = synthizer.Source3D(ctx) src.add_generator(gen) # create and connect the effect with a default gain of 1.0. echo = synthizer.GlobalEcho(ctx) ctx.config_route(src, echo) # Generate uniformly distributed random taps. # Remember: echo currently only allows up to 5 seconds of delay. n_taps = 100 duration = 2.0 delta = duration / n_taps taps = [ synthizer.EchoTapConfig(delta + i * delta + random.random() * 0.01, random.random(), random.random()) for i in range(n_taps)
with synthizer.initialized(log_level=synthizer.LogLevel.DEBUG, logging_backend=synthizer.LoggingBackend.STDERR): # Get our context, which almost everything requires. # This starts the audio threads. ctx = synthizer.Context() # Enable HRTF as the default panning strategy before making a source ctx.default_panner_strategy = synthizer.PannerStrategy.HRTF # A BufferGenerator plays back a buffer: generator = synthizer.BufferGenerator(ctx) # A buffer holds audio data. We read from the specified file: buffer = synthizer.Buffer.from_file(sys.argv[1]) # Tell the generator to use the buffer. generator.buffer = buffer # A Source3D is a 3D source, as you'd expect. source = synthizer.Source3D(ctx) # It'll play the BufferGenerator. source.add_generator(generator) # Keep track of looping, since property reads are expensive: looping = False # A simple command parser. while True: cmd = input("Command: ") cmd = cmd.split() if len(cmd) == 0: continue if cmd[0] == "pause": source.pause() elif cmd[0] == "play": source.play()