'Real-Time Audio Data to Byte-Array
With the codes below, the byte array is created as soon as the audio record wav file is saved.
But I need to create this byte-array real-time, for live streams.
How can I do this?
[DllImport("winmm.dll")]
private static extern long mciSendString(string command, StringBuilder retstrign, int Returnlength, IntPtr callback);
public Form1()
{
InitializeComponent();
mciSendString("open new Type waveaudio alias recsound", null, 0, IntPtr.Zero);
button1.Click += new EventHandler(this.startbutton_Click);
}
private void startbutton_Click(object sender, EventArgs e)
{
// throw new NotImplementedException();
mciSendString("record recsound", null, 0, IntPtr.Zero);
button2.Click += new EventHandler(this.stopbutton_Click);
}
private void stopbutton_Click(object sender, EventArgs e)
{
string filename = @"C:\Users\seymk\source\repos\AudioFromSoundCard\test.wav";
//throw new NotImplementedException();
mciSendString(@"save recsound C:\Users\seymk\source\repos\AudioFromSoundCard\test.wav", null, 0, IntPtr.Zero);
mciSendString("close recsound", null, 0, IntPtr.Zero);
//ReadWav(filename, out float[] L, out float[] R);
using (WaveFileReader reader = new WaveFileReader(filename))
{
Assert.AreEqual(8, reader.WaveFormat.BitsPerSample, "");
byte[] buffer = new byte[reader.Length];
int read = reader.Read(buffer, 0, buffer.Length);
short[] sampleBuffer = new short[read / 2];
Buffer.BlockCopy(buffer, 0, sampleBuffer, 0, read);
string text = "Some metadata........" + Environment.NewLine +
String.Join(" ", buffer.Select(x => x.ToString("x2"))); // <- encoding
File.WriteAllText(@"C:\Users\seymk\source\repos\AudioFromSoundCard\Foo.txt", text);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
