Recursive AI

Convert audio files

All oneaudio recipes

Convert MP3, Ogg Vorbis, Opus, FLAC and WAV files to WAV, FLAC or Ogg Opus in C# with oneaudio, change the sample rate and channels on the way, and convert streams in memory.

C#
using OneAudio;
 
// Every format can be read; WAV, FLAC and Ogg Opus can be written.
string[] inputs = ["music.mp3", "music.ogg", "music.opus", "music.flac", "music.wav"];
foreach (string input in inputs)
{
    string ext = Path.GetExtension(input).TrimStart('.');
    string output = $"music-from-{ext}.flac";
    TranscodeResult r = Audio.Transcode(input, output);
    long size = new FileInfo(output).Length;
    Console.WriteLine($"{input,-10} -> {output,-20} {r.Frames,7} frames, " +
                      $"{r.Input.SampleRate} Hz, {size,7:N0} bytes");
}
 
// Convert rate and channels on the way: a 16 kHz mono line to 48 kHz stereo.
var options = new TranscodeOptions { SampleRate = 48000, Channels = 2 };
TranscodeResult voice = Audio.Transcode("voice.wav", "voice.opus", options);
Console.WriteLine($"voice.opus: {voice.Input.Channels} ch at {voice.Input.SampleRate} Hz, " +
                  $"{voice.Frames} frames");
 
// Streams have no file name, so the format must be given.
using (var input = File.OpenRead("music.ogg"))
using (var output = new MemoryStream())
{
    Audio.Transcode(input, output, new TranscodeOptions { Format = AudioFileFormat.Wav });
    Console.WriteLine($"music.ogg to WAV in memory: {output.Length:N0} bytes");
}
 
// MP3, Vorbis and AAC can't be written.
try
{
    Audio.Transcode("music.wav", "music-out.mp3");
}
catch (ArgumentException e)
{
    Console.WriteLine(e.Message);
}
Text
music.mp3  -> music-from-mp3.flac  1411200 frames, 44100 Hz, 890,883 bytes
music.ogg  -> music-from-ogg.flac  1411200 frames, 44100 Hz, 867,043 bytes
music.opus -> music-from-opus.flac 1536000 frames, 48000 Hz, 998,012 bytes
music.flac -> music-from-flac.flac 1411200 frames, 44100 Hz, 813,193 bytes
music.wav  -> music-from-wav.flac  1411200 frames, 44100 Hz, 813,193 bytes
voice.opus: 2 ch at 48000 Hz, 289200 frames
music.ogg to WAV in memory: 5,644,880 bytes
Cannot tell the output format from the extension '.mp3'; use .wav, .flac or .opus, or set TranscodeOptions.Format. (Parameter 'path')

What can be written

FromTo WAVTo FLACTo Ogg Opus
Integer WAV, FLACBit-exact, same depthBit-exact, same depthLossy
Float WAVSame float type16-bit by defaultLossy
MP3, Ogg Vorbis, Ogg Opus16-bit PCM by default16-bit by defaultLossy, again

Bit-exact holds when nothing is converted. A change of rate or channels goes through floats, and converted 24 and 32-bit audio is written to FLAC at 24 bits.

  • The output format comes from the extension: .wav or .wave, .flac, and .opus, .ogg or .oga for Ogg Opus. Set TranscodeOptions.Format to choose it yourself. A stream has no extension, so it needs Format.
  • There’s no MP3, Vorbis or AAC encoder. An .mp3 output path throws ArgumentException, and .ogg writes Opus, not Vorbis.
  • SampleRate and Channels in TranscodeOptions convert on the way. Mono becomes two or more channels as a copy on the front pair. Other changes fold speakers down: the center goes into left and right at −3 dB, and stereo to mono takes each side at −6 dB. Folded sums aren’t normalized, so loud multichannel audio can pass full scale.
  • The conversion streams block by block, so memory use doesn’t depend on the length. If it fails, the partial output file is deleted.
  • music.opus converts at 48 kHz, the rate Opus decodes at. Add SampleRate = 44100 to get the source rate back.
  • music-from-wav.flac and music-from-flac.flac are byte-identical: the same samples and the same settings give the same bytes.

The encoder settings are TranscodeOptions.Wav, Flac and Opus. See Read and write WAV, Read and write FLAC and Encode Opus.

When a managed library can replace FFmpeg, with ffmpeg commands mapped to C#: Audio in .NET without FFmpeg.