public static short DecodeAdpcm(byte input, ref AdpcmState state) { int index = state.index; int step = stepsizeTable[index]; int valpred = state.valprev; int delta = input; index += indexTable[delta]; if (index < 0) index = 0; if (index > 88) index = 88; bool sign = ((delta & 8) == 8); delta = delta & 7; int vpdiff = step >> 3; if ((delta & 4) == 4) vpdiff += step; if ((delta & 2) == 2) vpdiff += step >> 1; if ((delta & 1) == 1) vpdiff += step >> 2; if (sign) valpred -= vpdiff; else valpred += vpdiff; if (valpred > 32767) valpred = 32767; else if (valpred < -32768) valpred = -32768; state.valprev = (short) valpred; state.index = (byte) index; return (short) valpred; }
static int DecodeSample(int code, ref AdpcmState state) { int Delta, Result; Delta = state.StepSize >> 3; if ((code & 4) > 0) { Delta += state.StepSize; } if ((code & 2) > 0) { Delta += state.StepSize >> 1; } if ((code & 1) > 0) { Delta += state.StepSize >> 2; } if ((code & 8) > 0) { Delta = -Delta; } Result = state.Predictor + Delta; Result = Delimit(Result, short.MaxValue, short.MinValue); state.Index += kIndexTable[code]; state.Index = (sbyte)Delimit(state.Index, 88, 0); state.StepSize = kStepTable[state.Index]; state.Predictor = (short)Result; return(Result); }
/// <summary> /// Decodes the byte array of Xbox ADPCM blocks. Returns a two-dimensional short array of all the decoded samples. /// </summary> /// <param name="data">The byte array to decode.</param> /// <param name="startPos">The position in the byte array at which to start.</param> /// <param name="size">The size of data to decode in the byte array.</param> /// <param name="FChannels">The number of channels.</param> /// <returns>A two-dimensional short array of all the decoded samples in 16-bit PCM.</returns> private static short[,] DecodeMemory(byte[] data, int startPos, int size, int FChannels) { AdpcmState[] State = new AdpcmState[FChannels]; int blockCount = size / (XboxAdpcmChunkSize * FChannels); byte[] block = new byte[XboxAdpcmChunkSize * FChannels]; short[,] samples = new short[blockCount * 64, FChannels]; int sampleCount = 0; for (int x = 0; x < blockCount; x++) { if (MemCpy(data, ref block, XboxAdpcmChunkSize * FChannels, (x * XboxAdpcmChunkSize * FChannels) + startPos) < XboxAdpcmChunkSize * FChannels) { return(samples); } short[,] temp = DecodeBlock(block, 0, FChannels, ref State); for (int y = 0; y < 64; y++) { for (int chan = 0; chan < FChannels; chan++) { samples[sampleCount, chan] = temp[y, chan]; } sampleCount++; } } return(samples); }
static uint DecodeMemory(ushort[] input, uint length, ushort[] output, int channels) { AdpcmState[] adpcmState = new AdpcmState[channels]; short[][] buffers = new short[][] { new short[channels], new short[8] }; uint codeBuffer; int i, j, c, outLength; int inIndex = 0; int outIndex = 0; length = (uint)((length / kXboxAdpcmSrcSize) / channels); for (outLength = 0; length-- > 0; outLength++) { for (c = 0; c < channels; c++) { output[outIndex] = input[0]; outIndex++; output[outIndex] = input[1]; outIndex++; adpcmState[c].Predictor = (short)(input[0] | (input[1] << 8)); inIndex += 2; //adpcmState[c].Index = (sbyte)(input[0] | (input[1] << 8)); // doesnt make sense, the last one will always be 0 adpcmState[c].Index = (sbyte)input[0]; inIndex += 2; adpcmState[c].Index = (sbyte)Delimit(adpcmState[c].Index, 88, 0); } for (i = 0; i < 8; i++) { for (c = 0; c < channels; c++) { codeBuffer = (uint)(input[0] | (input[1] << 8) | (input[2] << 16) | (input[3] << 24)); inIndex += 4; for (j = 0; j < 8; j++) { buffers[c][j] = (short)DecodeSample((int)codeBuffer & 15, ref adpcmState[c]); codeBuffer >>= 4; // divide by 16 } } } for (j = 0; j < 8; j++) { for (c = 0; c < channels; c++) { output[outIndex] = (ushort)(buffers[c][j] & 0xFF); outIndex++; output[outIndex] = (ushort)((buffers[c][j] >> 8) & 0xFF); outIndex++; } } } return((uint)(outLength * kXboxAdpcmDstSize * channels)); }
static uint DecodeMemory(ushort[] input, uint length, ushort[] output, int channels) { AdpcmState[] adpcmState = new AdpcmState[channels]; short[][] buffers = new short[][] { new short[channels], new short[8] }; uint codeBuffer; int i, j, c, outLength; int inIndex = 0; int outIndex = 0; length = (uint)((length / kXboxAdpcmSrcSize) / channels); for (outLength = 0; length-- > 0; outLength++) { for (c = 0; c < channels; c++) { output[outIndex] = input[0]; outIndex++; output[outIndex] = input[1]; outIndex++; adpcmState[c].Predictor = (short)(input[0] | (input[1] << 8)); inIndex += 2; //adpcmState[c].Index = (sbyte)(input[0] | (input[1] << 8)); // doesnt make sense, the last one will always be 0 adpcmState[c].Index = (sbyte)input[0]; inIndex += 2; adpcmState[c].Index = (sbyte)Delimit(adpcmState[c].Index, 88, 0); } for (i = 0; i < 8; i++) { for (c = 0; c < channels; c++) { codeBuffer = (uint)(input[0] | (input[1] << 8) | (input[2] << 16) | (input[3] << 24)); inIndex += 4; for (j = 0; j < 8; j++) { buffers[c][j] = (short)DecodeSample((int)codeBuffer & 15, ref adpcmState[c]); codeBuffer >>= 4; // divide by 16 } } } for (j = 0; j < 8; j++) { for (c = 0; c < channels; c++) { output[outIndex] = (ushort)(buffers[c][j] & 0xFF); outIndex++; output[outIndex] = (ushort)((buffers[c][j] >> 8) & 0xFF); outIndex++; } } } return (uint)(outLength * kXboxAdpcmDstSize * channels); }
public static short DecodeAdpcm(byte input, ref AdpcmState state) { int index = state.index; int step = stepsizeTable[index]; int valpred = state.valprev; int delta = input; index += indexTable[delta]; if (index < 0) { index = 0; } if (index > 88) { index = 88; } bool sign = ((delta & 8) == 8); delta = delta & 7; int vpdiff = step >> 3; if ((delta & 4) == 4) { vpdiff += step; } if ((delta & 2) == 2) { vpdiff += step >> 1; } if ((delta & 1) == 1) { vpdiff += step >> 2; } if (sign) { valpred -= vpdiff; } else { valpred += vpdiff; } if (valpred > 32767) { valpred = 32767; } else if (valpred < -32768) { valpred = -32768; } state.valprev = (short)valpred; state.index = (byte)index; return((short)valpred); }
private static short DecodeSample(byte sample, ref AdpcmState State) { short result = 0; // Decode the sample... result = (short)(State.StepSize >> 3); if ((sample & 4) != 0) { result += State.StepSize; } if ((sample & 2) != 0) { result += (short)(State.StepSize >> 1); } if ((sample & 1) != 0) { result += (short)(State.StepSize >> 2); } if ((sample & 8) != 0) { State.Previous -= result; } else { State.Previous += result; } // Delimit the result... if (State.Previous > 32767) { State.Previous = 32767; } else if (State.Previous < -32768) { State.Previous = -32768; } // Adjust the step index... State.Index += IndexTable[sample]; // Delimit the step index... if (State.Index > 88) { State.Index = 88; } else if (State.Index < 0) { State.Index = 0; } // Adjust the step size to the new step index... State.StepSize = StepTable[State.Index]; return(State.Previous); }
static int DecodeSample(int code, ref AdpcmState state) { int Delta, Result; Delta = state.StepSize >> 3; if ((code & 4) > 0) Delta += state.StepSize; if ((code & 2) > 0) Delta += state.StepSize >> 1; if ((code & 1) > 0) Delta += state.StepSize >> 2; if ((code & 8) > 0) Delta = -Delta; Result = state.Predictor + Delta; Result = Delimit(Result, short.MaxValue, short.MinValue); state.Index += kIndexTable[code]; state.Index = (sbyte)Delimit(state.Index, 88, 0); state.StepSize = kStepTable[state.Index]; state.Predictor = (short)Result; return Result; }
/// <summary> /// Encodes the array of 16-bit uncompressed PCM samples into Xbox ADPCM blocks. /// </summary> /// <param name="samples">The array of 16-bit uncompressed PCM samples.</param> /// <returns>A memory buffer of type byte array with the encoded Xbox ADPCM blocks.</returns> private static int EncodeMemory(ref short[,] samples, ref byte[] data) { int FChannels = samples.GetLength(1); AdpcmState[] State = new AdpcmState[FChannels]; int sampleCount = samples.GetLength(0); int blockCount = (sampleCount / XboxAdpcmSampleCount); data = new byte[blockCount * XboxAdpcmChunkSize * FChannels]; byte[] block = new byte[XboxAdpcmChunkSize * FChannels]; int dataPos = 0; for (int x = 0; x < blockCount; x++) { EncodeBlock(ref samples, x * 64, ref State, ref block); for (int y = 0; y < block.Length; y++) { data[dataPos++] = block[y]; } } return(data.Length); }
private static byte EncodeSample(short sample, ref AdpcmState State) { byte bytecode = 0; // this will store the sample short dif = (short)(sample - State.Previous); // this will store the unresolved difference between // the current sample and the previous prediction. // short tempDif = dif; // short predictedDelta; // The difference between the current prediction and // the previous State.StepSize = StepTable[State.Index]; // #region Old stuff /*if (dif < 0) * { * bytecode = 8; * dif = (short)Math.Abs(dif); * } * predictedDelta = (short)(State.StepSize >> 3); * * for (int x = 1; x <= 3; x++) * { * if (dif >= (State.StepSize >> x)) * { * bytecode |= (byte)(1 << (3-x)); * dif -= (short)(State.StepSize >> x); * predictedDelta += (short)(State.StepSize >> x); * } * } * if ((bytecode & 8) != 0) State.Previous -= predictedDelta; * else State.Previous += predictedDelta; * * if (State.Previous > 32767) State.Previous = 32767; * else if (State.Previous < -32768) State.Previous = -32768; * * State.Index += IndexTable[bytecode]; * if (State.Index > 88) State.Index = 88; * else if (State.Index < 0) State.Index = 0; * * return bytecode;*/ #endregion short predictedDelta = (short)(State.StepSize >> 3); if (dif < 0) { bytecode = 8; dif = (short)(dif * -1); } byte mask = 4; short step = State.StepSize; while (mask != 0) { if (dif >= step) { bytecode |= mask; dif -= step; predictedDelta += step; } step >>= 1; mask >>= 1; } if ((bytecode & 8) != 0) { State.Previous -= predictedDelta; } else { State.Previous += predictedDelta; } if (State.Previous > 32767) { State.Previous = 32767; } else if (State.Previous < -32768) { State.Previous = -32768; } State.Index += IndexTable[bytecode]; if (State.Index > 88) { State.Index = 88; } else if (State.Index < 0) { State.Index = 0; } return(bytecode); }