Hi,
Whether it is possible to play a Encoded file using ... following method directly?
PlayOutputFile.Play()
Hi,
Whether it is possible to play a Encoded file using ... following method directly?
PlayOutputFile.Play()
Hi,
Even though i installed LameAcm on my system,its not listing in the Format Combo box
No, NAudio does not allow you to play compressed audio directly. I don't know why your LAME Acm is not showing up. Are you sure it installed correctly?
Hi,
i am using following Wave Format for recoding a audio file.
waveIn =New WaveIn ()
waveIn.WaveFormat =New WaveFormat (8000, 1)
inputformat = waveIn.WaveFormat
Now,i need to Encode/Compress these recorded audio file into Ima Adpcm Format.And,I also seen the discussion about this issue in your forum.There you said,
"Simply add a short in there. Extra size means how many extra bytes there are on top of the original WaveFormat structure. You need a variable to hold that data (a short because you have two extra bytes). And you should also set the contents of that extra data according to the specification of the specific WaveFormatTag you are using"
private class MyWaveFormat:WaveFormat
{
short extraData;
public MyWaveFormat(WaveFormatEncoding WaveFormatTag, short BitsPerSample, short Channels, int AverageBytesPerSecond, short BlockAlign, int SampleRate, short ExtraSize)
{
base.extraSize = ExtraSize;
base.averageBytesPerSecond = AverageBytesPerSecond;
base.bitsPerSample = BitsPerSample;
base.blockAlign = BlockAlign;
base.channels = Channels;
base.sampleRate = SampleRate;
base.waveFormatTag = WaveFormatTag;
}
}
But i am not getting how to set contents "ExtraData" corresponding to Extra Size.
I also tried to set ""ExtraData"" using following Methods,but still i am getting the Same Error ""AcmNotPossible Calling the AcmStreamOpen""
ptr=Waveformat.MarshalToPtr (inputformat)
TargetFormat=waveformat.MarshalFromPtr(ptr)
Can you Please guide me About this problem as early as possible.
Regards,
Chandan k.s
Look here at the WAVE_FORMAT_DVI_ADPCM structure definition. You'll need to find appropriate values for the wSamplesPerBlock value, but you can do that by examining the extra bytes reported by the NAudioDemo ACM screen for the IMA ADPCM codec
How i need to make use of those extra bytes...? . Also,CreateCustomWaveFormat() wont woks for this Ima Adpcm WaveFormat,So which method i need to use for Ima Adpcm Encoding...?
I tried in this way by creating My_ImaAdpcmWaveFormat method Ima Adpcm class,and i am passing that Extra Byte value,since Extra Byte in Hexadecimal,i converted that to Decimal,then passed to these Function.
public My_ImaAdpcmWaveFormat(int sampleRate, int channels, int bitsPerSample,int sampleperblock)
{
this.waveFormatTag = WaveFormatEncoding.DviAdpcm; // can also be ImaAdpcm - they are the same
this.sampleRate = sampleRate;
this.channels = (short)channels;
this.bitsPerSample = (short)bitsPerSample; // TODO: can be 3 or 4
this.extraSize = 2;//
this.blockAlign = 0; //TODO //Also i tried for blockalign and avgbytespersecond also i assigned values
this.averageBytesPerSecond = 0; //TODO
this.samplesPerBlock = short(sampleperblock);
}
Then,when i excecute following line,
convertedStream=NewWaveFormatConversionStream(NewImaAdpcmWaveFormat(InputFormat.SampleRate,InputFormat.Channels,InputFormat.bitspersample,63745), Reader)
Getting the same error ""AcmNotPossible Calling the AcmStreamOpen"".So " How i need to make use of those extra bytes...?"
you need to look very carefully at the values returned in the NAudioDemo application. This shows what the ACM codec is expecting from a WaveFormat. The WaveFormat you pass in must be exactly the same.
as mark said in a pcm stream left and right is always next to each other. this means:
left|right|left|right
how big these blocks are is stored in the waveformat. just take a look at bitspersample
that means if you have a 16bit stream each block is 2 byte long
left|right|left|right
2 2 2 2 - byte
if you have a 24 bit stream you have to take 3 bytes... should be everything clear now :)
Yes thanks ;)
Ya,i tried the way you said.but still i am getting the same error .
public ImaAdpcmWaveFormat(int sampleRate, int channels, int bitsPerSample,int sperblock)
{
this.waveFormatTag = WaveFormatEncoding.DviAdpcm; // can also be ImaAdpcm - they are the same
this.sampleRate = sampleRate;
this.channels = (short)channels;
this.bitsPerSample = (short)bitsPerSample; // TODO: can be 3 or 4
this.extraSize = 2;//
this.blockAlign = 256; //TODO
this.averageBytesPerSecond = 4055; //TODO
this.samplesPerBlock = (short)sperblock; // TODO
}
Above values which i am assigning are same as NaudioDemo Application values.
Format 0: 8.000 kHz, 4 Bit, Mono FormatTag: DviAdpcm, Support Flags: Codec WaveFormat: DviAdpcm 8000Hz Channels: 1 Bits: 4 Block Align: 256, AverageBytesPerSecond: 4055 (32.4 kbps), Extra Size: 2 Extra Bytes: F9 01
In my code i wrote like this,
convertedStream = New WaveFormatConversionStream(New ImaAdpcmWaveFormat(8000, 1, 4, 2048), Reader) WaveFileWriter.CreateWaveFile(CurrentFile, convertedStream)
Since Extra Bytes in Hexa Decimal,i converted it into Decimal.
Like this way also i tried..... 63745 is the Decimal value of Extra Byte:F901
TargetFormat = ImaAdpcmWaveFormat.MyImaAdpcmWaveFormat(8000, 1, 4, 63745)
convertedStream = New WaveFormatConversionStream(TargetFormat, Reader)
public static ImaAdpcmWaveFormat MyImaAdpcmWaveFormat(int sampleRate, int channels, int bitsPerSample, float sperblock)
{
ImaAdpcmWaveFormat ImaAdpcmWformat = new ImaAdpcmWaveFormat();
ImaAdpcmWformat.waveFormatTag = WaveFormatEncoding.DviAdpcm; // can also be ImaAdpcm - they are the same
ImaAdpcmWformat.sampleRate = sampleRate;
ImaAdpcmWformat.channels = (short)channels;
ImaAdpcmWformat.bitsPerSample = (short)bitsPerSample; // TODO: can be 3 or 4
ImaAdpcmWformat.extraSize = 2;//
ImaAdpcmWformat.blockAlign = 256; //TODO
ImaAdpcmWformat.averageBytesPerSecond = 4055; //TODO
ImaAdpcmWformat.samplesPerBlock = (short)sperblock; // TODO
return ImaAdpcmWformat;
}
Same error i am getting.
I think you must be getting close now. Try 505 as the samplesPerBlock value (intel byte order is the other way round so this is 0x01F9)
Also, make sure that the thing you are converting is 8kHz, mono, 16 bit PCM, since ACM codecs do not usually support changing sample rates or number of channels.
Mark
Also i converted "F9" and "01" separately to Decimal ,Then i got the value "249" & "01".
now i assigned "249" & "01" to SamplesPerBlock.
but same error.
Your class should have a single private variable of type short. Set it to 505.
Yes, NAudio is designed to give you access to the individual audio samples. However, there are lots of different audio formats (bit depths, compression types etc), so there is no one simple way that works for everything. The WaveFileReader class will give you access to the raw byte data. You can then convert each pair of bytes into 16 bit samples yourself, or take advantage of some of the NAudio helper classes to allow you to more easily work directly with 16 bit audio (assuming your audio is 16 bit). Then you use the WaveFileWriter class to write your modified audio back out to disk.
If you want to deal with audio as floats, as you say, use the new AudioFileReader class which will return a ISampleProvider, which makes it very easy to examine the value of each sample as a float as it comes through the Read method. You would create your own ISampleProvider whose Read method reads from the source AudioFileReader and examines and modifies the audio:
var reader = new AudioFileReader();
var mySampleProvider = new MySampleProvider(reader);
WaveFileWriter.CreateWaveFile(myWaveProvider, "example.wav");
class MySampleProvider: ISampleProvider
private int Read(float[] buffer, int offset, int count)
{
int samplesRead = source.Read(buffer, offset, count);
// TODO: examine and optionally change the contents of buffer
return samplesRead;
}
Ya ,now its Encoding,but when i try to play the Encoded Audio File using " System.Diagnostics.Process.Start(outputFileName)",i am getting the error stating that
"Windows Media Player encountered a problem while playing the file"
and i tried using following steps to play the Encoded Audio,but getting the error "Invalid WAV file - No data chunk found" .
wavePlayer = New WaveOut()
FileRdr = New AudioFileReader(CurrentFile)
wavePlayer.Init(FileRdr)
wavePlayer.Play()
And above Stpes are working fine for all other Encoded Wave Format Audio Files.
Please reply as early as possible.
Regards ,
chandan.ks
what is the code you are using to create the WAV file?
waveIn = New WaveIn()
waveIn.WaveFormat = New WaveFormat(8000, 16, 1)
writer = New WaveFileWriter(CurrentFile, waveIn.WaveFormat)
where currentfile contains newly cretaed file path