Inspire92's picture

Background Music

Hello everyone,

I want to know how to reproduce a background sound continuously while drawing things on screen.
I am using the example provided by OpenTK for AUDIO (playback) in a custom class:

using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using System;
using System.IO;
 
namespace Visual3D_CSHARP
{
    struct PlaybackFile {
        public int source;
        public int buffer;
    } 
 
    public static class Playback
    {
        private static String backgroundSoundFileName = "the_ring_that_fell.wav";
        private static PlaybackFile BGMusic = new PlaybackFile();
 
        private static int state;
        private static int channels, bits, rate;
        private static byte[] data;
 
        private static AudioContext context = new AudioContext();
 
        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.");
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        public static void Intialize()
        {
            using (context)
            {
                BGMusic.buffer = AL.GenBuffer();
                BGMusic.source = AL.GenSource();
 
                data = Playback.LoadWave(File.Open("Audio/" + backgroundSoundFileName, FileMode.Open), out channels, out bits, out rate);
 
                AL.BufferData(BGMusic.buffer, GetSoundFormat(channels, bits), data, data.Length, rate);
                //AL.Source(BGMusic.source, ALSourceb.Looping, true);
                //AL.Source(BGMusic.source, ALSourcef.ConeOuterGain, 1.0f);
                AL.Source(BGMusic.source, ALSourcei.Buffer, BGMusic.buffer);
 
                AL.SourcePlay(BGMusic.source);
 
 
                do
                {
 
                    AL.GetSource(BGMusic.source, ALGetSourcei.SourceState, out state);
                }
                while ((ALSourceState)state == ALSourceState.Playing);
 
                PlayNormalBackgroundMusic();
            }         
        }
        /// <summary>
        /// 
        /// </summary>
        public static void PlayNormalBackgroundMusic()
        {
            AL.GetSource(BGMusic.source, ALGetSourcei.SourceState, out state);
            //AL.SourcePlay(BGMusic.source);
        }
    }
}

I've tried to call...

AL.GetSource(BGMusic.source, ALGetSourcei.SourceState, out state);

... on a timer but it doesnt make any sound.

If I use:

do
 {
  Thread.Sleep(250);
  Trace.Write(".");
  AL.GetSource(source, ALGetSourcei.SourceState, out state);
}
while ((ALSourceState)state == ALSourceState.Playing);

The sound works ok, still, it doesnt fit because I want to it to be like a background music that plays while other things are being drawn.

Thanks for the support.