### Core API - Audio Output Initialization Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Initializes the audio engine and returns an AudioOutput instance. This should typically be called once at the start of your application. ```APIDOC ## POST /kivy/audiostream/get_output ### Description Initializes the engine and gets an output device. This method can be used only once, usually at the start of your application. ### Method POST ### Endpoint /kivy/audiostream/get_output ### Parameters #### Query Parameters - **rate** (integer) - Optional - Rate of the audio, default to 44100 - **channels** (integer) - Optional - Number of channels, minimum 1, default to 2 - **encoding** (integer) - Optional - Encoding of the audio stream, can be 8 or 16, default to 16 - **buffersize** (integer) - Optional - Size of the output buffer. Tiny buffer will use consume more CPU, but will be more reactive. ### Request Example ```python from audiostream import get_output stream = get_output(channels=2, rate=22050, buffersize=1024) ``` ### Response #### Success Response (200) - **AudioOutput instance** (AudioOutput) - An instance of the AudioOutput class. ``` -------------------------------- ### AudioInput Class Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Abstract class for handling audio input. Provides methods to start, stop, and poll audio data, along with properties for configuration. ```APIDOC ## Class: audiostream.AudioInput ### Description Abstract class for handling an audio input. Normally, the default audio source is the microphone. It will be recorded with a rate of 44100hz, mono, with 16bit PCM. These defaults are the most used and guaranteed to work on Android and iOS. Any other combination might fail. ### Methods #### start() Start the input to gather data from the source. #### stop() Stop the input to gather data from the source. #### poll() Read the internal queue and dispatch the data through the callback. ### Properties #### callback Callback to call when bytes are available on the input, called from the audio thread. The callback must have one parameter for receiving the data. #### encoding (readonly) Encoding of the audio, can be 8 or 16, default to 16. #### source (readonly) Source device to read, default to ‘default. Depending of the platform, you might read other input source. Check the get_input_sources() function. #### channels (readonly) Number of channels, minimum 1, default to 2. #### buffersize (readonly) Size of the input buffer. If <= 0, it will be automatically sized by the system. ``` -------------------------------- ### Initialize Audio Output Stream Source: https://context7.com/kivy/audiostream/llms.txt Initializes the audio output engine. Call this once at the start of your application. Parameters control sample rate, channels, encoding, and buffer size for reactivity. ```python from audiostream import get_output # Initialize audio output with stereo, 22050Hz sample rate, and 1024 byte buffer stream = get_output(channels=2, rate=22050, buffersize=1024) # Parameters: # - rate: Sample rate in Hz (default: 44100) # - channels: Number of audio channels, minimum 1 (default: 2) # - encoding: Bit depth, 8 or 16 (default: 16) # - buffersize: Output buffer size in bytes (smaller = more CPU, more reactive) ``` -------------------------------- ### Initialize Audio Input Stream with Callback Source: https://context7.com/kivy/audiostream/llms.txt Initializes audio input from a microphone. A callback function receives raw audio bytes. Ensure to start, poll regularly, and then stop the input stream. Supports Android, iOS, and macOS. ```python from audiostream import get_input import time frames = [] def mic_callback(buf): """Callback receives raw audio bytes from the input source""" print('Received', len(buf), 'bytes') frames.append(buf) # Initialize microphone input with callback mic = get_input(callback=mic_callback) # Parameters: # - callback: Function called with audio bytes when data is available # - source: Input device name (default: 'default') # - rate: Sample rate in Hz (default: 44100) # - channels: Number of channels, 1 or 2 (default: 1) # - encoding: Bit depth, 8 or 16 (default: 16) # - buffersize: Input buffer size, <= 0 for auto-sizing # Start recording mic.start() # Poll regularly to trigger callbacks (runs callback in calling thread) for _ in range(50): mic.poll() time.sleep(0.1) # Stop recording mic.stop() ``` -------------------------------- ### audiostream.get_output Source: https://github.com/kivy/audiostream/blob/master/docs/source/index.md Initializes the audio engine and returns an `AudioOutput` instance for streaming audio to the speaker. This function should typically be called only once at the application's start. ```APIDOC ## audiostream.get_output ### Description Initializes the engine and get an output device. This method can be used only once, usually at the start of your application. ### Method `audiostream.get_output` ### Parameters #### Query Parameters - **rate** (integer) - Optional - Rate of the audio, default to 44100 - **channels** (integer) - Optional - Number of channels, minimum 1, default to 2 - **encoding** (integer) - Optional - Encoding of the audio stream, can be 8 or 16, default to 16 - **buffersize** (integer) - Optional - Size of the output buffer. Tiny buffer will use consume more CPU, but will be more reactive. ### Return type [`AudioOutput`](#audiostream.AudioOutput) instance ### Request Example ```python from audiostream import get_output stream = get_output(channels=2, rate=22050, buffersize=1024) ``` ``` -------------------------------- ### Core API - Get Input Sources Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Retrieves a list of available audio input sources, which is platform-dependent. Permissions may be required to access certain sources. ```APIDOC ## GET /kivy/audiostream/get_input_sources ### Description Return a list of available input sources. This list is platform-dependent. You might need some additionnals permissions in order to access to the sources. * android: ‘camcorder’, ‘default’, ‘mic’, ‘voice_call’, ‘voice_communication’, ‘voice_downlink’, ‘voice_recognition’, ‘voice_uplink’ * ios: ‘default’ This function currently work only on Android and iOS. ### Method GET ### Endpoint /kivy/audiostream/get_input_sources ### Response #### Success Response (200) - **list of strings** (list) - A list of available input source names. ``` -------------------------------- ### AudioInput Class Source: https://github.com/kivy/audiostream/blob/master/docs/source/index.md Represents an abstract class for handling audio input. It provides methods to start, stop, and poll for audio data, and defines a callback for received data. ```APIDOC ## *class* audiostream.AudioInput(object) ### Description Abstract class for handling an audio input. Normally, the default audio source is the microphone. It will be recorded with a rate of 44100hz, mono, with 16bit PCM. Theses defaults are the most used and guaranted to work on Android and iOS. Any others combination might fail. ### Methods #### start() Start the input to gather data from the source #### stop() Stop the input to gather data from the source #### poll() Read the internal queue and dispatch the data through the callback #### callback Callback to call when bytes are available on the input, called from the audio thread. The callback must have one parameter for receiving the data. ``` -------------------------------- ### Initialize and play an AudioSample Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Demonstrates setting up an audio output stream, adding a sample, and writing raw byte data to the buffer. ```python from audiostream import get_output, AudioSample stream = get_output(channels=1, buffersize=1024, rate=22050) sample = AudioSample() stream.add_sample(sample) sample.play() while True: # audio stuff, this is not accurate. sample.write("\x00\x00\x00\x00\xff\xff\xff\xff") ``` -------------------------------- ### Core API - Audio Input Initialization Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Returns an AudioInput instance for capturing audio. Received data is stored in a queue, and the provided callback is triggered when data is available. ```APIDOC ## POST /kivy/audiostream/get_input ### Description Returns an AudioInput instance. All the data received from the input will be stored in a queue. You need to AudioInput.poll() the queue regularly in order to trigger the callback. Please note that the callback will be called in the same thread as the one that calls AudioInput.poll(). This function currently works only on Android and iOS. ### Method POST ### Endpoint /kivy/audiostream/get_input ### Parameters #### Query Parameters - **callback** (callable) - Required - Callback to call when bytes are available on the input, called from the audio thread. - **source** (string) - Optional - Source device to read, default to ‘default. Depending of the platform, you might read other input source. Check the get_input_sources() function. - **rate** (integer) - Optional - Rate of the audio, default to 44100 - **channels** (integer) - Optional - Number of channels, minimum 1, default to 2 - **encoding** (integer) - Optional - Encoding of the audio stream, can be 8 or 16, default to 16 - **buffersize** (integer) - Optional - Size of the input buffer. If <= 0, it will be automatically sized by the system. ### Request Example ```python from audiostream import get_input def mic_callback(buf): print('got', len(buf)) # get the default audio input (mic on most cases) mic = get_input(callback=mic_callback) mic.start() while not quit: mic.poll() # do something here, like sleep(2) mic.stop() ``` ### Response #### Success Response (200) - **AudioInput instance** (AudioInput) - An instance of the AudioInput class. ``` -------------------------------- ### Initialize Audio Output Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Initializes the audio engine and retrieves an output device instance. This should be called only once at application startup. ```python from audiostream import get_output stream = get_output(channels=2, rate=22050, buffersize=1024) ``` -------------------------------- ### get_output - Initialize Audio Output Stream Source: https://context7.com/kivy/audiostream/llms.txt Creates and initializes the audio output engine. This function should be called once to return an AudioOutput instance. ```APIDOC ## get_output ### Description Creates and initializes the audio output engine. Returns an AudioOutput instance. ### Parameters #### Arguments - **rate** (int) - Optional - Sample rate in Hz (default: 44100) - **channels** (int) - Optional - Number of audio channels, minimum 1 (default: 2) - **encoding** (int) - Optional - Bit depth, 8 or 16 (default: 16) - **buffersize** (int) - Optional - Output buffer size in bytes ### Request Example stream = get_output(channels=2, rate=22050, buffersize=1024) ``` -------------------------------- ### List Available Audio Input Sources Source: https://context7.com/kivy/audiostream/llms.txt Retrieves a list of available audio input devices for the current platform. Some platforms may require specific permissions. You can then use a specific source when initializing `get_input`. ```python from audiostream import get_input_sources # Get available input sources for current platform sources = get_input_sources() print(sources) # Platform-specific sources: # Android: ('camcorder', 'default', 'mic', 'voice_call', # 'voice_communication', 'voice_downlink', # 'voice_recognition', 'voice_uplink') # iOS: ('default',) # macOS: ('default',) # Use a specific source from audiostream import get_input mic = get_input(callback=my_callback, source='mic') ``` -------------------------------- ### get_input - Initialize Audio Input Stream Source: https://context7.com/kivy/audiostream/llms.txt Returns an AudioInput instance for capturing audio from a microphone or other input source. ```APIDOC ## get_input ### Description Initializes microphone input and returns an AudioInput instance for capturing audio data. ### Parameters #### Arguments - **callback** (function) - Required - Function called with audio bytes when data is available - **source** (string) - Optional - Input device name (default: 'default') - **rate** (int) - Optional - Sample rate in Hz (default: 44100) - **channels** (int) - Optional - Number of channels, 1 or 2 (default: 1) - **encoding** (int) - Optional - Bit depth, 8 or 16 (default: 16) - **buffersize** (int) - Optional - Input buffer size, <= 0 for auto-sizing ### Request Example mic = get_input(callback=mic_callback) ``` -------------------------------- ### Add AudioSample to Mixer Source: https://context7.com/kivy/audiostream/llms.txt Creates an `AudioSample` and adds it to the `AudioOutput` mixer for playback. Multiple samples can be added and will be mixed. Call `sample.play()` to begin playback. ```python from audiostream import get_output, AudioSample # Initialize audio output stream = get_output(channels=1, buffersize=1024, rate=22050) # Create a sample and add it to the mixer sample = AudioSample() stream.add_sample(sample) # The sample is now ready for playback sample.play() ``` -------------------------------- ### Generate Square Wave Audio Source: https://context7.com/kivy/audiostream/llms.txt Demonstrates manual generation of a square wave by writing signed 16-bit samples to an output stream. ```python # For 16-bit audio, each sample is 2 bytes (signed short) def generate_square_wave(frequency, duration_ms, rate=22050): samples_per_period = rate // frequency high = struct.pack('h', 16000) # Positive peak low = struct.pack('h', -16000) # Negative peak total_samples = (rate * duration_ms) // 1000 for i in range(total_samples): if (i % samples_per_period) < (samples_per_period // 2): sample.write(high) else: sample.write(low) generate_square_wave(440, 1000) # 440Hz for 1 second # Stop playback when done sample.stop() ``` -------------------------------- ### Play Pure Data Patches with PatchSource Source: https://context7.com/kivy/audiostream/llms.txt Load and play Pure Data patches using PatchSource. Requires the pylibpd library. ```python from audiostream import get_output from audiostream.sources.puredata import PatchSource # Audio configuration CHANNELS = 2 BUFSIZE = 2048 SAMPLERATE = 44100 # Initialize audio output stream = get_output(channels=CHANNELS, buffersize=BUFSIZE, rate=SAMPLERATE) # Load and play a Pure Data patch # The patch file must be in the current directory or provide full path source = PatchSource(stream, 'synthesizer.pd') # Parameters: # - stream: AudioOutput instance # - patchfile: Path to the .pd patch file # Start the patch playback source.start() # Patch runs until stopped import time time.sleep(10) # Stop playback source.stop() ``` -------------------------------- ### Generate Sine Waves with SineSource Source: https://context7.com/kivy/audiostream/llms.txt Use SineSource for continuous sine wave generation with dynamic frequency control. ```python from time import sleep from audiostream import get_output from audiostream.sources.wave import SineSource # Initialize audio output stream = get_output(channels=2, rate=22050, buffersize=128) # Create sine wave source at 220Hz sinsource = SineSource(stream, 220) # Parameters: # - stream: AudioOutput instance # - frequency: Initial frequency in Hz (e.g., 440 for A4 note) # - chunksize: Size of audio chunks to generate (default: 64) # Start playback sinsource.start() # Dynamically change frequency during playback (smooth transition) for x in range(20): sinsource.frequency = 220 + x * 20 # Sweep from 220Hz to 600Hz sleep(0.1) # Play musical notes notes = { 'C4': 261.63, 'D4': 293.66, 'E4': 329.63, 'F4': 349.23, 'G4': 392.00, 'A4': 440.00, 'B4': 493.88, 'C5': 523.25 } for note_name, freq in notes.items(): sinsource.frequency = freq print(f'Playing {note_name}: {freq}Hz') sleep(0.3) # Stop playback sinsource.stop() ``` -------------------------------- ### AudioOutput.add_sample - Add Sample to Mixer Source: https://context7.com/kivy/audiostream/llms.txt Adds an AudioSample instance to the internal mixer for playback. ```APIDOC ## AudioOutput.add_sample ### Description Adds an AudioSample instance to the internal mixer for playback. Multiple samples can be added. ### Parameters #### Arguments - **sample** (AudioSample) - Required - The sample instance to add to the mixer. ``` -------------------------------- ### Implement Custom Audio Generator with ThreadSource Source: https://context7.com/kivy/audiostream/llms.txt Subclass ThreadSource to create custom audio generators. Override get_bytes to provide audio data in a separate thread. ```python from audiostream import get_output from audiostream.sources.thread import ThreadSource from array import array import math class CustomToneSource(ThreadSource): """Custom audio generator that produces a tone with configurable frequency""" def __init__(self, stream, frequency=440): ThreadSource.__init__(self, stream) self.frequency = frequency self.phase = 0.0 self.amplitude = 0.5 def get_bytes(self): """Generate audio bytes - called repeatedly by the thread""" buf = array('h') # Signed 16-bit samples samples_to_generate = self.buffersize // (2 * self.channels) for _ in range(samples_to_generate): # Generate sine wave sample value = int(32767 * self.amplitude * math.sin(self.phase)) # Write to all channels for _ in range(self.channels): buf.append(value) # Advance phase self.phase += 2 * math.pi * self.frequency / self.rate if self.phase > 2 * math.pi: self.phase -= 2 * math.pi return buf.tobytes() # Usage stream = get_output(channels=2, rate=44100, buffersize=1024) tone = CustomToneSource(stream, frequency=880) tone.start() # Starts the generator thread # Tone plays until stopped import time time.sleep(2) tone.stop() ``` -------------------------------- ### Record Microphone Input Source: https://context7.com/kivy/audiostream/llms.txt Capture audio from the microphone using a callback function and save or process the data. ```python import time import wave from audiostream import get_input frames = [] def mic_callback(buf): """Store received audio frames""" print('Received', len(buf), 'bytes') frames.append(buf) # Initialize microphone with default settings (44100Hz, mono, 16-bit) mic = get_input(callback=mic_callback) # Start recording mic.start() # Record for 5 seconds, polling to receive data start_time = time.time() while time.time() - start_time < 5: mic.poll() time.sleep(0.01) # Small sleep to avoid busy waiting # Stop recording mic.stop() ``` -------------------------------- ### AudioSample Class Methods Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Methods for managing audio samples within the AudioStream mixer. ```APIDOC ## POST /audiostream/AudioSample/add_sample ### Description Adds an AudioSample to be managed by the internal mixer. Typically called when starting an AudioSample. ### Method POST ### Endpoint /audiostream/AudioSample/add_sample ### Parameters #### Request Body - **sample** (AudioSample) - Required - The AudioSample object to add to the mixer. ### Request Example ```json { "sample": "" } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation of successful addition. #### Response Example ```json { "message": "Sample added successfully." } ``` ## DELETE /audiostream/AudioSample/remove_sample ### Description Removes an AudioSample from the internal mixer. Typically called when stopping an AudioSample. ### Method DELETE ### Endpoint /audiostream/AudioSample/remove_sample ### Parameters #### Request Body - **sample** (AudioSample) - Required - The AudioSample object to remove from the mixer. ### Request Example ```json { "sample": "" } ``` ### Response #### Success Response (200) - **message** (string) - Confirmation of successful removal. #### Response Example ```json { "message": "Sample removed successfully." } ``` ``` -------------------------------- ### AudioOutput Class Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Abstract class for handling audio output streams and mixing multiple audio samples. It supports various sample implementations. ```APIDOC ## Class: audiostream.AudioOutput ### Description Abstract class for handling audio output stream, and handle the mixing of multiple sample. One sample is an instance of AudioSample abstract class. You can implement your own sample that generate bytes, and those bytes will be mixed in the final output stream. We also expose multiple AudioSample implementation, such as: * audiostream.sources.thread.ThreadSource: base for implementing a generator that run in a thread * audiostream.sources.wave.SineSource: generate a sine wave * audiostream.sources.puredata.PatchSource: sample generator that use a Puredata patch (require pylibpd) ``` -------------------------------- ### audiostream.get_input_sources Source: https://github.com/kivy/audiostream/blob/master/docs/source/index.md Retrieves a list of available audio input sources, which is platform-dependent. Permissions may be required to access certain sources. ```APIDOC ## audiostream.get_input_sources ### Description Return a list of available input sources. This list is platform-dependent. You might need some additionnals permissions in order to access to the sources. * android: ‘camcorder’, ‘default’, ‘mic’, ‘voice_call’, ‘voice_communication’, ‘voice_downlink’, ‘voice_recognition’, ‘voice_uplink’ * ios: ‘default’ ### Method `audiostream.get_input_sources` ### Return type list of strings #### NOTE This function currently work only on Android and iOS. ``` -------------------------------- ### audiostream.AudioOutput Source: https://github.com/kivy/audiostream/blob/master/docs/source/index.md Abstract class for handling audio output streams and mixing multiple samples. ```APIDOC ## audiostream.AudioOutput ### Description Abstract class for handling audio output stream and mixing multiple samples. It manages instances of AudioSample. ### Methods - **add_sample(sample)**: Adds a sample to the internal mixer. - **remove_sample(sample)**: Removes a sample from the internal mixer. ``` -------------------------------- ### ThreadSource Class Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md A sample generator that utilizes a thread. ```APIDOC ## Class: audiostream.sources.thread.ThreadSource ### Description Sample generator using a thread. It does nothing by default and can be used to implement custom generators. ### Methods #### __init__(stream: AudioOutput) ##### Description Initializes the ThreadSource. ##### Parameters - **stream** (AudioOutput) - Required - The `AudioOutput` instance to use. #### get_bytes() → bytes ##### Description Must return a bytes string containing the data to be stored in the ring buffer. ##### Returns (bytes) - The generated audio data. ``` -------------------------------- ### audiostream.sources.thread.ThreadSource Source: https://github.com/kivy/audiostream/blob/master/docs/source/index.md Base class for implementing sample generators that run in a thread. ```APIDOC ## audiostream.sources.thread.ThreadSource ### Description Sample generator using a thread. Inherits from AudioSample. ### Methods - **__init__(stream)**: Initializes the generator with an AudioOutput instance. - **get_bytes()**: Returns a bytes string with the data to store in the ring buffer. ``` -------------------------------- ### audiostream.sources.puredata.PatchSource Source: https://github.com/kivy/audiostream/blob/master/docs/source/index.md Sample generator that uses a PureData patch. ```APIDOC ## audiostream.sources.puredata.PatchSource ### Description Loads a PureData patch and reads the generated output. ### Methods - **__init__(stream, patchfile)**: Initializes the generator with an AudioOutput instance and a path to the patch file. ``` -------------------------------- ### Write Raw Audio Bytes to AudioSample Source: https://context7.com/kivy/audiostream/llms.txt Manages a ring buffer for streaming audio bytes to the speaker. Data written to the sample is consumed by the audio system. Underruns are filled with silence, and overruns block until buffer space is available. ```python from audiostream import get_output, AudioSample import struct # Initialize output stream stream = get_output(channels=1, buffersize=1024, rate=22050) # Create and register sample sample = AudioSample() stream.add_sample(sample) # Start playback sample.play() # Write raw audio bytes to the ring buffer ``` -------------------------------- ### Save audio frames to a WAV file Source: https://context7.com/kivy/audiostream/llms.txt Writes recorded audio frames to a file using the wave module. Ensure the mic object provides valid channel and rate attributes. ```python wf = wave.open("recording.wav", 'wb') wf.setnchannels(mic.channels) # Number of channels from input wf.setsampwidth(2) # 2 bytes for 16-bit audio wf.setframerate(mic.rate) # Sample rate from input wf.writeframes(b''.join(frames)) # Join all frames and write wf.close() print(f'Saved {len(frames)} frames to recording.wav') ``` -------------------------------- ### audiostream.get_input Source: https://github.com/kivy/audiostream/blob/master/docs/source/index.md Returns an `AudioInput` instance for reading audio data from an input source, such as a microphone. Received data is stored in a queue and dispatched via a callback. ```APIDOC ## audiostream.get_input ### Description Return an [`AudioInput`](#audiostream.AudioInput) instance. All the data received from the input will be stored in a queue. You need to [`AudioInput.poll()`](#audiostream.AudioInput.poll) the queue regulary in order to trigger the callback. Please note that the callback will be called in the same thread as the one that call [`AudioInput.poll()`](#audiostream.AudioInput.poll). ### Method `audiostream.get_input` ### Parameters #### Query Parameters - **callback** (callable) - Required - Callback to call when bytes are available on the input, called from the audio thread. - **source** (string) - Optional - Source device to read, default to ‘default. Depending of the platform, you might read other input source. Check the [`get_input_sources()`](#audiostream.get_input_sources) function. - **channels** (integer) - Optional - Number of channels, minimum 1, default to 2 - **encoding** (integer) - Optional - Encoding of the audio stream, can be 8 or 16, default to 16 - **buffersize** (integer) - Optional - Size of the input buffer. If <= 0, it will be automatically sized by the system. ### Return type [`AudioInput`](#audiostream.AudioInput) instance ### Request Example ```python from audiostream import get_input def mic_callback(buf): print('got', len(buf)) # get the default audio input (mic on most cases) mic = get_input(callback=mic_callback) mic.start() while not quit: mic.poll() # do something here, like sleep(2) mic.stop() ``` #### NOTE This function currently work only on Android and iOS. ``` -------------------------------- ### Generate and Stream Sine Wave Audio Source: https://github.com/kivy/audiostream/blob/master/docs/source/examples.md Generates a sine wave and streams it to the speaker. Allows changing the frequency during playback. Requires `audiostream` and `time` modules. ```python from time import sleep from audiostream import get_output from audiostream.sources.wave import SineSource # get a output stream where we can play samples stream = get_output(channels=2, rate=22050, buffersize=1024) # create one wave sin() at 440Hz, attach it to our speaker, and play sinsource = SineSource(stream, 440) sinsource.start() # you can change the frequency of the source during the playtime for x in xrange(10): sinsource.frequency = 440 + x sleep(.5) # ok we are done, stop everything. sinsource.stop() ``` -------------------------------- ### PatchSource Class Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Loads and reads output from a PureData patch. ```APIDOC ## Class: audiostream.sources.puredata.PatchSource ### Description Loads a PureData patch and reads the generated output. ### Methods #### __init__(stream: AudioOutput, patchfile: string) ##### Description Initializes the PatchSource. ##### Parameters - **stream** (AudioOutput) - Required - The `AudioOutput` instance to use. - **patchfile** (string) - Required - The filename of the PureData patch to load using pylibpd. ``` -------------------------------- ### SineSource Class Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md A sample generator that produces sine wave audio data. ```APIDOC ## Class: audiostream.sources.wave.SineSource ### Description Sample generator that uses `ThreadSource` and generates bytes from a sine wave function. ### Methods #### __init__(stream: AudioOutput, frequency: int) ##### Description Initializes the SineSource. ##### Parameters - **stream** (AudioOutput) - Required - The `AudioOutput` instance to use. - **frequency** (integer) - Required - The frequency of the sine wave in Hz (e.g., 440). ``` -------------------------------- ### audiostream.sources.wave.SineSource Source: https://github.com/kivy/audiostream/blob/master/docs/source/index.md Sample generator that produces a sine wave. ```APIDOC ## audiostream.sources.wave.SineSource ### Description Generates audio bytes from a sine wave generator. ### Methods - **__init__(stream, frequency)**: Initializes the generator with an AudioOutput instance and a frequency (e.g., 440). ``` -------------------------------- ### AudioSample Class Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Represents an audio sample for generating bytes consumed by AudioOutput. ```APIDOC ## Class: audiostream.AudioSample ### Description An `AudioSample` class is used for generating bytes that will be consumed by `AudioOutput`. Data is first placed in a RingBuffer, which is then consumed by the speaker according to the `AudioOutput` initialization settings. ### Methods #### write(chunk: bytes) ##### Description Writes a data chunk into the ring buffer. This data will be consumed later by the speaker. ##### Parameters - **chunk** (bytes) - Required - The data chunk to write. #### play() ##### Description Plays the sample using the internal ring buffer. #### stop() ##### Description Stops the playback of the sample. ``` -------------------------------- ### audiostream.AudioSample Source: https://github.com/kivy/audiostream/blob/master/docs/source/index.md Class for generating bytes consumed by AudioOutput. ```APIDOC ## audiostream.AudioSample ### Description Generates bytes that are stored in a RingBuffer and consumed by the speaker via AudioOutput. ### Methods - **write(chunk)**: Writes a data chunk (bytes) into the ring buffer. - **play()**: Starts playback of the sample. - **stop()**: Stops playback of the sample. ``` -------------------------------- ### Capture Audio Input Source: https://github.com/kivy/audiostream/blob/master/docs/source/api.md Retrieves an audio input instance and processes data via a callback. The poll method must be called regularly to trigger the callback. ```python from audiostream import get_input def mic_callback(buf): print('got', len(buf)) # get the default audio input (mic on most cases) mic = get_input(callback=mic_callback) mic.start() while not quit: mic.poll() # do something here, like sleep(2) mic.stop() ``` -------------------------------- ### Read Microphone Audio Bytes Source: https://github.com/kivy/audiostream/blob/master/docs/source/examples.md Reads audio data from the microphone and prints the size of the received data. Requires `audiostream` module. On Android, RECORD_AUDIO permission is necessary. ```python from audiostream import get_input # declare a callback where we'll receive the data def callback_mic(data): print('i got', len(data)) # get the microphone (or from another source if available) mic = get_input(callback=callback_mic) mic.start() sleep(5) mic.stop() ``` -------------------------------- ### Remove AudioSample from Mixer Source: https://context7.com/kivy/audiostream/llms.txt Removes an `AudioSample` from the `AudioOutput` mixer, stopping its playback. This is useful for deallocating resources when a sample is no longer needed. ```python from audiostream import get_output, AudioSample stream = get_output(channels=2, rate=44100, buffersize=1024) sample = AudioSample() stream.add_sample(sample) sample.play() # Later, when done with the sample stream.remove_sample(sample) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.