How to Upload raw audio data to OpenAL

342 views Asked by At

Im now searching for hours to find some tutorials or documentation to upload RAW audio data (no matter if mp3, wav, mp4, ...) to OpenAL.

The Problem is: i made some 3D Gaming (and audio) with Python and also OpenGL and got some libs to work for me and manage all these data stuff for me.
Now im using OpenTK for C# and there is really NO documentation for this.
Neither nuget libs to get it managed like in my python program nor documentation to customize each audio format to upload data on my own.

My current code (which works for WAV audio) look like this:

public void Play()
{
        var device = ALC.OpenDevice(null);
        var context = ALC.CreateContext(device, new ALContextAttributes());
        ALC.MakeContextCurrent(context);
        int channels, bits_per_sample, sample_rate;
        byte[] soundData = LoadWave(
            File.Open(filename, FileMode.Open),
            out channels,
            out bits_per_sample,
            out sample_rate);
        ALFormat format = GetSoundFormat(channels, bits_per_sample);

        int bufferId = AL.GenBuffer();
        AL.BufferData(bufferId, format, soundData, bits_per_sample);
        // AL.Listener(ALListener3f.Position, 0.0f, 0.0f, 0.0f);
        // AL.Listener(ALListener3f.Velocity, 0.0f, 0.0f, 0.0f);

        int sourceId = AL.GenSource();
        // AL.Source(sourceId, ALSourcef.Gain, 1);
        // AL.Source(sourceId, ALSourcef.Pitch, 1);
        // AL.Source(sourceId, ALSource3f.Position, 0.0f, 0.0f, 0.0f);

        AL.Source(sourceId, ALSourcei.Buffer, bufferId);
        AL.SourcePlay(sourceId);
}

    // Loads a wave/riff audio file.
    public static byte[] LoadWave(Stream stream, out int channels, out int bits, out int rate)
    {
        if (stream == null)
            throw new ArgumentNullException("stream");

        using (BinaryReader reader = new BinaryReader(stream))
        {
            // RIFF header
            string signature = new string(reader.ReadChars(4));
            if (signature != "RIFF")
                throw new NotSupportedException("Specified stream is not a wave file.");

            int riff_chunck_size = reader.ReadInt32();

            string format = new string(reader.ReadChars(4));
            if (format != "WAVE")
                throw new NotSupportedException("Specified stream is not a wave file.");

            // WAVE header
            string format_signature = new string(reader.ReadChars(4));
            if (format_signature != "fmt ")
                throw new NotSupportedException("Specified wave file is not supported.");

            int format_chunk_size = reader.ReadInt32();
            int audio_format = reader.ReadInt16();
            int num_channels = reader.ReadInt16();
            int sample_rate = reader.ReadInt32();
            int byte_rate = reader.ReadInt32();
            int block_align = reader.ReadInt16();
            int bits_per_sample = reader.ReadInt16();

            string data_signature = new string(reader.ReadChars(4));
            if (data_signature != "data")
                throw new NotSupportedException("Specified wave file is not supported.");

            int data_chunk_size = reader.ReadInt32();

            channels = num_channels;
            bits = bits_per_sample;
            rate = sample_rate;

            return reader.ReadBytes((int)reader.BaseStream.Length);
        }
    }

    public static ALFormat GetSoundFormat(int channels, int bits)
    {
        switch (channels)
        {
            case 1: return bits == 8 ? ALFormat.Mono8 : ALFormat.Mono16;
            case 2: return bits == 8 ? ALFormat.Stereo8 : ALFormat.Stereo16;
            default: throw new NotSupportedException("The specified sound format is not supported.");
        }
    }

The loadWave method validates some details inside the raw byte file content to verify RIFF WAVE fmt and data signatures.

Can someone help me at this point?
How can I upload files like MP3 (no matter which format) and get it to work or where can i find useful documentation?

Maybe I "just" need this information to load to OpenAL:

  • where raw data is inside the file byte array
  • where to find information about ALFormat
  • where to find information about bits_per_sample

Really really thanks for your help!

1

There are 1 answers

0
Julius Häger On

The documentation you are looking for is https://www.openal.org/documentation/

This will tell you how to use OpenAL, and what to expect from the API. One such expectation you shouldn't have is that OpenAL will be able to play your mp3 file directly. There are extensions that allow for it, but not all drivers will support this.

I think you are looking for some kind of library for loading and decoding the "raw" audio data from these formats.