Skip to main content
Rollback to Revision 2
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

UPDATE

Here is the new code, three different for loops, and i tried to shorten it as much as possible, and prevent unnecessary copyback methods.

        //For Loop Conversions
        if (MixMono) //For Loop for Downmixing to Mono
        for (int i = 0; i < bufferSize; i++)            
        {
            Test[Tindex] = ((mixBuffer[i] + mixBuffer[i++]) * 32768.0f) / 2;
            Tindex++;
        }
        if (left && !MixMono) //For Loop for Getting Left Channel
        for (int i = 0; i < bufferSize; i + 2)          
        {
            Test[Tindex] = (mixBuffer[i] * 32768.0f);
            Tindex++;
        }

        if (!left && !MixMono) //For Loop for BitDepth Conversion
        for (int i = 0; i < bufferSize; i ++)           
        {
            Test[i] = (mixBuffer[i] * 32768.0f); //32bit Float to 16Bit Int
        }
        //End of For Loop Conversions

UPDATE

Here is the new code, three different for loops, and i tried to shorten it as much as possible, and prevent unnecessary copyback methods.

        //For Loop Conversions
        if (MixMono) //For Loop for Downmixing to Mono
        for (int i = 0; i < bufferSize; i++)            
        {
            Test[Tindex] = ((mixBuffer[i] + mixBuffer[i++]) * 32768.0f) / 2;
            Tindex++;
        }
        if (left && !MixMono) //For Loop for Getting Left Channel
        for (int i = 0; i < bufferSize; i + 2)          
        {
            Test[Tindex] = (mixBuffer[i] * 32768.0f);
            Tindex++;
        }

        if (!left && !MixMono) //For Loop for BitDepth Conversion
        for (int i = 0; i < bufferSize; i ++)           
        {
            Test[i] = (mixBuffer[i] * 32768.0f); //32bit Float to 16Bit Int
        }
        //End of For Loop Conversions
Update, new Code
Source Link
Zerowalker
  • 963
  • 14
  • 22

UPDATE

Here is the new code, three different for loops, and i tried to shorten it as much as possible, and prevent unnecessary copyback methods.

        //For Loop Conversions
        if (MixMono) //For Loop for Downmixing to Mono
        for (int i = 0; i < bufferSize; i++)            
        {
            Test[Tindex] = ((mixBuffer[i] + mixBuffer[i++]) * 32768.0f) / 2;
            Tindex++;
        }
        if (left && !MixMono) //For Loop for Getting Left Channel
        for (int i = 0; i < bufferSize; i + 2)          
        {
            Test[Tindex] = (mixBuffer[i] * 32768.0f);
            Tindex++;
        }

        if (!left && !MixMono) //For Loop for BitDepth Conversion
        for (int i = 0; i < bufferSize; i ++)           
        {
            Test[i] = (mixBuffer[i] * 32768.0f); //32bit Float to 16Bit Int
        }
        //End of For Loop Conversions

UPDATE

Here is the new code, three different for loops, and i tried to shorten it as much as possible, and prevent unnecessary copyback methods.

        //For Loop Conversions
        if (MixMono) //For Loop for Downmixing to Mono
        for (int i = 0; i < bufferSize; i++)            
        {
            Test[Tindex] = ((mixBuffer[i] + mixBuffer[i++]) * 32768.0f) / 2;
            Tindex++;
        }
        if (left && !MixMono) //For Loop for Getting Left Channel
        for (int i = 0; i < bufferSize; i + 2)          
        {
            Test[Tindex] = (mixBuffer[i] * 32768.0f);
            Tindex++;
        }

        if (!left && !MixMono) //For Loop for BitDepth Conversion
        for (int i = 0; i < bufferSize; i ++)           
        {
            Test[i] = (mixBuffer[i] * 32768.0f); //32bit Float to 16Bit Int
        }
        //End of For Loop Conversions
deleted 165 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

For loop-loop for Mixing Audiomixing audio (Left Channelleft channel, Stereostereo to Monomono)

As the topic says, it'sThis is a very simple Forfor loop doing that, but iI find it very, complex looking, and-looking. I hope you can give me some insight on how it may be improved.

        int Tindex = 0;
        int16_t Test[960];
        int TestSize = SampleSize / 2; //Test is 16Bit, Half size of 32bit
        int TestChannels = 2; //Stereo
        float copy = 0; //Used to store Audio data used to Mixdown to Mono
        bool left = true; //Only use Left Channel
        bool MixMono = true; //MixMono has priority over left
        if (MixMono || left) //Change the approprite variables if it's Mono instead of Stereo
        {
            int16_t Test[960 / 2];
            TestSize = TestSize / 2; //If Mono is used, Half Channel/Size.
            TestChannels = 1; //Mono
        }
        int ChannelCheck = 0;

        for (int i = 0; i < bufferSize; i += 1)         //For Loop for Downmixing and BitDepth Conversion
        {
            int s = (micBuffer[i] * 32768.0f); //32bit Float to 16Bit Int
    
            ChannelCheck++;
            
            if (MixMono)//Mixdown Stereo to Mono
            {
                copy += s;
                if (ChannelCheck == 2)
                {
                    Test[Tindex] = copy / 2;
                    copy = 0;
                    Tindex++;
                    ChannelCheck = 0;
                }
                
            }
            else if (left)
            {
                if (ChannelCheck == 1)
                {
                    Test[Tindex] = s;
                    ChannelCheck -= 2;
                    Tindex++;
                }
            }
            else
            {
                Test[Tindex] = s;
                Tindex++;
            }
        }
        //End of For Loop

I tried commenting most stuff, so it should be self explained-explained.

PrettyIt's pretty much it's just, Mix Stereoa mix stereo to Mono = Combinemono. It combines 2 indexesindices from the floatfloat array (add them), then divide them (to keep the correct volume).

16bit Conversion,16-bit conversion just multiplymultiplies by Int16 Max,Max; not much there (Would. I would like to have some kind of dithering, so if anyone knowknows some easy references, please link, or give suggestions)offer them.

But as you can see, it's overly complicated with checks all over the place, i. I think it can be improved, but iI can't really figure out a good way to do it.

Any and all suggestions are appreciated!

For loop for Mixing Audio (Left Channel, Stereo to Mono)

As the topic says, it's a very simple For loop doing that, but i find it very, complex looking, and hope you can give me some insight on how it may be improved.

        int Tindex = 0;
        int16_t Test[960];
        int TestSize = SampleSize / 2; //Test is 16Bit, Half size of 32bit
        int TestChannels = 2; //Stereo
        float copy = 0; //Used to store Audio data used to Mixdown to Mono
        bool left = true; //Only use Left Channel
        bool MixMono = true; //MixMono has priority over left
        if (MixMono || left) //Change the approprite variables if it's Mono instead of Stereo
        {
            int16_t Test[960 / 2];
            TestSize = TestSize / 2; //If Mono is used, Half Channel/Size.
            TestChannels = 1; //Mono
        }
        int ChannelCheck = 0;

        for (int i = 0; i < bufferSize; i += 1)         //For Loop for Downmixing and BitDepth Conversion
        {
            int s = (micBuffer[i] * 32768.0f); //32bit Float to 16Bit Int
    
            ChannelCheck++;
            
            if (MixMono)//Mixdown Stereo to Mono
            {
                copy += s;
                if (ChannelCheck == 2)
                {
                    Test[Tindex] = copy / 2;
                    copy = 0;
                    Tindex++;
                    ChannelCheck = 0;
                }
                
            }
            else if (left)
            {
                if (ChannelCheck == 1)
                {
                    Test[Tindex] = s;
                    ChannelCheck -= 2;
                    Tindex++;
                }
            }
            else
            {
                Test[Tindex] = s;
                Tindex++;
            }
        }
        //End of For Loop

I tried commenting most stuff, so it should be self explained.

Pretty much it's just, Mix Stereo to Mono = Combine 2 indexes from the float array (add them), then divide them (to keep the correct volume).

16bit Conversion, just multiply by Int16 Max, not much there (Would like to have some kind of dithering, if anyone know some easy references, please link, or give suggestions)

But as you can see, it's overly with checks all over the place, i think it can be improved, but i can't really figure out a good way to do it.

Any and all suggestions are appreciated!

For-loop for mixing audio (left channel, stereo to mono)

This is a very simple for loop, but I find it very, complex-looking. I hope you can give me some insight on how it may be improved.

int Tindex = 0;
int16_t Test[960];
int TestSize = SampleSize / 2; //Test is 16Bit, Half size of 32bit
int TestChannels = 2; //Stereo
float copy = 0; //Used to store Audio data used to Mixdown to Mono
bool left = true; //Only use Left Channel
bool MixMono = true; //MixMono has priority over left
if (MixMono || left) //Change the approprite variables if it's Mono instead of Stereo
{
    int16_t Test[960 / 2];
    TestSize = TestSize / 2; //If Mono is used, Half Channel/Size.
    TestChannels = 1; //Mono
}
int ChannelCheck = 0;

for (int i = 0; i < bufferSize; i += 1)         //For Loop for Downmixing and BitDepth Conversion
{
    int s = (micBuffer[i] * 32768.0f); //32bit Float to 16Bit Int

    ChannelCheck++;
    
    if (MixMono)//Mixdown Stereo to Mono
    {
        copy += s;
        if (ChannelCheck == 2)
        {
            Test[Tindex] = copy / 2;
            copy = 0;
            Tindex++;
            ChannelCheck = 0;
        }
        
    }
    else if (left)
    {
        if (ChannelCheck == 1)
        {
            Test[Tindex] = s;
            ChannelCheck -= 2;
            Tindex++;
        }
    }
    else
    {
        Test[Tindex] = s;
        Tindex++;
    }
}
//End of For Loop

I tried commenting most stuff, so it should be self-explained.

It's pretty much a mix stereo to mono. It combines 2 indices from the float array (add them), then divide them (to keep the correct volume).

16-bit conversion just multiplies by Int16 Max; not much there. I would like to have some kind of dithering, so if anyone knows some easy references, please offer them.

But as you can see, it's overly complicated with checks all over the place. I think it can be improved, but I can't really figure out a good way to do it.

Source Link
Zerowalker
  • 963
  • 14
  • 22
Loading