Ejemplo n.º 1
0
        /*internal void BufferFiller(IntPtr data, int size)
         * {
         *      if (t.Count != 0)
         *      {
         *              byte[] b = t.Dequeue();
         *              //size = b.Length;
         *              System.Runtime.InteropServices.Marshal.Copy(b, 0, data, b.Length);
         *              //data = t.Dequeue();
         *              //size = t2.Dequeue();
         *      }
         * }*/
        //WaveLib.WaveNative.WaveHdr h = new WaveLib.WaveNative.WaveHdr();
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x113)
            {
                frame++;
                if (frame >= File.Header.NrFrames)
                {
                    frame = 0;
                }
                IO.Misc.THP.THPFrame f = File.GetFrame(frame);                //t.Dequeue();

                if (audio)
                {
                    byte[] a;
                    f.ToPCM16(out a);
                    //t.Enqueue(a);
                    //w.Size = f.ToPCM16(out w.Data);

                    //unsafe
                    //{
                    //WaveLib.WaveNative.waveOutPrepareHeader(WaveOut, ref h, sizeof(WaveLib.WaveNative.WaveHdr));
                    //WaveLib.WaveNative.waveOutWrite(WaveOut, ref h, sizeof(WaveLib.WaveNative.WaveHdr));
                    //}
                    //byte[] s = f.ToPCM16();
                    bb.AddSamples(a, 0, a.Length);
                }
                pictureBox1.Image = f.ToBitmap();
                //backgroundWorker1.RunWorkerAsync();
            }
            base.WndProc(ref m);
        }
Ejemplo n.º 2
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!stop)
            {
                if (Frames.Count < 8 || AudioBuffer.BufferedBytes < 8192 * 4)
                {
retry:
                    byte[] audio;
                    Bitmap b = Video.GetNextFrame(out audio);
                    if (audio != null)
                    {
                        short[] data   = AudioConverter.GetWaveData(audio, 0, audio.Length);
                        byte[]  result = new byte[data.Length * 2];
                        IOUtil.WriteS16sLE(result, 0, data);
                        AudioBuffer.AddSamples(result, 0, result.Length);
                        goto retry;
                    }
                    if (b == null)
                    {
                        stop = true;
                        if ((Video.Header.Flags & 4) == 4)
                        {
                            Player.Stop();
                            Player.Dispose();
                            Player      = null;
                            AudioBuffer = null;
                        }
                    }
                    else
                    {
                        Frames.Enqueue(b);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void RunAudioThread(object jobObj)
        {
            var job = (AudioOut.Job)jobObj;

            using (var audioOut = new NAudio.Wave.WaveOut())
            {
                var audioBuffer = new NAudio.Wave.BufferedWaveProvider(new NAudio.Wave.WaveFormat());

                audioOut.DesiredLatency = 100;
                audioOut.Init(audioBuffer);
                audioOut.Play();

                var bufferSize   = 5000;
                var sampleBuffer = new float[bufferSize];
                var byteBuffer   = new byte[bufferSize * 4];
                while (true)
                {
                    while (audioBuffer.BufferedBytes < bufferSize * 2)
                    {
                        for (var i = 0; i < sampleBuffer.Length; i++)
                        {
                            sampleBuffer[i] = 0;
                        }

                        var sampleNum = job.GetNextSamples(sampleBuffer);
                        if (sampleNum == 0)
                        {
                            goto end;
                        }

                        for (var i = 0; i < sampleNum; i++)
                        {
                            var sampleU = unchecked ((ushort)(short)(sampleBuffer[i] * 0x4000));

                            byteBuffer[i * 4 + 0] = (byte)((sampleU >> 0) & 0xff);
                            byteBuffer[i * 4 + 1] = (byte)((sampleU >> 8) & 0xff);
                            byteBuffer[i * 4 + 2] = (byte)((sampleU >> 0) & 0xff);
                            byteBuffer[i * 4 + 3] = (byte)((sampleU >> 8) & 0xff);
                        }

                        audioBuffer.AddSamples(byteBuffer, 0, sampleNum * 4);
                    }

                    System.Threading.Thread.Sleep(50);
                }

end:
                audioOut.Stop();
            }

            lock (audioThreads)
                audioThreads.Remove(System.Threading.Thread.CurrentThread);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Add some music sample to the buffer. Not all samples are required to be accepted into the player buffer
        /// </summary>
        /// <param name="channels"></param>
        /// <param name="rate"></param>
        /// <param name="samples"></param>
        /// <param name="frames"></param>
        /// <returns></returns>
        public int EnqueueSamples(int channels, int rate, byte[] samples, int frames)
        {
            //  If we don't yet have a buffer, allocate one and start playing from it as a barground activity
            if (buffer == null)
            {
                buffer = new NAudio.Wave.BufferedWaveProvider(new NAudio.Wave.WaveFormat(rate, channels));
                dso    = new NAudio.Wave.DirectSoundOut(70);
                dso.Init(buffer);
                dso.Play();
            }

            //  Do we have room in the buffer to add all the new samples
            int space = buffer.BufferLength - buffer.BufferedBytes;

            if (space > samples.Length)
            {
                //  Add them all
                buffer.AddSamples(samples, 0, samples.Length);
                return(frames);
            }

            //  None added as there was insufficient room for them all
            return(0);
        }
Ejemplo n.º 5
0
 private static void session_OnMusicDelivered(object sender, Spotify.MusicDeliveryEventArgs e)
 {
     _audioProvider.AddSamples(e.PcmData, 0, e.PcmData.Length);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Feed audio data into the speakers
 /// </summary>
 /// <param name="recv">The audio data bytes</param>
 public void BufferPlay(byte[] recv)
 {
     byte[] copied = new byte[recv.Length];
     Array.Copy(recv, copied, recv.Length);
     provider.AddSamples(copied, 0, copied.Length);
 }