0

Okay couldn´t come up with a good topic.

The point is, i have a code which i got help with that´s changing the bit depth from 32bit to 16bit, not sure how it works, but i think it simply just removes every other bit, nothing fancy.

So if it´s possible to do that with the code, it should be possible to change the samplerate. Also i don´t want something fancy here as it´s more for debugging uses, i just want to make it into some other samplerate, it doesn´t have to be a "resampling".

Meaning for example, if the audio is 48khz, and i resample it while it plays to 47khz, it won´t sound the same as it still think it´s 48khz, meaning it will play it slower.

So in other words i am going to use it for changing the speed of the audio in a very simple way.

here is the code to change bit depth which i hope can be reused for this.

Int32 bytesrecorded = e.BytesRecorded;
byte[] newArray16Bit = new byte[bytesrecorded / 2];
fixed (byte* sourcePtr = e.Buffer)
fixed (byte* targetPtr = newArray16Bit)
{
    float* sourceTyped = (float*)sourcePtr;
    short* targetTyped = (short*)targetPtr;

    Int32 count = bytesrecorded / 4;
    for (int i = 0; i < count; i++)
    {
        targetTyped[i] = (Int16)(sourceTyped[i] * Int16.MaxValue);
    }
}

e.Bytesrecorded = the size of the audio buffer,

e.Buffer = the actual buffer

As i don´t fully understand this, i simply look at the /2 and assume it just divides teh length of the size, then the /4 part will be moving the bytes around to make it fit.

Though i don´t know how i can use this to change the samplerate, as this is just bytes moving around, it shouldn´t be to hard to make something for the samplerate.

Thanks

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Zerowalker
  • 761
  • 2
  • 8
  • 27
  • Changing bitdepth and sample rate are radically different processes. This code won't help you. – Bjorn Roche Dec 15 '13 at 14:35
  • Okay, well good to know that. Isn´t there a similar approach? As i just want to decrease the samplerate, which would mean, remove some bytes somehow. – Zerowalker Dec 15 '13 at 19:02
  • No, you are not just removing bytes. I discuss the two in detail in my answer to this question: http://stackoverflow.com/questions/15087668/how-to-convert-pcm-samples-in-byte-array-as-floating-point-numbers-in-the-range – Bjorn Roche Dec 15 '13 at 19:58

1 Answers1

0

If you want to adjust the speed, there are a few possible solutions:

  1. Use a resampler to resample your track from e.g. 44100Hz to 50000Hz but still tell the audio driver to play it with 44100Hz.
  2. Simply change the samplerate of the file but not the content of the file. For example modify the wave-header and change the samplerate. Audioplayers will read the samplerate of the wave-header and will play your 44100Hz track with a samplerate of e.g. 60000Hz.
  3. Use a professional dsp algorithm to adjust the speed.
Florian
  • 5,918
  • 3
  • 47
  • 86