- Audio Readers is a hierarchy of Alvas.Audio classes that implement IAudioReader interface used to read format and audio data.
- AudioReader is a base class for the readers that can read an audio data from the stream. This class is abstract.
- AuReader class reads an audio data from the .AU and .SND streams.
- AviReader class reads audio data from the Avi stream.
- WaveReader class reads WAVE (waveform audio format) data from the stream.
- RawReader class reads an audio data from the headerless stream. Slinear, Gsm, A-law, mu-law etc. For that, you need to specify explicitly the format of the audio data that they contain.
- Mp3Reader class reads audio data from the Mp3 stream.
- AuReader class reads an audio data from the .AU and .SND streams.
- DsReader class reads audio data using DirectShow. List of supported formats can be expanded with additional DirectShow filters installed.
Use Combined_Community_Codec_Pack, K-Lite_Codec_Pack or Ffdshow to expand the list of supported formats.
At the moment, can only work with files stored on disk.
Three methods of IAudioReader interface most commonly used.
string fileName = "Your audio file.wav";
IAudioReader ir = new WaveReader(File.OpenRead(fileName));
IntPtr format = ir.ReadFormat();// Reads an audio format.
byte[] data = ir.ReadData();// Reads all audio data from the stream.
ir.Close();// Closes the current reader and the underlying stream.
To read the audio data portions of 1 second, you can use the following code.
string fileName = "Your audio file.mp3";
IAudioReader ir = new Mp3Reader(File.OpenRead(fileName));
IntPtr format = ir.ReadFormat();// Reads an audio format.
int skipSeconds = 0;
const int readSeconds = 1;
while (true)
{
// Reads audio data starts at skipSeconds position and readSeconds duration.
byte[] data = ir.ReadData(skipSeconds, readSeconds);
if (data.Length == 0)
{
break;
}
skipSeconds += readSeconds;
}
ir.Close();// Closes the current reader and the underlying stream.
To read the audio data by portions that are smaller than 1 second, you can use the following code.
string fileName = "Your audio file.pcm";
//Specifies the format of audio data
IntPtr format = AudioCompressionManager.GetPcmFormat(2, 16, 44100);
IAudioReader ir = new RawReader(File.OpenRead(fileName), format);
const int miliseconds = 500;
int skipBytes = 0;
// Converts from milliseconds to bytes for current stream.
int readBytes = ir.Milliseconds2Bytes(miliseconds);
while (true)
{
// Reads audio data starts at skipBytes position and readBytes length.
byte[] data = ir.ReadDataInBytes(skipBytes, readBytes);
if (data.Length == 0)
{
break;
}
skipBytes += readBytes;
}
ir.Close();// Closes the current reader and the underlying stream.
The following code allows to determine the audio stream duration in milliseconds and the audio data size in bytes.
string fileName = "Your audio file.snd";
IAudioReader ir = new AuReader(File.OpenRead(fileName));
// Gets audio stream duration in milliseconds.
int durationInMS = ir.GetDurationInMS();
// Gets audio stream length in bytes.
int lengthInBytes = ir.GetLengthInBytes();
ir.Close();// Closes the current reader and the underlying stream.
Avi files are video files. They may not contain audio data. To determine this AviReader class has HasAudio property.
DsReader HasAudio equal false means that the file contains no audio data or audio file format is unknown, ie no suitable DirectShow filter.
using (AviReader ar = new AviReader(File.OpenRead("your.avi")))
{
if (ar.HasAudio)
{
// ...
}
}
using (DsReader dr = new DsReader("Your audio file"))
{
if (dr.HasAudio)
{
// ...
}
}
Enjoy.
No comments:
Post a Comment