Quantcast
Channel: NAudio
Viewing all 5831 articles
Browse latest View live

New Post: Convert Mpeg1 layer 2 to wave

$
0
0
The Mp3FileReader converts MP3 to WAV. just call its Read method.

New Post: 10 band Equalizer

$
0
0
Hi, the BiQuadFilter isn't really part of the official NAUdio API. I'd like to rework it at some point and make it a bit easier to use. You are right - q controls the peak width, but I don't know what the "standard" way to overlap bands in a 10 band eq is.

New Post: Need help reviewing code for a stream player. Can't get a few features integrated.

$
0
0
BufferedWaveProvider is what I would use - it is backed by a circular buffer, so you won't leak memory

New Post: Insert silence to beginning of audio file and save as new file

$
0
0
that's right, although be very careful that your silenceArray is always a multiple of the BlockAlign of the target WaveFormat.

New Post: Need help for playing a wave file from URL

$
0
0
Hi,

I would like to use NAudio to play a wave file from an URL. Every time I use
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
            var responseStream = resp.GetResponseStream()
            WaveStream readerStream = new WaveFileReader(responseStream);
It fails on reading wave header. What am I doing wrong?
Here is the test Wave file I'm trying to play:
http://www.nch.com.au/acm/11k16bitpcm.wav

Thanks for any help.

New Post: Need help for playing a wave file from URL

$
0
0
For streaming audio you really need to use a BufferedWaveProvider and write audio data into that as it arrives. WaveFileReader is unlikely to work because it expects to be able to reposition within the stream which you can't do with a network stream. WAV is a very poor choice of format for streaming audio, but if you really must use it, I'd probably write my own custom code to read the format chunk and then find the data chunk, copying everything in that to the BufferedWaveProvider.

New Post: Problem with FFT calculation

$
0
0
Hey,

A while ago I've posted on your blog (Sound Code), but I'm having a rather similar issue after having read one of your FFT articles.

Basically, what I need to do is calculate the frequency of the microphone input. I'm using IWaveProvider for this and its implemented Read(). The buffer always has a size of 8820 elements and something seems to be going wrong with the conversion from byte array to float array as well (the .FloatBuffer property part).

Here are some of the important bits...

This is where I start my recording:
private void InitializeSoundRecording()
        {
            WaveIn waveIn = new WaveIn();
            waveIn.DeviceNumber = 0;   
            waveIn.DataAvailable += (s, e) => this.waveIn_DataAvailable(s, e); 
            waveIn.RecordingStopped += (s, e) => this.waveIn_RecordingStopped(s, e);
            waveIn.WaveFormat = new WaveFormat(44100, 1);
            waveIn.StartRecording();
        }
When the DataAvailable event handler is called, the following is executed:
private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            WaveBuffer wb = new WaveBuffer(e.Buffer.Length);

            IWaveProvider iWaveProvider = new PitchDetector(new WaveInProvider(sender as WaveIn), new WaveBuffer(e.Buffer));
            iWaveProvider.Read(wb, 0, e.Buffer.Length);

            PitchDetector pd = iWaveProvider as PitchDetector;
            
            this.ShowPitch(pd.Pitch);
        }
And lastly, this is the "actual" important bit:
private const int FLOAT_BUFFER_SIZE = 8820;
        private IWaveProvider source;
        private WaveBuffer waveBuffer;
        private int sampleRate;
        private float[] fftBuffer;
        private float[] prevBuffer;
        public float Pitch { get; private set; }

        public WaveFormat WaveFormat { get { return this.source.WaveFormat; } }

        internal PitchDetector(IWaveProvider waveProvider, WaveBuffer waveBuffer = null)
        {
            this.source = waveProvider;
            this.sampleRate = waveProvider.WaveFormat.SampleRate;
            this.waveBuffer = waveBuffer;
        }

        /// <summary>
        /// UNSAFE METHOD! Do not edit unless you know what to do
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private unsafe float[] ByteArrayToFloatArray(byte[] input)
        {
            float[] fb = new float[FLOAT_BUFFER_SIZE];
            unsafe
            {
                fixed (byte* ptrBuffer = input)
                {
                    float* ptrFloatBuffer = (float*)ptrBuffer;
                    for (int i = 0; i < FLOAT_BUFFER_SIZE; i++)
                    {
                        fb[i] = *ptrFloatBuffer;
                        ptrFloatBuffer++;
                    }
                }
            }
            return fb;
        }

        public int Read(byte[] buffer, int offset = 0, int count = 0)
        {
            if (this.waveBuffer == null || this.waveBuffer.MaxSize < count)
                this.waveBuffer = new WaveBuffer(count);

            int readBytes = this.source.Read(this.waveBuffer, 0, count);

            if (readBytes > 0) readBytes = count;

            int frames = readBytes / sizeof(float);

            this.Pitch = this.DeterminePitch(this.waveBuffer.FloatBuffer, frames);

            return frames * 4;
        }

Strangely enough, when it enters the constructor, waveBuffer contains some data (255, 1, 0, etc.), but when I check the "buffer" parameter of Read(), it's entirely 0. Every element.

Out of curiosity also, why does Read() have a buffer parameter, but isn't actually used in the method at all (I got that piece of code from one of your articles)?

Any help to resolve this issue would be greatly appreciated! I've been at this for quite a while already, but can make no sense out of it.


Thanks,
Alain

New Post: Record a "conversation"

$
0
0
Is it possible to record the input and output at the same time as one stream? I am using Sipek to make calls I would like to record. I haven't been succesful in recording 2 input devices in 1 stream. I am using the WasapiLoopbackCapture to record what the other is saying. And I can record what I am saying.

But I would really like to have one stream, could anyone provide a clue how to do something like that?

New Post: Event for unplugged Microphone

$
0
0
Hello, I am a newbie programmer. I wonder if anyone could give me some pointers on how to create an event for when microphone is unplugged, open msg "Your mic is unplugged".

Is there an event from Naudio to do this, like the Form_Load, timer_tick events?

Thank you.

Created Unassigned: Problem with WavOut.Eventhandler [16393]

$
0
0
Hi

I have been trying to use the, playbackstopped eventhandler on the wavout class. I am trying to get a playlist to cycle through audio tracks so at the end of each track it evaluates the playlist and plays the next valid track. For some reason NAudio crashes when I do this at the .init() call I have had a look at the PlaybackStatus and it seems to still be at Playing when the playbackstopped event is or has been called.

Any suggestions....

if you want my project off me please give me a shout moogus@hotmail.co.uk

Darren

Created Unassigned: read mp3 to URL [16394]

$
0
0
Can Naudio read this mp3 url?
http://players.creacast.com/creacast/sudtirol1a/playlist.pls



Regards
Alex

New Post: Streaming Stereo Mix by NAudio trough wifi to Android mobile app

$
0
0
I am trying to capture audio from Stereo Mix input device in windows by NAudio .Net library, then send it over my wifi to my android phone, where I play it.

The problem is following: The sound coming from my mobile is very distorted and glitchy. What am I doing wrong?

This is my server - C# code - runs on PC
    static int C = 0; //BPS
    static WaveInEvent wi; 
    static TcpListener server;
    static List<Socket> clients = new List<Socket>(); //List of connected clients

    static void Main(string[] args)
    {
        /*  Here is a block of code, where I get stereo mix device ID */

        new System.Threading.Thread(new System.Threading.ThreadStart(rec_start)).Start();
        new System.Threading.Thread(new System.Threading.ThreadStart(conn_acept)).Start();
    }

 //network init
  private static void conn_acept()
  {            
        server = new TcpListener(new IPEndPoint(IPAddress.Any, port));
        server.Start();

        while (Running)
        {
            Socket client = server.AcceptSocket();
            client.NoDelay = true;
            client.Send(UTF8Encoding.UTF8.GetBytes(C + "\n"));
            clients.Add(client);
        }
  }


 //capture sound ------------
 private static void rec_start()
 {
    wi = new WaveInEvent();
    wi.DeviceNumber = stereomixid;
    wi.WaveFormat = new WaveFormat(44100, 2);
    wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);
    wi.StartRecording();
 }

 static void wi_DataAvailable(object sender, WaveInEventArgs e)
 {
        foreach (Socket client in clients)
        {
            try {
                client.Send(e.Buffer);
            }
            catch{}
        }
        C = e.BytesRecorded;
 }
This is my client - Java code - runs on My android mobile
 try {
            t.append("Connecting to 192.168.0.12:4546...\n");
            tcp = new Socket("192.168.0.12", 4546);

            in = new BufferedReader(new InputStreamReader(tcp.getInputStream()));

            //first line bsize -> "packetsize"
            Integer bsize = Integer.parseInt(in.readLine());

            AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, 2, AudioFormat.ENCODING_PCM_16BIT, bsize, AudioTrack.MODE_STREAM);

            while(true)
            {
                byte[] buffer = new byte[bsize];
                for(int i = 0; i < bsize; i++)
                {
                    buffer[i] = (byte) in.read();
                }
                at.write(buffer, 0, bsize);

                if(at.getPlayState() == 1)
                    at.play();
            }

        } catch (UnknownHostException e) {
            t.append("Can't connect! Host not found!\n");
        } catch (IOException e) {
            t.append("Can't connect! IOException: " + e.getMessage() + "\n");
        }
Do I have to compress the data in wi_DataAvailable before sending through wifi?
Or, do I have problem in my android application?
Is that strange for loop source of my problem?

I tried sending data asynchronously from my PC, but i didn't hepled.

Also I tried save recorded data by WaveFileWriter, it worked. So I was thinking about writing audio data to memory stream by WaveFileWriter, then compress it and send it trought wifi, but this idea looks really crazy, so I think, there is another way to achieve this.

Thanks for answers guys.

Commented Unassigned: Problem with WavOut.Eventhandler [16393]

$
0
0
Hi

I have been trying to use the, playbackstopped eventhandler on the wavout class. I am trying to get a playlist to cycle through audio tracks so at the end of each track it evaluates the playlist and plays the next valid track. For some reason NAudio crashes when I do this at the .init() call I have had a look at the PlaybackStatus and it seems to still be at Playing when the playbackstopped event is or has been called.

Any suggestions....

if you want my project off me please give me a shout moogus@hotmail.co.uk

Darren
Comments: what callback mechanism are you using? It can be problematic to be opening the waveout device at the same time as closing it with some callback models.

New Post: Event for unplugged Microphone

$
0
0
This is not something you can reliably detect. If it was a USB microphone, then the device itself would be removed, but many soundcards will not surface any notification to Windows that something has been connected or disconnected from one of their inputs.

New Post: Streaming Stereo Mix by NAudio trough wifi to Android mobile app

$
0
0
You'd need buffering at the receiving end to make up for latency in the network. It would also be usual to compress the audio somehow as PCM is a very inefficient way to transmit audio. Also, you should only send BytesRecorded worth of audio each time.

Commented Unassigned: read mp3 to URL [16394]

$
0
0
Can Naudio read this mp3 url?
http://players.creacast.com/creacast/sudtirol1a/playlist.pls



Regards
Alex
Comments: NAudio does not read PLS files. They are only text files, so you would have to see what is at the end of the URLs within the PLS file. If it is MP3, or something that MediaFoundationReader could read, then you might be able to play it.

New Post: Record a "conversation"

$
0
0
I don't think you can do this as one step. My approach has been to mix together the two recorded streams into one file.

New Post: Convert Mpeg1 layer 2 to wave

$
0
0
Sorry but one question,
is this method compatible to windows store app yet?

New Post: Convert Mpeg1 layer 2 to wave

$
0
0
For windows store apps, the MediaFoundationReader will be the preferred option in the future. This will be able to coinvert MP3 to WAV

Commented Unassigned: read mp3 to URL [16394]

$
0
0
Can Naudio read this mp3 url?
http://players.creacast.com/creacast/sudtirol1a/playlist.pls



Regards
Alex
Comments: Perfect !!! I have create a routine for extract url and it work fine. Regards Alex
Viewing all 5831 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>