Multimedia-Lecture-Four
Audio
Eng. Noor Alhakim
Audio Signal
An audio signal is a representation of sound.
It is typically using a level of electrical voltage for analog signals, and a series of binary
numbers for digital signals.
Audio signals have frequencies in the audio frequency range of roughly 20 to 20,000 Hz,
which corresponds to the lower and upper limits of human hearing.
Dealing With Audio in C#
o Reading Audio File
o Writing Audio File o Drawing the Audio Signal
o Playing Audio File o Merge Two Audio Files
o Find More Information about Audio o Recording Audio File
o Audio Scaling
Reading Audio File:
string audioFile = "[Link]";
var audioFileReader = new AudioFileReader(audioFile);
Writing Audio File:
[Link]("[Link]", audioFileReader);
Playing an audio:
using (var outputDevice = new WaveOutEvent())
{
[Link](audioFileReader);
[Link]();
while ([Link] == [Link])
{
[Link](1000);
}
}
Find More Information about Audio:
using (var audioFileReader = new AudioFileReader(audioFile))
{
// Get information about the audio file
TimeSpan duration = [Link];
int sampleRate = [Link];
int channels = [Link];
// Display the information
[Link]($"Duration: {duration}");
[Link]($"Sample Rate: {sampleRate}"); Find out the BitPerSample
[Link]($"Channels: {channels}"); property
}
Audio Format
Audio Scaling
Audio Scaling allows for modifying an audio signal amplitude or frequency.
To increase the volume of the audio
track you can multiple the variable it is
stored in by a scalar.
To slow down or speed up the track
played you can adjust the sampling
rate.
Modifying the Volume of Audio File
using (var audioFileReader = new AudioFileReader(audioFile))
{
// Create a new buffer to store scaled audio data
float[] buffer = new float[[Link]];
// Read audio data into the buffer
int samplesRead = [Link](buffer, 0, [Link]);
// Scale each sample in the buffer
for (int i = 0; i < samplesRead; i++)
{
buffer[i] *= scaleFactor;
}
// Create a new WaveFileWriter to write scaled audio data to a new file
using (var waveFileWriter = new WaveFileWriter(outputAudioFile, [Link]))
{
// Write the scaled audio data to the output file
byte[] byteBuffer = new byte[samplesRead * sizeof(float)];
[Link](buffer, 0, byteBuffer, 0, [Link]);
[Link](byteBuffer, 0, [Link]);
}
}
Drawing the Audio Signal
using [Link];
WaveViewer waveViewer = new WaveViewer();
[Link] = [Link];
[Link] = 400;
[Link] = 10000;
// Load the audio file and set it as the WaveStream for the WaveViewer
WaveFileReader waveFileReader = new WaveFileReader(audioFile1);
[Link] = waveFileReader;
// Create and configure the form
Form form = new Form();
[Link] = "WaveViewer Example";
[Link](waveViewer);
[Link] = new [Link](800, 600); // Set the form size
[Link]();
Recording Audio File
using (var waveIn = new WaveInEvent())
{
[Link] = new WaveFormat(44100, 1); // 44100 Hz, mono
WaveFileWriter waveFileWriter = null;
[Link] += (sender, e) =>
{
// Initialize the WaveFileWriter on the first call to the DataAvailable event
if (waveFileWriter == null)
{
waveFileWriter = new WaveFileWriter(outputAudioFile, [Link]);
}
// Write the recorded audio data to the WAV file
[Link]([Link], 0, [Link]);
};
Recording Audio File
[Link]();
[Link]("Recording. Press any key to stop...");
[Link]();
[Link]();
// Close the WaveFileWriter after recording is stopped
waveFileWriter?.Dispose();
[Link]("Recording stopped. Audio saved to: " + outputAudioFile);
}
Merge two audio files
Example:
using [Link];
using (var reader1 = new AudioFileReader(audioFile1))
using (var reader2 = new AudioFileReader(audioFile2))
{
var mixer = new MixingSampleProvider(new[] { reader1, reader2 });
WaveFileWriter.CreateWaveFile16(outputAudioFile, mixer);
}
Now check [Link] file and note
the duration property should be equal
to duration of two files together
Exercise:
Write a C#-code to:
a. Reverse the audio signal.
b. Reverse the audio Channels.
c. Plot the audio signal after reversing it.
d. Save the new audio files on your disk
Tip:
Use Charting to plot the audio signal OR you can
use OxyPlot
That’s All