### Instantiate and Use PulseAudio Player in Go Source: https://context7.com/xaionaro-go/audio/llms.txt This Go code snippet shows how to directly instantiate the PulseAudio backend for audio playback. It bypasses the automatic backend selection, allowing for explicit control over the audio hardware. The example opens a raw audio file and plays it using the configured PulseAudio player. ```go package main import ( "context" "os" "time" "github.com/xaionaro-go/audio/pkg/audio" "github.com/xaionaro-go/audio/pkg/audio/backends/pulseaudio" ) func main() { ctx := context.Background() // Create PulseAudio player directly pulsePCMPlayer := pulseaudio.NewPlayerPCM() player := audio.NewPlayer(pulsePCMPlayer) defer player.Close() // Play audio through PulseAudio file, _ := os.Open("audio.raw") defer file.Close() stream, err := player.PlayPCM( ctx, audio.SampleRate(48000), audio.Channel(2), audio.PCMFormatFloat32LE, 100*time.Millisecond, file, ) if err != nil { panic(err) } defer stream.Close() stream.Drain() } ``` -------------------------------- ### Record Audio using Go Source: https://github.com/xaionaro-go/audio/blob/main/README.md Shows how to record PCM audio data to an io.Writer using the automatic recorder backend. The example captures 48kHz stereo float32 audio for a duration of 5 seconds. ```go import ( "github.com/xaionaro-go/audio/pkg/audio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/portaudio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/pulseaudio" ) func record5Seconds(ctx context.Context, w io.Writer) { ctx, cancelFn := context.WithCancel(ctx) recorder := audio.NewRecorderAuto(ctx) defer recorder.Close() streamRecord, err := recorder.RecordPCM(ctx, 48000, 2, audio.PCMFormatFloat32LE, w) defer streamRecord.Close() time.Sleep(5 * time.Second) cancelFn() } ``` -------------------------------- ### Initialize Audio Player with Automatic Backend Selection Source: https://context7.com/xaionaro-go/audio/llms.txt Demonstrates how to instantiate an audio player that automatically selects the best available backend. It shows the process of playing a Vorbis/OGG file using the library's unified API. ```go package main import ( "bytes" "context" "os" "time" "github.com/xaionaro-go/audio/pkg/audio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/oto" _ "github.com/xaionaro-go/audio/pkg/audio/backends/portaudio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/pulseaudio" ) func main() { ctx := context.Background() player := audio.NewPlayerAuto(ctx) defer player.Close() oggData, _ := os.ReadFile("audio.ogg") stream, err := player.PlayVorbis(ctx, bytes.NewReader(oggData)) if err != nil { panic(err) } defer stream.Close() stream.Drain() } ``` -------------------------------- ### Play Audio using Backends in Go Source: https://github.com/xaionaro-go/audio/blob/main/README.md Demonstrates how to initialize an audio player using automatic backend selection or a specific backend implementation. It supports playing Vorbis files or raw PCM streams. ```go import ( "github.com/xaionaro-go/audio/pkg/audio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/oto" _ "github.com/xaionaro-go/audio/pkg/audio/backends/portaudio" ) func beep(ctx context.Context, vorbisReader io.Reader) { player := audio.NewPlayerAuto(ctx) defer player.Close() stream, err := player.PlayVorbis(ctx, vorbisReader) } ``` ```go pulsePCMPlayer := pulse.NewPlayerPCM() player := audio.NewPlayer(pulsePCMPlayer) defer player.Close() stream, err := player.PlayVorbis(ctx, vorbisReader) ``` -------------------------------- ### Record Audio with Automatic Backend Selection Source: https://context7.com/xaionaro-go/audio/llms.txt Illustrates how to initialize an audio recorder and capture PCM data to an io.Writer. The recorder automatically selects an appropriate backend and supports configurable recording parameters. ```go package main import ( "context" "os" "time" "github.com/xaionaro-go/audio/pkg/audio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/portaudio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/pulseaudio" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() recorder := audio.NewRecorderAuto(ctx) defer recorder.Close() outputFile, _ := os.Create("recording.raw") defer outputFile.Close() stream, err := recorder.RecordPCM( ctx, audio.SampleRate(48000), audio.Channel(2), audio.PCMFormatFloat32LE, outputFile, ) if err != nil { panic(err) } defer stream.Close() time.Sleep(5 * time.Second) cancel() } ``` -------------------------------- ### PCM Format Utilities and Constants in Go Source: https://context7.com/xaionaro-go/audio/llms.txt Provides constants for various PCM audio formats and utility functions for working with them. This includes format definitions like `audio.PCMFormatFloat32LE`, string parsing with `audio.PCMFormatFromString`, and calculations for bytes per sample, second, and duration using `audio.EncodingPCM`. ```go package main import ( "fmt" "time" "github.com/xaionaro-go/audio/pkg/audio" ) func main() { // Available PCM formats formats := []audio.PCMFormat{ audio.PCMFormatU8, // Unsigned 8-bit audio.PCMFormatS16LE, // Signed 16-bit little-endian audio.PCMFormatS16BE, // Signed 16-bit big-endian audio.PCMFormatS24LE, // Signed 24-bit little-endian audio.PCMFormatS24BE, // Signed 24-bit big-endian audio.PCMFormatS32LE, // Signed 32-bit little-endian audio.PCMFormatS32BE, // Signed 32-bit big-endian audio.PCMFormatFloat32LE, // 32-bit float little-endian audio.PCMFormatFloat32BE, // 32-bit float big-endian audio.PCMFormatFloat64LE, // 64-bit float little-endian audio.PCMFormatFloat64BE, // 64-bit float big-endian audio.PCMFormatS64LE, // Signed 64-bit little-endian audio.PCMFormatS64BE, // Signed 64-bit big-endian } for _, f := range formats { fmt.Printf("Format: %s, Size: %d bytes\n", f.String(), f.Size()) } // Parse format from string format := audio.PCMFormatFromString("s16le") fmt.Printf("Parsed format: %v\n", format) // Calculate bytes for duration using EncodingPCM encoding := audio.EncodingPCM{ PCMFormat: audio.PCMFormatFloat32LE, SampleRate: audio.SampleRate(48000), } bytesPerSample := encoding.BytesPerSample() // 4 bytes bytesPerSecond := encoding.BytesForSecond() // 192000 bytes bytesFor5Sec := encoding.BytesForDuration(5 * time.Second) // 960000 bytes fmt.Printf("Bytes per sample: %d\n", bytesPerSample) fmt.Printf("Bytes per second: %d\n", bytesPerSecond) fmt.Printf("Bytes for 5 seconds: %d\n", bytesFor5Sec) } ``` -------------------------------- ### GCC-PHAT Audio Synchronization in Go Source: https://context7.com/xaionaro-go/audio/llms.txt Calculates the time delay between audio tracks using the Generalized Cross-Correlation with Phase Transform (GCC-PHAT) algorithm. This is implemented using `gccphat.NewSyncer` and is ideal for synchronizing multiple audio recordings or detecting audio offsets. It requires audio encoding details and returns shift and confidence values. ```go package main import ( "context" "fmt" "os" "github.com/xaionaro-go/audio/pkg/audio" "github.com/xaionaro-go/audio/pkg/syncer/implementations/gccphat" ) func main() { ctx := context.Background() // Define audio encoding encoding := audio.EncodingPCM{ PCMFormat: audio.PCMFormatFloat32LE, SampleRate: audio.SampleRate(48000), } // Create syncer syncer, err := gccphat.NewSyncer(encoding, audio.Channel(1)) if err != nil { panic(err) } defer syncer.Close() // Load reference and comparison audio tracks referenceTrack, _ := os.ReadFile("reference.raw") comparisonTrack, _ := os.ReadFile("comparison.raw") // Calculate shift between tracks results, err := syncer.CalculateShiftBetween( ctx, referenceTrack, comparisonTrack, // Can pass multiple comparison tracks ) if err != nil { panic(err) } for i, result := range results { fmt.Printf("Track %d: Shift=%.2f samples, Confidence=%.2f\n", i, result.Shift, result.Confidence) } } ``` -------------------------------- ### Audio Player - NewPlayerAuto Source: https://context7.com/xaionaro-go/audio/llms.txt Creates an audio player that automatically selects the best available backend (Oto, PortAudio, or PulseAudio) for playback. Supports PCM and Vorbis audio formats. ```APIDOC ## Audio Player - NewPlayerAuto ### Description Creates an audio player that automatically selects the best available backend (Oto, PortAudio, or PulseAudio) based on platform availability. The player supports PCM and Vorbis audio playback with configurable sample rates, channels, and formats. ### Method `NewPlayerAuto(ctx context.Context) Player` ### Endpoint N/A (Library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "bytes" "context" "os" "time" "github.com/xaionaro-go/audio/pkg/audio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/oto" _ "github.com/xaionaro-go/audio/pkg/audio/backends/portaudio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/pulseaudio" ) func main() { ctx := context.Background() player := audio.NewPlayerAuto(ctx) defer player.Close() oggData, _ := os.ReadFile("audio.ogg") stream, err := player.PlayVorbis(ctx, bytes.NewReader(oggData)) if err != nil { panic(err) } defer stream.Close() stream.Drain() } ``` ### Response #### Success Response (200) Returns a `Player` interface which provides methods for audio playback. #### Response Example N/A (Returns an interface) ``` -------------------------------- ### Planarize/Unplanarize Audio Format Conversion in Go Source: https://context7.com/xaionaro-go/audio/llms.txt Converts between interleaved and planar audio formats using `planar.NewPlanarizeReader` and `planar.NewUnplanarizeReader`. This is useful for processing audio data where different channel layouts are required. It handles reading from an input source and writing to an output destination, performing the conversion on the fly. ```go package main import ( "io" "os" "github.com/xaionaro-go/audio/pkg/audio" "github.com/xaionaro-go/audio/pkg/audio/planar" ) func main() { // Open interleaved stereo audio inputFile, _ := os.Open("interleaved_stereo.raw") defer inputFile.Close() // Create planarize reader (converts interleaved to planar on read) planarReader := planar.NewPlanarizeReader( inputFile, audio.Channel(2), // Number of channels 4, // Sample size in bytes (float32 = 4) 8192, // Buffer size (must be multiple of channels * sample_size) ) // Read planarized audio outputFile, _ := os.Create("planar_stereo.raw") defer outputFile.Close() io.Copy(outputFile, planarReader) // --- Converting back to interleaved --- planarFile, _ := os.Open("planar_stereo.raw") defer planarFile.Close() // Create unplanarize reader (converts planar to interleaved on read) interleavedReader := planar.NewUnplanarizeReader( planarFile, audio.Channel(2), 4, 8192, ) finalOutput, _ := os.Create("interleaved_again.raw") defer finalOutput.Close() io.Copy(finalOutput, interleavedReader) } ``` -------------------------------- ### Audio Recorder - NewRecorderAuto Source: https://context7.com/xaionaro-go/audio/llms.txt Creates an audio recorder that automatically selects the best available recording backend (PortAudio or PulseAudio). Records PCM audio to any io.Writer. ```APIDOC ## Audio Recorder - NewRecorderAuto ### Description Creates an audio recorder that automatically selects the best available recording backend (PortAudio or PulseAudio). Records PCM audio to any io.Writer with configurable sample rate, channels, and format. ### Method `NewRecorderAuto(ctx context.Context) Recorder` ### Endpoint N/A (Library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "context" "os" "time" "github.com/xaionaro-go/audio/pkg/audio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/portaudio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/pulseaudio" ) func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() recorder := audio.NewRecorderAuto(ctx) defer recorder.Close() outputFile, _ := os.Create("recording.raw") defer outputFile.Close() stream, err := recorder.RecordPCM( ctx, audio.SampleRate(48000), audio.Channel(2), audio.PCMFormatFloat32LE, outputFile, ) if err != nil { panic(err) } defer stream.Close() time.Sleep(5 * time.Second) cancel() } ``` ### Response #### Success Response (200) Returns a `Recorder` interface which provides methods for audio recording. The `Recorder` interface has a `Close()` method to stop recording. #### Response Example N/A (Returns an interface) ``` -------------------------------- ### Play Raw PCM Audio Data Source: https://context7.com/xaionaro-go/audio/llms.txt Shows how to play raw PCM audio streams by explicitly defining sample rates, channel counts, and bit formats. This method is suitable for handling uncompressed audio data from various sources. ```go package main import ( "context" "os" "time" "github.com/xaionaro-go/audio/pkg/audio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/oto" ) func main() { ctx := context.Background() player := audio.NewPlayerAuto(ctx) defer player.Close() file, _ := os.Open("audio.raw") defer file.Close() stream, err := player.PlayPCM( ctx, audio.SampleRate(48000), audio.Channel(2), audio.PCMFormatFloat32LE, 100*time.Millisecond, file, ) if err != nil { panic(err) } defer stream.Close() stream.Drain() } ``` -------------------------------- ### Resample Audio with NewResampler Source: https://context7.com/xaionaro-go/audio/llms.txt Converts audio streams between different sample rates, channel counts, and PCM formats. It implements the io.Reader interface, allowing for seamless integration into standard Go audio processing pipelines. ```go package main import ( "io" "os" "github.com/xaionaro-go/audio/pkg/audio" "github.com/xaionaro-go/audio/pkg/audio/resampler" ) func main() { inputFile, _ := os.Open("input_44100_mono.raw") defer inputFile.Close() inputFormat := resampler.Format{ Channels: audio.Channel(1), SampleRate: audio.SampleRate(44100), PCMFormat: audio.PCMFormatS16LE, } outputFormat := resampler.Format{ Channels: audio.Channel(2), SampleRate: audio.SampleRate(48000), PCMFormat: audio.PCMFormatFloat32LE, } r, err := resampler.NewResampler(inputFormat, inputFile, outputFormat) if err != nil { panic(err) } outputFile, _ := os.Create("output_48000_stereo.raw") defer outputFile.Close() io.Copy(outputFile, r) } ``` -------------------------------- ### Audio Player - PlayPCM Source: https://context7.com/xaionaro-go/audio/llms.txt Plays raw PCM audio data with specified sample rate, channels, format, and buffer size. Supports various PCM formats. ```APIDOC ## Audio Player - PlayPCM ### Description Plays raw PCM audio data with specified sample rate, channels, format, and buffer size. Supports various PCM formats including signed 16-bit, 32-bit float, and 64-bit formats in both little and big endian. ### Method `PlayPCM(ctx context.Context, sampleRate SampleRate, channels Channel, format PCMFormat, bufferSize time.Duration, reader io.Reader) (Stream, error)` ### Endpoint N/A (Library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "context" "os" "time" "github.com/xaionaro-go/audio/pkg/audio" _ "github.com/xaionaro-go/audio/pkg/audio/backends/oto" ) func main() { ctx := context.Background() player := audio.NewPlayerAuto(ctx) defer player.Close() file, _ := os.Open("audio.raw") defer file.Close() stream, err := player.PlayPCM( ctx, audio.SampleRate(48000), audio.Channel(2), audio.PCMFormatFloat32LE, 100*time.Millisecond, file, ) if err != nil { panic(err) } defer stream.Close() stream.Drain() } ``` ### Response #### Success Response (200) Returns a `Stream` interface for managing the playback. The `Stream` interface has a `Close()` method to stop playback and a `Drain()` method to wait until all audio has been played. #### Response Example N/A (Returns an interface) ``` -------------------------------- ### Detect Voice Activity with libfvad Source: https://context7.com/xaionaro-go/audio/llms.txt Utilizes the libfvad library to identify voice segments within audio samples. It returns confidence scores and the duration of detected segments based on configurable sensitivity and thresholds. ```go package main import ( "context" "fmt" "os" "time" "github.com/xaionaro-go/audio/pkg/audio" libfvad "github.com/xaionaro-go/audio/pkg/vad/implementations/libfvad" ) func main() { ctx := context.Background() vad, err := libfvad.NewVAD(audio.SampleRate(8000), 3) if err != nil { panic(err) } defer vad.Close() samples, _ := os.ReadFile("speech_8000hz_mono_s16le.raw") confidence, voiceStart, err := vad.FindNextVoice( ctx, samples, 0.5, 100*time.Millisecond, ) if err != nil { panic(err) } if voiceStart >= 0 { fmt.Printf("Voice detected at %v with confidence %.2f\n", voiceStart, confidence) } else { fmt.Println("No voice detected") } } ``` -------------------------------- ### Suppress Noise with RNNoise Source: https://context7.com/xaionaro-go/audio/llms.txt Applies neural-network-based noise suppression to audio streams using RNNoise. This implementation supports 48kHz float32 audio and provides a streaming wrapper for continuous processing. ```go //go:build rnnoise package main import ( "context" "io" "os" "github.com/xaionaro-go/audio/pkg/audio" "github.com/xaionaro-go/audio/pkg/noisesuppression/implementations/rnnoise" "github.com/xaionaro-go/audio/pkg/noisesuppressionstream" ) func main() { ctx := context.Background() inputFile, _ := os.Open("noisy_audio_48k_stereo.raw") defer inputFile.Close() noiseSuppressor, err := rnnoise.New(audio.Channel(2)) if err != nil { panic(err) } defer noiseSuppressor.Close() stream, err := noisesuppressionstream.NewNoiseSuppressionStream( ctx, inputFile, noiseSuppressor, 65536, 65536, ) if err != nil { panic(err) } outputFile, _ := os.Create("clean_audio.raw") defer outputFile.Close() io.Copy(outputFile, stream) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.