hope you can help. I record sound from a microphone and broadcast it over the network. The quality of the samples is 11025 Hz, 8 bits, mono. Although there is a slight delay (1 second), it works great. I need help trying to implement noise reduction and compression to make the sound quieter and use less bandwidth. Audio samples are stored in a C # array with bytes [], which I send / receive using Socket.
Can anyone suggest how to implement compression and noise reduction in C #? I do not mind using a third-party library if it is free (LGPL license, etc.), and can be used with C #. However, I would prefer actual source code examples. Thank you in advance for any suggestion you have.
UPDATE:
I changed the bit size from 8-bit sound to 16-bit sound, and the interference problem was fixed. Apparently, the 8-bit microphone sound had a too low signal to noise ratio. The voice sounds great at 11 kHz, 16 bit mono.
The requirements of this project have changed since I posted this. We are also trying to add a video. I have a callback function that receives live images every 100 ms from a webcam. I need to encode audio and video, multiplex them, transfer them to my socket on the server, the server retransmits the stream to another client, which receives the stream, demultiplexes the stream and decodes the audio and video, displays the video in the image window and outputs the sound to the speaker.
I look at ffmpeg to help with (de | en) coding / [de] muxing, and I also see SharpFFmpeg as a C # library of interaction with ffmpeg.
I can not find good examples of this. I scoured the Internet all week without knowing any luck. Any help you can provide is greatly appreciated!
Here's some code, including my mic callback function:
private const int AUDIO_FREQ = 11025;
private const int CHANNELS = 1;
private const int BITS = 16;
private const int BYTES_PER_SEC = AUDIO_FREQ * CHANNELS * (BITS / 8);
private const int BLOCKS_PER_SEC = 40;
private const int BUFFER_SECS = 1;
private const int BUF_SIZE = ((int) (BYTES_PER_SEC / BLOCKS_PER_SEC * BUFFER_SECS / 2)) * 2; // rounded to nearest EVEN number
private WaveLib.WaveOutPlayer m_Player;
private WaveLib.WaveInRecorder m_Recorder;
private WaveLib.FifoStream m_Fifo;
WebCam MyWebCam;
public void OnPickupHeadset ()
{
stopRingTone ();
m_Fifo = new WaveLib.FifoStream ();
WaveLib.WaveFormat fmt = new WaveLib.WaveFormat (AUDIO_FREQ, BITS, CHANNELS);
m_Player = new WaveLib.WaveOutPlayer (-1, fmt, BUF_SIZE, BLOCKS_PER_SEC,
new WaveLib.BufferFillEventHandler (PlayerCB));
m_Recorder = new WaveLib.WaveInRecorder (-1, fmt, BUF_SIZE, BLOCKS_PER_SEC,
new WaveLib.BufferDoneEventHandler (RecorderCB));
MyWebCam = null;
try
{
MyWebCam = new WebCam ();
MyWebCam.InitializeWebCam (ref pbMyPhoto, pbPhoto.Width, pbPhoto.Height);
MyWebCam.Start ();
}
catch {}
}
private byte [] m_PlayBuffer;
private void PlayerCB (IntPtr data, int size)
{
try
{
if (m_PlayBuffer == null || m_PlayBuffer.Length! = size)
m_PlayBuffer = new byte [size];
if (m_Fifo.Length> = size)
{
m_Fifo.Read (m_PlayBuffer, 0, size);
}
else
{
// Read what we can
int fifoLength = (int) m_Fifo.Length;
m_Fifo.Read (m_PlayBuffer, 0, fifoLength);
// Zero out rest of buffer
for (int i = fifoLength; i <m_PlayBuffer.Length; i ++)
m_PlayBuffer [i] = 0;
}
// Return the play buffer
Marshal.Copy (m_PlayBuffer, 0, data, size);
}
catch {}
}
private byte [] m_RecBuffer;
private void RecorderCB (IntPtr data, int size)
{
try
{
if (m_RecBuffer == null || m_RecBuffer.Length! = size)
m_RecBuffer = new byte [size];
Marshal.Copy (data, m_RecBuffer, 0, size);
// HERE WHERE I WOULD ENCODE THE AUDIO IF I KNEW HOW
// Send data to server
if (theForm.CallClient! = null)
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs ();
args.SetBuffer (m_RecBuffer, 0, m_RecBuffer.Length);
theForm.CallClient.SendAsync (args);
}
}
catch {}
}
// Called from network stack when data received from server (other client)
public void PlayBuffer (byte [] buffer, int length)
{
try
{
// HERE WHERE I WOULD DECODE THE AUDIO IF I KNEW HOW
m_Fifo.Write (buffer, 0, length);
}
catch {}
}
So where do I go?