简体   繁体   中英

How can I convert an 8bit unsigned wave file, to an 8bit signed wave file?

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM