Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Currently working on project that has a byte array to store audio data(pcm). When I alter the array the array remains unchanged.

The code used.
private byte[] rawAudio;

When I use a function inside class for reversing the array the array remains unchanged.

The code used to alter the rawAudio array
C#
Public void Reverse()
{
    int i = 0;
    int j = rawAudio.Lenght - 1;
    While(i < j)
    {
         byte temp = rawAudio[i];
         rawAudio[i] = rawAudio[j];
         rawAudio[j] = temp;
         i++;
         j--;
    }
}


It seems that any manipulation on the array leaves the array unchanged. What could be the reason the array doesn't change although I do in fact alter the array.

What I have tried:

I've tried to alter the array as well as use Array Reverse with the same issue of array not being updated.
Posted
Updated 18-Dec-23 20:16pm
v2
Comments
Richard MacCutchan 18-Dec-23 12:46pm    
What you describe is impossible, there must be something else happening. Use the debugger to trace through that process and you should see the actual changes.
[no name] 18-Dec-23 12:50pm    
There's "nothing" in rawAudio to start with.
charles henington 18-Dec-23 13:01pm    
There is 6.7mb stored in rawaudio. I have determined that the raw audio is in fact reversed and converts it to a backwards wave file using Naudio but when converted to mp3 the audio is same as original (not backwards)
Dave Kreskowiak 18-Dec-23 13:13pm    
The array is in fact being reversed. It's your evaluation process that checks the array that is flawed, whatever that process is. Perhaps you're not writing the data back to what you think you're writing it to?
charles henington 18-Dec-23 13:31pm    
Whatever reason, converting the pcm to wave and then to mp3 seemed to be the issue. I'm now converting the pcm directly to mp3 and is working as expected.

Why do you reinvent the wheel?

There already is a method which is able to reverse an array.

A documentation as well as an example can be found here:
Array.Reverse Method (System) | Microsoft Learn[^]
 
Share this answer
 
Comments
charles henington 5-Feb-24 15:55pm    
Array.Reverse takes to long. Using a while loop takes only a fraction of the time.
TheRealSteveJudge 6-Feb-24 1:11am    
Thank you for the hint. I will give it a try!
charles henington 6-Feb-24 9:36am    
Test it out and let me know your test results. Tested this on windows 10 running under visual studio 2022 and it seems Array.Reverse was faster by about 25ms, but on ubuntu 22.04.3 LTS running under monodevelop 7.8.4(build 2) the while loop was faster by about 6,740 ms. The current build is being done on Ubuntu as the alaw format is natively supported as it is not on windows although it can be played using ffplay on windows.
TheRealSteveJudge 7-Feb-24 5:29am    
I did what you asked for:
Windows 10, VS2022, .NET8, 64 bit, 10 runs

byte array size 1GB filled with random data

DEBUG

Run 1
While Loop: Ellapsed time 2688 ms.
Array.Reverse: Ellapsed time 575 ms.
Run 2
While Loop: Ellapsed time 2774 ms.
Array.Reverse: Ellapsed time 642 ms.
Run 3
While Loop: Ellapsed time 2574 ms.
Array.Reverse: Ellapsed time 609 ms.
Run 4
While Loop: Ellapsed time 2552 ms.
Array.Reverse: Ellapsed time 609 ms.
Run 5
While Loop: Ellapsed time 2553 ms.
Array.Reverse: Ellapsed time 613 ms.
Run 6
While Loop: Ellapsed time 2545 ms.
Array.Reverse: Ellapsed time 295 ms.
Run 7
While Loop: Ellapsed time 2557 ms.
Array.Reverse: Ellapsed time 287 ms.
Run 8
While Loop: Ellapsed time 2544 ms.
Array.Reverse: Ellapsed time 284 ms.
Run 9
While Loop: Ellapsed time 2623 ms.
Array.Reverse: Ellapsed time 656 ms.
Run 10
While Loop: Ellapsed time 2702 ms.
Array.Reverse: Ellapsed time 637 ms.

RELEASE

Run 1
While Loop: Ellapsed time 546 ms.
Array.Reverse: Ellapsed time 554 ms.
Run 2
While Loop: Ellapsed time 572 ms.
Array.Reverse: Ellapsed time 616 ms.
Run 3
While Loop: Ellapsed time 652 ms.
Array.Reverse: Ellapsed time 597 ms.
Run 4
While Loop: Ellapsed time 622 ms.
Array.Reverse: Ellapsed time 600 ms.
Run 5
While Loop: Ellapsed time 615 ms.
Array.Reverse: Ellapsed time 595 ms.
Run 6
While Loop: Ellapsed time 617 ms.
Array.Reverse: Ellapsed time 294 ms.
Run 7
While Loop: Ellapsed time 588 ms.
Array.Reverse: Ellapsed time 289 ms.
Run 8
While Loop: Ellapsed time 539 ms.
Array.Reverse: Ellapsed time 287 ms.
Run 9
While Loop: Ellapsed time 536 ms.
Array.Reverse: Ellapsed time 285 ms.
Run 10
While Loop: Ellapsed time 548 ms.
Array.Reverse: Ellapsed time 306 ms.
charles henington 7-Feb-24 8:39am    
Seems there's a bigger gain using the while loop with Linux vs Windows.
You have a typo in your code which results in nothing happening as you are not doing error checking in your code and most probably did not use the debugger -
C#
int j = rawAudio.Lenght - 1; //error here

'Lenght should be 'Length'

To handle errors your code should look something like -
C#
public void Reverse()
{
    if (rawAudio == null)
    {
        //Handle the case if rawAudio is null...
        Console.WriteLine("Error: My rawAudio array is null.");
        return;
    }

    int length = rawAudio.Length;

    if (length == 0)
    {
        //Handle the case if your rawAudio is an empty array...
        Console.WriteLine("Error: My rawAudio array is empty.");
        return;
    }

    int i = 0;
    int j = length - 1;

    while (i < j)
    {
        byte temp = rawAudio[i];
        rawAudio[i] = rawAudio[j];
        rawAudio[j] = temp;
        i++;
        j--;
    }
}
 
Share this answer
 
Comments
charles henington 6-Feb-24 9:53am    
This was typed from my win 10 machine and wasn't an exact copy which is the reason for the typos, my apologies but that mistake would have been displayed in the error list window. I did in fact mark this as accepted for the handling of rawAudio being null

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900