0

I'm using NAudio with C# to convert wave files to 48,000, 8bit, Mono. I'm storing the converted data in a byte array. My code is below. It works fine, but the new wave file data is unsigned, and I need it to be signed.

WaveFormat target = new WaveFormat(48000, 8, 1);
WaveStream stream = new WaveFileReader(this.FilePath);
WaveFormatConversionStream conv = new WaveFormatConversionStream(target, stream);
this.ConvertedData = new byte[conv.Length];
conv.Position = 0;
conv.Read(this.ConvertedData, 0, Convert.ToInt32(conv.Length));
stream.Dispose();
conv.Dispose();

I've tried looping through the byte array after the data is converted, and subtracting 128 from each byte, which is a solution that I found on this site.

for (int i = 0; i < end; i++)
{
    signed = (int)this.ConvertedData[i] - 128;
    this.Data[i] = (byte)signed;
}

This kind of works, but is not fool proof, and I'm sure there has to be a better way. Any suggestions would be greatly appreciated. Thanks.

user1561302
  • 81
  • 2
  • 5
  • 1
    according to this description of wav file format, when you use 8-bit the values can only be unsigned. http://www.sonicspot.com/guide/wavefiles.html "when samples are represented with 8-bits, they are specified as unsigned values" – TJD Aug 18 '12 at 14:46
  • not sure if this site will help but it may provide a better explanation about what you can and can't do http://stackoverflow.com/questions/11711689/remove-the-headers-from-several-wav-files-and-then-concatenate-the-remaining-da – MethodMan Aug 18 '12 at 14:47

3 Answers3

0
WaveFormat target = new WaveFormat(48000, 8, 1);
WaveStream stream =new WaveFileReader(this.FilePath);
WaveFormatConversionStream str = new WaveFormatConversionStream(target, stream);
WaveFileWriter.CreateWaveFile("c:\\converted.wav", str); //or the path of .wav file

try using the WaveFileWriter.CreateFile method instead..

Checkout NAudio This link you can checkout too they have a C# audio library Alvis.Audio

MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

I cannot comment, so I have to "misuse" the answer function:

try:

Convert.toSByte

It is String based but should work...

http://msdn.microsoft.com/en-us/library/b0hdkwd9.aspx

0

If you are just looking a better way to convert from unsigned byte array to signed byte array, you can use Buffer.BlockCopy method, or try with sbyte[] signed = (sbyte[])(Array)unsigned . Hope it helps.

Avichal Badaya
  • 3,423
  • 1
  • 21
  • 23