Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net How to play audio samples directly

I have audio samples extracted through NAudio, i know parameters:

  • channels
  • bytes per sample,
  • samplerate

How to play that samples by using .Net api / or other .Net library

Here code:

    openFileDialog1.ShowDialog();
    using (var reader = new Mp3FileReader(openFileDialog1.FileName))
    {
        var pcmLength = (int)reader.Length;
        var _leftBuffer = new byte[pcmLength / 2];
        var buffer = new byte[pcmLength];
        var bytesRead = reader.Read(buffer, 0, pcmLength);
        int index = 0;
        for (int i = 0; i < bytesRead; i += 4)
        {
           //extracting only left channel 
           _leftBuffer[index] = buffer[i];
           index++;
           _leftBuffer[index] = buffer[i + 1];
           index++;
         }

         // How to play  _leftBuffer (Single channel, 2 bytes per sample, 44100 samples per secound)
}
like image 353
testCoder Avatar asked Dec 19 '12 18:12

testCoder


2 Answers

First, you need to implement IWaveProvider or user one of the IWaveProvider implementations that come with NAudio like WaveProvider16, for example. Next, Initialize a WaveOut object with your IWaveProvider using WaveOut.Init(IWaveProvider Provider), and finally, call WaveOut.Play().

like image 116
Quanta Avatar answered Oct 15 '22 04:10

Quanta


YES, I have found a solution: A low-level audio player in C#

Full worked code:

public partial class Form1 : Form
    {
        private byte[] _leftBuffer;
        private BiQuadFilter _leftFilter;
        private BiQuadFilter _rightFilter;
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            using (var reader = new Mp3FileReader(openFileDialog1.FileName))
            {
                var pcmLength = (int)reader.Length;
                _leftBuffer = new byte[pcmLength / 2];
                var buffer = new byte[pcmLength];
                var bytesRead = reader.Read(buffer, 0, pcmLength);

                int index = 0;
                for (int i = 0; i < bytesRead; i += 4)
                {
                    _leftBuffer[index] = buffer[i];
                    index++;
                    _leftBuffer[index] = buffer[i + 1];
                    index++;
                }
                var player = new WaveLib.WaveOutPlayer(-1, new WaveLib.WaveFormat(44100, 16, 1), _leftBuffer.Length, 1, (data, size) =>
                                                                                                               {
                                                                                                                   byte[] b = _leftBuffer;
                                                                                                                   System.Runtime.InteropServices.Marshal.Copy(b, 0, data, size);
                                                                                                               });
            }
        }
    }
like image 1
testCoder Avatar answered Oct 15 '22 03:10

testCoder