### Full ATRAC9 Decoding Example Source: https://github.com/thealexbarney/vgaudio/blob/master/docs/vgaudio/Atrac9Decoder.md A complete example demonstrating how to initialize the decoder, prepare PCM buffers, and loop through superframes for decoding. ```csharp // Initialize the decoder var decoder = new Atrac9Decoder(); decoder.Initialize(configData); // Create a buffer for the output PCM var pcmBuffer = new short[decoder.Config.ChannelCount][]; for (int i = 0; i < pcmBuffer.Length; i++) { pcmBuffer[i] = new short[decoder.Config.SuperframeSamples]; } // Decode each superframe for (int i = 0; i < atrac9Data.Length; i++) { decoder.Decode(atrac9Data[i], pcmBuffer); // Use the decoded audio in pcmBuffer however you want } ``` -------------------------------- ### Basic CLI Conversion Commands Source: https://github.com/thealexbarney/vgaudio/blob/master/README.md Standard operations for converting, looping, and inspecting audio files using VGAudioCli. ```bash VGAudioCli input.wav output.dsp ``` ```bash VGAudioCli -o output.dsp -i input.wav ``` ```bash VGAudioCli input.wav output.dsp -l 30000-200000 ``` ```bash VGAudioCli -m input.wav ``` ```bash VGAudioCli input.wav output.brstm -f pcm16 ``` -------------------------------- ### Convert Audio Formats and Access Data Source: https://context7.com/thealexbarney/vgaudio/llms.txt Illustrates how to use the AudioData class to retrieve specific audio formats, access raw channel data, and query available formats. ```csharp using VGAudio.Formats; using VGAudio.Formats.GcAdpcm; using VGAudio.Formats.Pcm16; using VGAudio.Formats.CriHca; using VGAudio.Codecs.GcAdpcm; // Read audio from any format var reader = new WaveReader(); AudioData audio = reader.Read("input.wav"); // Get PCM16 format (universal intermediate format) Pcm16Format pcm16 = audio.GetFormat(); Console.WriteLine($"Channels: {pcm16.ChannelCount}"); Console.WriteLine($"Sample Rate: {pcm16.SampleRate}"); Console.WriteLine($"Sample Count: {pcm16.SampleCount}"); Console.WriteLine($"Duration: {(double)pcm16.SampleCount / pcm16.SampleRate} seconds"); // Get GC-ADPCM format (triggers encoding if not already available) var gcAdpcmParams = new GcAdpcmParameters(); GcAdpcmFormat gcAdpcm = audio.GetFormat(gcAdpcmParams); // Access raw channel data short[][] pcmChannels = pcm16.Channels; GcAdpcmChannel[] adpcmChannels = gcAdpcm.Channels; // Check which formats are currently available (without triggering conversion) foreach (Type format in audio.ListAvailableFormats()) { Console.WriteLine($"Available: {format.Name}"); } ``` -------------------------------- ### Manage Audio Loop Points in C# Source: https://context7.com/thealexbarney/vgaudio/llms.txt Demonstrates setting, removing, and checking loop points, as well as configuring frame-aligned loops for GC-ADPCM formats. ```csharp using VGAudio.Formats; using VGAudio.Containers.Wave; using VGAudio.Containers.NintendoWare; var reader = new WaveReader(); AudioData audio = reader.Read("music.wav"); // Set loop points (sample-based) int loopStart = 44100; // Start at 1 second (assuming 44100 Hz) int loopEnd = 220500; // End at 5 seconds audio.SetLoop(true, loopStart, loopEnd); // Remove loop points audio.SetLoop(false); // Check current loop status via format Pcm16Format pcm = audio.GetFormat(); Console.WriteLine($"Looping: {pcm.Looping}"); Console.WriteLine($"Loop Start: {pcm.LoopStart}"); Console.WriteLine($"Loop End: {pcm.LoopEnd}"); // Write with loop points preserved var brstmWriter = new BrstmWriter(); brstmWriter.WriteToFile(audio, "looped_music.brstm"); // GC-ADPCM loop alignment (required for some formats) var brstmConfig = new BxstmConfiguration { LoopPointAlignment = 14336, // Align loop start to frame boundary RecalculateLoopContext = true }; brstmWriter.WriteToFile(audio, "aligned_loop.brstm", brstmConfig); ``` -------------------------------- ### Initialize Atrac9Decoder Source: https://github.com/thealexbarney/vgaudio/blob/master/docs/vgaudio/Atrac9Decoder.md Initialize the Atrac9Decoder with configuration data before decoding. ```csharp Atrac9Decoder decoder = new Atrac9Decoder().Initialize(configData); ``` -------------------------------- ### Write Audio Files with VGAudio Source: https://context7.com/thealexbarney/vgaudio/llms.txt Shows how to export AudioData to different container formats using specific writer classes and configuration objects. ```csharp using VGAudio.Containers.Wave; using VGAudio.Containers.Dsp; using VGAudio.Containers.NintendoWare; using VGAudio.Formats; // Read source audio var reader = new WaveReader(); AudioData audio = reader.Read("input.wav"); // Write to Nintendo DSP format var dspWriter = new DspWriter(); dspWriter.WriteToFile(audio, "output.dsp"); // Write to BRSTM with custom configuration var brstmWriter = new BrstmWriter(); var brstmConfig = new BxstmConfiguration { Codec = NwCodec.GcAdpcm, SamplesPerInterleave = 14336, SamplesPerSeekTableEntry = 14336 }; brstmWriter.WriteToFile(audio, "output.brstm", brstmConfig); // Write to WAV with specific bit depth var waveWriter = new WaveWriter(); var waveConfig = new WaveConfiguration { Codec = WaveCodec.Pcm16Bit // or WaveCodec.Pcm8Bit }; waveWriter.WriteToFile(audio, "output.wav", waveConfig); // Write to stream using (FileStream outStream = File.Create("output.dsp")) { dspWriter.WriteToStream(audio, outStream); } ``` -------------------------------- ### Read Audio Files with VGAudio Source: https://context7.com/thealexbarney/vgaudio/llms.txt Demonstrates reading various audio container formats into an AudioData object from files or streams, including metadata-only extraction. ```csharp using VGAudio.Containers.Wave; using VGAudio.Containers.Dsp; using VGAudio.Containers.NintendoWare; using VGAudio.Formats; // Read a WAV file var waveReader = new WaveReader(); AudioData wavAudio = waveReader.Read("input.wav"); // Read a Nintendo DSP file var dspReader = new DspReader(); AudioData dspAudio = dspReader.Read("input.dsp"); // Read a BRSTM file (Nintendo Wii/Revolution) var brstmReader = new BrstmReader(); AudioData brstmAudio = brstmReader.Read("input.brstm"); // Read from a stream instead of a file using (FileStream stream = File.OpenRead("audio.wav")) { AudioData streamAudio = waveReader.Read(stream); } // Read only metadata without audio data (faster for file info queries) AudioData metadataOnly = waveReader.ReadMetadata("input.wav"); ``` -------------------------------- ### Splitting and Combining Audio Channels Source: https://github.com/thealexbarney/vgaudio/blob/master/README.md Advanced channel selection and multi-file merging operations. ```bash VGAudioCli -i:0-2,5 input.wav output.dsp ``` ```bash VGAudioCli -i input_left.wav -i input_right.wav output_stereo.dsp ``` ```bash VGAudioCli -i:0,3 input1.wav -i:1 input2.dsp -i input3.adx output.hca ``` -------------------------------- ### Read and Write Nintendo Audio Formats in C# Source: https://context7.com/thealexbarney/vgaudio/llms.txt Use format-specific readers and writers to process Nintendo audio files. Configuration objects allow for fine-tuning codec, endianness, and loop settings. ```csharp using VGAudio.Containers.NintendoWare; using VGAudio.Formats; // Read various Nintendo formats var brstmReader = new BrstmReader(); // Wii BRSTM var bcfstmReader = new BCFstmReader(); // 3DS BCSTM and Wii U/Switch BFSTM AudioData wiiAudio = brstmReader.Read("music.brstm"); AudioData switchAudio = bcfstmReader.Read("music.bfstm"); // Write BRSTM (Wii) var brstmWriter = new BrstmWriter(); var brstmConfig = new BxstmConfiguration { Codec = NwCodec.GcAdpcm, // GcAdpcm, Pcm16Bit, or Pcm8Bit SamplesPerInterleave = 14336, SamplesPerSeekTableEntry = 14336, TrackType = BrstmTrackType.Standard, // Standard or Short LoopPointAlignment = 1, RecalculateSeekTable = true, RecalculateLoopContext = true }; brstmWriter.WriteToFile(wiiAudio, "output.brstm", brstmConfig); // Write BFSTM (Switch/Wii U) var bfstmWriter = new BCFstmWriter(); var bfstmConfig = new BxstmConfiguration { Codec = NwCodec.GcAdpcm, Endianness = Endianness.LittleEndian, // LittleEndian for Switch // Use BigEndian for Wii U }; bfstmWriter.WriteToFile(switchAudio, "output.bfstm", bfstmConfig); // Write BCSTM (3DS) var bcstmConfig = new BxstmConfiguration { Codec = NwCodec.GcAdpcm, Endianness = Endianness.LittleEndian }; bfstmWriter.WriteToFile(wiiAudio, "output.bcstm", bcstmConfig); ``` -------------------------------- ### Method D: Huffman Coded Delta of Offset with Baseline Source: https://github.com/thealexbarney/vgaudio/blob/master/docs/audio-formats/atrac9/scale-factors.md This method is similar to Method A, but uses another set of scale factors as a baseline instead of pre-defined weights. If the baseline has fewer scale factors than the set being coded, the remaining values are directly stored as 5-bit integers. ```APIDOC ## Method D: Huffman Coded Delta of Offset with Baseline ### Description This method is similar to Method A, but uses another set of scale factors as a baseline instead of pre-defined weights. If the baseline has fewer scale factors than the set being coded, the remaining values are directly stored as 5-bit integers. ### Parameters #### Variable Length Parameters - **base_value** (5 bits) - The base value. Stored with offset binary representation. - **bit_length** (2 bits) - The bit length of the encoded values. Add 1 to the read value. - **initial_value** (`bit_length`) - The initial value for the delta coding. - **delta_values** (varies) - Huffman encoded unsigned deltas. - **direct_values** (5 bits each) - The remaining scale factors. Used when the block being read has more scale factors than the baseline. ### Decoding Process 1. Read the Huffman encoded values and perform delta decoding to obtain the offsets. 2. Add the `base_value` to each offset. 3. Add the vector of baseline scale factors to the vector being decoded. 4. Read any leftover values from the bitstream. ``` -------------------------------- ### Manipulate Audio Channels in C# Source: https://context7.com/thealexbarney/vgaudio/llms.txt Covers extracting specific channels, combining multiple audio files into a single multi-channel file, and creating audio from raw PCM data. ```csharp using VGAudio.Formats; using VGAudio.Formats.Pcm16; using VGAudio.Containers.Wave; // Read a stereo file var reader = new WaveReader(); AudioData stereoAudio = reader.Read("stereo.wav"); // Extract specific channels (0 = left, 1 = right) Pcm16Format pcm = stereoAudio.GetFormat(); Pcm16Format leftOnly = (Pcm16Format)pcm.GetChannels(0); Pcm16Format rightOnly = (Pcm16Format)pcm.GetChannels(1); // Combine multiple audio files into one multi-channel file AudioData file1 = reader.Read("left.wav"); AudioData file2 = reader.Read("right.wav"); AudioData combined = AudioData.Combine(file1, file2); // Create audio from raw PCM data short[][] channels = new short[2][]; channels[0] = new short[44100]; // Left channel, 1 second at 44.1kHz channels[1] = new short[44100]; // Right channel // Fill with a sine wave for (int i = 0; i < 44100; i++) { double sample = Math.Sin(2 * Math.PI * 440 * i / 44100); channels[0][i] = (short)(sample * 32767); channels[1][i] = (short)(sample * 32767); } var format = new Pcm16Format(channels, 44100); var audioData = new AudioData(format); var writer = new WaveWriter(); writer.WriteToFile(audioData, "sine_wave.wav"); ``` -------------------------------- ### VGAudioCli Command-Line Operations Source: https://context7.com/thealexbarney/vgaudio/llms.txt Perform audio conversions, metadata extraction, and batch processing using the VGAudio command-line interface. ```bash # Basic conversion (format inferred from extension) VGAudioCli input.wav output.dsp VGAudioCli input.brstm output.wav # Explicit input/output specification VGAudioCli -i input.wav -o output.dsp # Set loop points (sample numbers) VGAudioCli input.wav output.dsp -l 30000-200000 # Remove loop points VGAudioCli input.brstm output.brstm --no-loop # Show file metadata VGAudioCli -m input.brstm # Specify output audio format for multi-format containers VGAudioCli input.wav output.brstm -f pcm16 VGAudioCli input.wav output.brstm -f gcadpcm # Channel selection (extract channels 0, 1, 2, and 5) VGAudioCli -i:0-2,5 input.wav output.dsp # Combine multiple mono files into stereo VGAudioCli -i left.wav -i right.wav output_stereo.dsp # Mix channels from multiple sources VGAudioCli -i:0,3 input1.wav -i:1 input2.dsp -i input3.adx output.hca # Batch conversion (convert entire folder) VGAudioCli -b -i source_folder -o dest_folder --out-format hca # Recursive batch conversion VGAudioCli -b -r -i source_folder -o dest_folder --out-format wav # Batch with options VGAudioCli -b -i source -o dest --out-format hca --no-loop VGAudioCli -b -i source -o dest --out-format hca --hcaquality high # HCA quality settings VGAudioCli input.wav output.hca --hcaquality highest # Quality levels: highest, high, middle, low, lowest # HCA bitrate VGAudioCli input.wav output.hca --bitrate 256000 # ADX encryption VGAudioCli input.wav output.adx --keycode 0x7F4551499DF55E68 VGAudioCli input.wav output.adx --keystring mypassword ``` -------------------------------- ### Batch Conversion Operations Source: https://github.com/thealexbarney/vgaudio/blob/master/README.md Parallel processing of entire directories with optional recursive subfolder support and codec-specific flags. ```bash VGAudioCli -b -i source -o dest --out-format hca ``` ```bash VGAudioCli -b -r -i source -o dest --out-format hca ``` ```bash VGAudioCli -b -i source -o dest --out-format hca --no-loop ``` ```bash VGAudioCli -b -i source -o dest --out-format hca --hcaquality low ``` -------------------------------- ### Encode and Decode GC-ADPCM in C# Source: https://context7.com/thealexbarney/vgaudio/llms.txt Provides low-level access to GC-ADPCM coefficient calculation, encoding, decoding, and building formatted channel data with loop context. ```csharp using VGAudio.Codecs.GcAdpcm; using VGAudio.Formats.GcAdpcm; // Low-level encoding from PCM samples short[] pcmSamples = new short[44100]; // 1 second of audio // ... fill pcmSamples with audio data ... // Calculate optimal coefficients for this audio short[] coefficients = GcAdpcmCoefficients.CalculateCoefficients(pcmSamples); // Encode to ADPCM var encodeParams = new GcAdpcmParameters { SampleCount = pcmSamples.Length, History1 = 0, History2 = 0 }; byte[] adpcmData = GcAdpcmEncoder.Encode(pcmSamples, coefficients, encodeParams); // Create a channel object var channel = new GcAdpcmChannel(adpcmData, coefficients, pcmSamples.Length); // Decode ADPCM back to PCM var decodeParams = new GcAdpcmParameters { SampleCount = pcmSamples.Length }; short[] decodedPcm = GcAdpcmDecoder.Decode(adpcmData, coefficients, decodeParams); // Build a complete GC-ADPCM format with multiple channels var channels = new GcAdpcmChannel[] { channel }; var gcAdpcmFormat = new GcAdpcmFormat(channels, 44100); // Set loop with context preservation var builder = gcAdpcmFormat.GetCloneBuilder() .WithLoop(true, 0, pcmSamples.Length - 1) .WithAlignment(14336); // Frame-aligned loop GcAdpcmFormat loopedFormat = builder.Build(); ``` -------------------------------- ### CRI ADX Encoding and Decoding Source: https://context7.com/thealexbarney/vgaudio/llms.txt Handle encrypted and unencrypted ADX files with automatic key detection and support for custom encryption keys. ```csharp using VGAudio.Codecs.CriAdx; using VGAudio.Containers.Adx; using VGAudio.Formats; // Read ADX file (automatic decryption if key is found) var adxReader = new AdxReader(); AudioData audio = adxReader.Read("encrypted.adx"); // Read with explicit decryption key var keyedReader = new AdxReader { EncryptionKey = new CriAdxKey(0x7F4551499DF55E68) // 64-bit key code }; AudioData decryptedAudio = keyedReader.Read("encrypted.adx"); // Write ADX with encryption var adxWriter = new AdxWriter(); var adxConfig = new AdxConfiguration { Type = CriAdxType.Linear, // Linear or Exponential ADPCM FrameSize = 18, // Bytes per frame (default: 18) EncryptionKey = new CriAdxKey("keystring"), // String-based key EncryptionType = 8 // Encryption revision }; adxWriter.WriteToFile(audio, "output.adx", adxConfig); // Create key from different sources var keyFromCode = new CriAdxKey(0x7F4551499DF55E68); var keyFromString = new CriAdxKey("mypassword"); var keyFromSeed = new CriAdxKey(0x1234, 0x5678); // Multiplier and increment ``` -------------------------------- ### ATRAC9 Decoding Source: https://context7.com/thealexbarney/vgaudio/llms.txt Decode ATRAC9 audio streams and convert them to WAV format. ```csharp using VGAudio.Codecs.Atrac9; using VGAudio.Containers.At9; using VGAudio.Formats; // Read AT9 file var at9Reader = new At9Reader(); AudioData audio = at9Reader.Read("audio.at9"); // Convert to WAV for playback var waveWriter = new WaveWriter(); waveWriter.WriteToFile(audio, "output.wav"); // Low-level ATRAC9 decoding byte[] configData = new byte[4] { /* config bytes from AT9 header */ }; var decoder = new Atrac9Decoder(); decoder.Initialize(configData); // Decode a superframe byte[] atrac9Data = new byte[decoder.Config.SuperframeBytes]; short[][] pcmOut = new short[decoder.Config.ChannelCount][]; for (int i = 0; i < decoder.Config.ChannelCount; i++) { pcmOut[i] = new short[decoder.Config.SuperframeSamples]; } decoder.Decode(atrac9Data, pcmOut); // Access decoder configuration Console.WriteLine($"Channels: {decoder.Config.ChannelCount}"); Console.WriteLine($"Sample Rate: {decoder.Config.SampleRate}"); Console.WriteLine($"Frame Samples: {decoder.Config.FrameSamples}"); Console.WriteLine($"Superframe Samples: {decoder.Config.SuperframeSamples}"); ``` -------------------------------- ### CRI HCA Encoding and Decoding Source: https://context7.com/thealexbarney/vgaudio/llms.txt Read, write, and perform low-level encoding of HCA audio files with support for quality settings and encryption. ```csharp using VGAudio.Codecs.CriHca; using VGAudio.Containers.Hca; using VGAudio.Formats; using VGAudio.Formats.CriHca; // Read an HCA file var hcaReader = new HcaReader(); hcaReader.Decrypt = true; // Auto-decrypt if encrypted AudioData audio = hcaReader.Read("input.hca"); // Write HCA with quality settings var hcaWriter = new HcaWriter(); var hcaConfig = new HcaConfiguration { Quality = CriHcaQuality.High, // Highest, High, Middle, Low, Lowest Bitrate = 256000, // Optional explicit bitrate LimitBitrate = true, // Prevent bitrate going too low EncryptionKey = null // Optional: CriHcaKey for encryption }; hcaWriter.WriteToFile(audio, "output.hca", hcaConfig); // Low-level HCA encoding var encoderParams = new CriHcaParameters { ChannelCount = 2, SampleRate = 48000, SampleCount = 480000, Quality = CriHcaQuality.High, Looping = true, LoopStart = 48000, LoopEnd = 432000 }; CriHcaEncoder encoder = CriHcaEncoder.InitializeNew(encoderParams); // Encode frames short[][] pcmFrame = new short[2][]; pcmFrame[0] = new short[1024]; pcmFrame[1] = new short[1024]; byte[] hcaFrame = new byte[encoder.FrameSize]; int framesEncoded = encoder.Encode(pcmFrame, hcaFrame); // Get any pending frames while (encoder.PendingFrameCount > 0) { byte[] pendingFrame = encoder.GetPendingFrame(); } ``` -------------------------------- ### Decode ATRAC9 Superframe Source: https://github.com/thealexbarney/vgaudio/blob/master/docs/vgaudio/Atrac9Decoder.md Decode a single superframe of ATRAC9 data into a PCM buffer. Ensure input and output buffers are correctly sized based on decoder configuration. ```csharp decoder.Decode(atrac9Buffer, pcmBuffer); ``` -------------------------------- ### ATRAC9 Band Extension Mode 4 Scaling Source: https://github.com/thealexbarney/vgaudio/blob/master/docs/audio-formats/atrac9/band-extension.md Mode 4 scales spectral coefficients using values from a lookup table. This mode is used when the frame has fewer than 8 stored bands. ```csharp Group_A_Scale = x * 0.7079468; Group_B_Scale = x * 0.5011902; Group_C_Scale = x * 0.3548279; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.