timotech
Member
- Joined
- Aug 15, 2022
- Messages
- 12
- Programming Experience
- 10+
Good day guys,
Please I am doing multiple recordings from different mic inputs using naudio, but I have issue displaying the volume meters for each recording.
Please how can I resolve this, please check my code below.
Thanks
Please I am doing multiple recordings from different mic inputs using naudio, but I have issue displaying the volume meters for each recording.
Please how can I resolve this, please check my code below.
Thanks
C#:
class VolumeSignal
{
public ProgressBar Progress { get; set; }
public double[] AudioValues { get; set; }
}
public partial class frmTestTabs : Form
{
static WaveInEvent waveSource;
readonly int SampleRate = 44100;
readonly int BitDepth = 16;
readonly int ChannelCount = 1;
readonly int BufferMilliseconds = 20;
readonly double[] AudioValues;
object baton = new object();
public frmTestTabs()
{
InitializeComponent();
TabControl.TabPageCollection pages = tabControl1.TabPages;
for (int i = 0; i < pages.Count; i++) //tab page is 8, pages.Count is 8
{
var AudioValues = new double[SampleRate * BufferMilliseconds / 1000];
_ = Task.Factory.StartNew(() => RecordMicNAudio(0, AudioValues));
//Each progress bar in the tabs
var firstProgressBar = (from t in pages[i].Controls.OfType<ProgressBar>()
select t).FirstOrDefault();
//Timer event for progress bar
TimerCallback timerCallback = new TimerCallback(timer_Elapsed);
System.Threading.Timer timer = new System.Threading.Timer(timerCallback, new VolumeSignal { Progress = firstProgressBar, AudioValues = AudioValues }, 1000, 500);
}
}
private void timer_Elapsed(object msg)
{
lock (baton)
{
VolumeSignal volumeSignal = (VolumeSignal)msg;
int level = (int)volumeSignal.AudioValues.Max();
var prBar = volumeSignal.Progress;
// auto-scale the maximum progressbar level
if (level > prBar.Maximum)
prBar.Maximum = level;
prBar.Value = level;
}
}
void RecordMicNAudio(int deviceNum, double[] AudioVals)
{
waveSource = new WaveInEvent();
waveSource.DeviceNumber = deviceNum;
waveSource.WaveFormat = new NAudio.Wave.WaveFormat(rate: sampleRate, bits: 16, channels: 1);
waveSource.DataAvailable += (_, e) =>
{
//Recording code here
//volume meter below
int peakValue = 0;
for (int i = 0; i < e.Buffer.Length / 2; i++)
{
int value = BitConverter.ToInt16(e.Buffer, i * 2);
peakValue = Math.Max(peakValue, value);
}
AudioVals[0] = peakValue;
}
};
waveSource.RecordingStopped += (_, _) => MessageBox.Show("Sound Stopped! Cannot capture sound from device...");
waveSource.BufferMilliseconds = 1000;
waveSource.StartRecording();
}
}
Last edited: