Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:51:44+00:00 2026-05-26T03:51:44+00:00

I need fastest way to convert byte array to short array of audio data.

  • 0

I need fastest way to convert byte array to short array of audio data.

Audio data byte array contains data from two audio channels placed in such way:

C1C1C2C2 C1C1C2C2 C1C1C2C2 ...

where

C1C1 - two bytes of first channel

C2C2 - two bytes of second channel

Currently I use such algorithm, but I feel there is better way to perform this task.

byte[] rawData = //from audio device
short[] shorts = new short[rawData.Length / 2];
short[] channel1 = new short[rawData.Length / 4];
short[] channel2 = new short[rawData.Length / 4];
System.Buffer.BlockCopy(rawData, 0, shorts, 0, rawData.Length);
for (int i = 0, j = 0; i < shorts.Length; i+=2, ++j)
{
    channel1[j] = shorts[i];
    channel2[j] = shorts[i+1];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:51:45+00:00Added an answer on May 26, 2026 at 3:51 am

    You could use unsafe code to avoid array addressing or bit shifts. But as PVitt said on new PCs you are better using standard managed code and the TPL if your data size is important.

    short[] channel1 = new short[rawData.Length / 4];
    short[] channel2 = new short[rawData.Length / 4];
    
    fixed(byte* pRawData = rawData)
    fixed(short* pChannel1 = channel1)
    fixed(short* pChannel2 = channel2)
    {
        byte* end = pRawData + rawData.Length;
        while(pRawData < end)
        {
            (*(pChannel1++)) = *((short*)pRawData);
            pRawData += sizeof(short);
            (*(pChannel2++)) = *((short*)pRawData);
            pRawData += sizeof(short);
        }
    }
    

    As with all optimization problems you need to time carefully, pay special attention to your
    buffer allocations, channel1 and channel2 could be static (big) buffers growing automatically
    and you could use only the nth first bytes. You will be able to skip 2 big arrays allocations
    for each executions of this function. and will make the GC work less
    (always better when timing is important)

    As noted by CodeInChaos the endianness could be important, if your data is not in the
    correct endianness you will need to do the conversion, for example to convert between big
    and little endian assuming 8bit atomic elements the code will look like :

    short[] channel1 = new short[rawData.Length / 4];
    short[] channel2 = new short[rawData.Length / 4];
    
    fixed(byte* pRawData = rawData)
    fixed(byte* pChannel1 = (byte*)channel1)
    fixed(byte* pChannel2 = (byte*)channel2)
    {
        byte* end = pRawData + rawData.Length;
        byte* pChannel1High = pChannel1 + 1;
        byte* pChannel2High = pChannel2 + 1;
    
        while(pRawData < end)
        {
            *pChannel1High = *pRawData;
            pChannel1High += 2 * sizeof(short);
    
            *pChannel1 = *pRawData;
            pChannel1 += 2 * sizeof(short);
    
            *pChannel2High = *pRawData;
            pChannel2High += 2 * sizeof(short);
    
            *pChannel2 = *pRawData;
            pChannel2 += 2 * sizeof(short);
        }
    }
    

    I didn’t compile any code in this post with an actual compiler so if you find errors feel free to edit it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need fastest way to convert files from latin1 to utf-8 in python. The
What is the fastest way to copy data from array b to array a,
What is the fastest way of transferring few thousand rows of data from one
Which is the fastest way to convert a byte[] to float[] and vice versa
I need fastest way to find the values in array A that their mods
What is the fastest way to load data from flatfiles into a MySQL database,
In a recent question I asked about the fastest way to convert a large
I need the fastest way to periodically sync file with memory. What I think
I need fastest way to transfer bitmap object over the lan in C#. Abdul
I need the fastest way to see if an ip address is reachable. On

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.