locked
Show data of e.buffer Naudio RRS feed

  • Question

  • Hi everyone!

    I record and play audio at the same time with NAudio. This code:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using NAudio.Wave;
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            private WaveIn recorder;
            private BufferedWaveProvider bufferedWaveProvider;
            private SavingWaveProvider savingWaveProvider;
            private WaveOut player;
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                numericUpDown1.Value = 1;
                numericUpDown1.DecimalPlaces = 1;
                numericUpDown1.Increment = 0.1M;
                numericUpDown1.Maximum = 1;
                numericUpDown1.Minimum = 0;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                recorder = new WaveIn();
                recorder.BufferMilliseconds = 20;
                recorder.DataAvailable += RecorderOnDataAvailable;

                // set up our signal chain
                bufferedWaveProvider = new BufferedWaveProvider(recorder.WaveFormat);
                savingWaveProvider = new SavingWaveProvider(bufferedWaveProvider, "temp.wav");

                // set up playback
                player = new WaveOut { DesiredLatency = 100};
                player.Init(savingWaveProvider);
                //Console.Write(player);
                // begin playback & record
                player.Volume = 1;
                player.Play();
                recorder.StartRecording();
            }

            private void RecorderOnDataAvailable(object sender, WaveInEventArgs e)
            {

                bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
            }

            private void button3_Click(object sender, EventArgs e)
            {
                recorder.StopRecording();
                player.Stop();
                savingWaveProvider.Dispose();
            }

            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                player.Volume = (float)numericUpDown1.Value;
            }
        }
    }

    In bufferedWaveProvider.AddSamples mabye data put in E.buffer and how to show that data to a Lable or Textbox. I tried "Tostring" but this is return System.Byte[]. Please help me!

    Wednesday, October 7, 2020 3:03 PM

Answers

  • Try something like this:

       string s = BitConverter.ToString( e.Buffer, 0, e.BytesRecorded );

    Then show or append this string (e.g. textBox1.AppendText( s + Environment.NewLine )).

    • Marked as answer by hungcui Thursday, October 8, 2020 4:21 AM
    Wednesday, October 7, 2020 5:10 PM