snocrash's picture

OpenAL 28 Source limit?

I'm making a drum machine style program and I seem to have hit a limit of 28 simultaneous sources... I'm not sure what the problem is, but i'm sure its possible to get more then 24 sources going... Maybe someone can spot something obvious im doing wrong.

the code:

namespace db101 {
    class DrumBox : GameWindow {
        public DrumBox()
            : base(1024, 768, GraphicsMode.Default, "db-101")
        {
            VSync = VSyncMode.On;
        }
        public override void OnLoad(EventArgs e)
        {
            GL.ClearColor(System.Drawing.Color.Black);
            GL.Enable(EnableCap.DepthTest);
 
        }
        protected override void OnResize(ResizeEventArgs e)
        {
            GL.Viewport(0, 0, Width, Height);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Glu.Ortho2D(0.0, Width, Height, 0.0);
        }
 
 
        AudioContext context = new AudioContext();
        int iPos = 0;
        int numSteps = 64;
        static int MAXTRACKS = 56;
        int numTracks = MAXTRACKS;
        AudioReader[] sound = new AudioReader[MAXTRACKS];
        int[] buffer = new int[MAXTRACKS];
        int[] source = new int[MAXTRACKS];
        string[] filenames = new string[]{
            "909kick.wav", "606snareacc.wav", "CH.wav", "tb303bass18.wav", 
            "chh-housemusique30.wav", "chh-housemusique26.wav", 
            "chh-housemusique28.wav","07hhc120_holophone_s HvS.wav", 
            "03hhc120_naroctic_s HvS.wav", "Bongo.wav", "HOUSE KICK 04.wav", "rimshot2.wav", 
            "photon.wav", "53 808Marc.wav", "808Clave(75).wav", 
            "808Rim(37).wav", "Bongo(39).wav", "hatz2.wav", 
            "hatz7.wav", "J1i.wav", "Klick 03 57-03.wav", 
            "Klick 05 57-05.wav", "Klick 06 57-06.wav", "Klick 08 57-08.wav",
            "Klick 10 57-10.wav", "Klick 11 57-11.wav", "One Shots Percussion 73-04.wav", 
            "Rim 05.wav", "RIM.wav", "snare 16.wav", 
            "snare 18.wav", "Synth Maraca 03 OS.wav", "Tap Kick 55-10.wav",
            "pop.wav", "drip.wav", "Siren.wav", 
            "Laser.wav", "doubclick_mouse.wav", "Effec016.wav",
            "Ddm-110 chh.wav", "02hhc120_freeze time_s HvS.wav", "05hhc120_paralyzed_s HvS.wav", 
            "02sde120_pulverize_s HvS.wav", "78 closed hat.wav", "Bongo 2.wav", 
            "hatz3.wav", "Ueb HM chh-hihat01_oriental.wav", "Klick 12 57-12.wav", 
            "shaker.wav",  "Synth Maraca 02 OS.wav", "st-01_slapbass.wav",
            "sine.wav", "sine.wav", "sine.wav", 
            "sine.wav", "sine.wav" };
 
        float[,] buttons = new float[(64 * 56), 6];
        float butSize = 768/72.0f;
        public override void OnUpdateFrame(UpdateFrameEventArgs e)
        {
 
            for (int iTrack = 0; iTrack < numTracks; iTrack++)
            {
                if (buttons[iPos + (iTrack * numSteps), 4] == 1.0f)
                {
                    int currentSource = (int)buttons[iPos + (iTrack * numSteps), 5];
                    float t1 = 0.9f;//(Mouse.Y)*0.005f;
                    AL.Source(source[currentSource], ALSourcef.Pitch, t1);
                  //  AL.Source(source[currentSource], ALSourcef.Gain, Mouse.Y);
                    AL.SourcePlay(source[currentSource]);
                }
            }
 
            iPos++;
            if (iPos > numSteps-1)
            {
                // AL.Source(source, ALSourceb.Looping, true); // source loops infinitely
                //AL.Source(source, ALSourcef.Gain, 0.1f);
                iPos = 0;
            }
 
 
            if (Keyboard[Key.Escape])
                Exit();
        }
        public override void OnRenderFrame(RenderFrameEventArgs e)
        {
            if (Mouse[MouseButton.Left])
            {
                for (int iBut = 0; iBut < buttons.GetLength(0); iBut++)
                {
                    if (Mouse.X > buttons[iBut, 0] && Mouse.X < buttons[iBut, 0] + buttons[iBut, 2] &&
                        Mouse.Y > buttons[iBut, 1] && Mouse.Y < buttons[iBut, 1] + buttons[iBut, 2])
                    {
 
                        buttons[iBut, 4] = 1.0f;
                    }
                }
            }
            if (Mouse[MouseButton.Right])
            {
                for (int iBut = 0; iBut < buttons.GetLength(0); iBut++)
                {
                    if (Mouse.X > buttons[iBut, 0] && Mouse.X < buttons[iBut, 0] + buttons[iBut, 2] &&
                        Mouse.Y > buttons[iBut, 1] && Mouse.Y < buttons[iBut, 1] + buttons[iBut, 2])
                    {
                        buttons[iBut, 4] = 0.75f;
                    }
                }
            }
            GL.Clear(ClearBufferMask.ColorBufferBit |
                     ClearBufferMask.DepthBufferBit);
            GL.PushMatrix();
           // GL.Translate(100.0, 100.0, 0.0);
            GL.Begin(BeginMode.Quads);
            for (int iBut = 0; iBut < buttons.GetLength(0); iBut++){
 
                GL.Color3(buttons[iBut, 3], buttons[iBut, 3], buttons[iBut, 3]);
                GL.Vertex2(buttons[iBut, 0], buttons[iBut, 1]);
                GL.Color3(buttons[iBut, 4], buttons[iBut, 4], buttons[iBut, 4]);
                GL.Vertex2(buttons[iBut, 0] + buttons[iBut, 2], buttons[iBut, 1]);
                GL.Color3(buttons[iBut, 4], buttons[iBut, 4], buttons[iBut, 4]);
                GL.Vertex2(buttons[iBut, 0] + buttons[iBut, 2], buttons[iBut, 1] + buttons[iBut, 2]);
                GL.Color3(buttons[iBut, 4], buttons[iBut, 4], buttons[iBut, 4]);
                GL.Vertex2(buttons[iBut, 0], buttons[iBut, 1] + buttons[iBut, 2]);
 
 
                if (Mouse.X > buttons[iBut, 0] && Mouse.X < buttons[iBut, 0] + buttons[iBut, 2] &&
                     Mouse.Y > buttons[iBut, 1] && Mouse.Y < buttons[iBut, 1] + buttons[iBut, 2])
                {
                    if (buttons[iBut, 4]!=1.0f)
                        buttons[iBut, 4] = 0.75f;
                }
                else if (buttons[iBut, 4]!=1.0f)
                {
                    buttons[iBut, 4] = 0.5f;
                }
 
            }
            GL.End();
            GL.PopMatrix();
 
            SwapBuffers();
        }
        public static void Main()
        {
            using (DrumBox drumBox = new DrumBox())
            {
                for (int i = 0; i < drumBox.numTracks; i++)
                {
                    drumBox.source[i] = AL.GenSource();
                    drumBox.buffer[i] = AL.GenBuffer();
 
                    drumBox.sound[i] = new AudioReader(Path.Combine(Path.Combine("Data", "Audio"), drumBox.filenames[i]));
                    if(i==0)
                        AL.BufferData(drumBox.buffer[i], drumBox.sound[i].ReadSamples(20000));//
                    else
                        AL.BufferData(drumBox.buffer[i], drumBox.sound[i].ReadSamples(3000));//
 
                    AL.Source(drumBox.source[i], ALSourcei.Buffer, drumBox.buffer[i]);
                }
                float leftMargin = 256.0f;
                float x = leftMargin;
 
                float y = 64.0f;
                int steps = 0;
                int halfbar = 0;
                int iTracks = 0;
                int iCols = 0;
                int iBut = 0;
 
                while (iTracks < drumBox.numTracks)
                {
                    drumBox.buttons[iBut, 0] = x;
                    drumBox.buttons[iBut, 1] = y;
                    drumBox.buttons[iBut, 2] = drumBox.butSize;
                    drumBox.buttons[iBut, 3] = 0.75f;
                    drumBox.buttons[iBut, 4] = 0.5f;
                    drumBox.buttons[iBut, 5] = iTracks;
                    steps++;
 
                    if (steps == 4)
                    {
                        halfbar++;
                        if (halfbar == 4)
                        {
                            x += drumBox.butSize + 3.0f;
                            halfbar = 0;
                        }
                        else
                        {
                            x += drumBox.butSize + 2.0f;
                        }
                        steps = 0;
                    }
                    else
                    {
                        x += drumBox.butSize + 1.0f;
                    }
                    iCols++;
                    if (iCols > drumBox.numSteps - 1)
                    {
                        y += (drumBox.butSize * 1) + 1.0f;
                        x = leftMargin;
                        iCols = 0;
                        iTracks++;
                    }
                    iBut++;
                }
                drumBox.Run(6.0, 0.0);
            }
        }//end main
        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }
    }
}

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Do you get some kind of error, or do new sources simply stop playing?

The maximum number of simultaneous sources depends on the sound card and its drivers. As far as I know, the only reliable way to find out this limit is to create new sources in a loop until it fails. It is also recommended to reuse sources whenever possible (e.g. when a track is idle).

You could also try using OpenAL Soft (a software implementation of OpenAL) and see if it has different limits.

snocrash's picture

Not getting any error. But any sources above 28 don't emit any sound.