示例#1
0
        void PlayUsingASound(AudioFrame frame)
        {
            var type = frame.WaveDate;
            int ret  = 0;

            if (pcm == IntPtr.Zero)
            {
                int dir;
                int rate = type.bit_rate == 0 ? 44100 : type.bit_rate;

                int channel = type.channel == 0 ? 2 : type.channel;

                string device = "default";
                ret = Asound.snd_pcm_open(out pcm, device,
                                          Asound.snd_pcm_stream_t.SND_PCM_STREAM_PLAYBACK,
                                          0);
                if (ret < 0)
                {
                    string err = Asound._snd_strerror(ret);
                    Console.WriteLine(err);
                    pcm = IntPtr.Zero;
                    return;
                }
                else
                {
                    Console.WriteLine("open audio device ok, pcm is {0}", pcm);
                }
                IntPtr param = Asound.snd_pcm_hw_params_alloca();
                ret = Asound.snd_pcm_hw_params_any(pcm, param);
                ret = Asound.snd_pcm_hw_params_set_access(pcm, param, Asound.snd_pcm_access_t.SND_PCM_ACCESS_RW_INTERLEAVED);
                ret = Asound.snd_pcm_hw_params_set_format(pcm, param, Asound.snd_pcm_format_t.SND_PCM_FORMAT_S16_LE);
                ret = Asound.snd_pcm_hw_params_set_channels(pcm, param, channel);
                int val = rate;
                ret = Asound.snd_pcm_hw_params_set_rate_near(pcm, param, ref val, out dir);
                ulong frames = (ulong)type.nb_samples;
                ret = Asound.snd_pcm_hw_params_set_period_size_near(pcm, param, ref frames, out dir);
                ret = Asound.snd_pcm_hw_params(pcm, param);
                Asound.snd_pcm_params_free(param);
            }
            if (pcm == IntPtr.Zero)
            {
                return;
            }

            //long r = Asound.snd_pcm_writei(pcm, type, (ulong)type.nb_samples);
            //Console.WriteLine("snd_pcm_writei {0}:{1}, return {2}", type.sample, type.nb_samples, r);
        }
示例#2
0
        public void PutSample(WaveDataType type)
        {
            int ret;
            int size    = type.size;
            int rate    = type.sample_rate == 0 ? 44100 : type.sample_rate;
            int bit     = type.bit_per_sample == 0 ? 16 : type.bit_per_sample;
            int channel = type.channel == 0 ? 2 : type.channel;

            if (pcm == IntPtr.Zero)
            {
                int dir;

                string device = "default";
                ret = Asound.snd_pcm_open(out pcm, device,
                                          Asound.snd_pcm_stream_t.SND_PCM_STREAM_PLAYBACK,
                                          0);
                if (ret < 0)
                {
                    string err = Asound._snd_strerror(ret);
                    Console.WriteLine(err);
                    pcm = IntPtr.Zero;
                    return;
                }
                else
                {
                    Console.WriteLine("open audio device ok, pcm is {0}", pcm);
                }
                IntPtr param = Asound.snd_pcm_hw_params_alloca();
                ret = Asound.snd_pcm_hw_params_any(pcm, param);
                ret = Asound.snd_pcm_hw_params_set_access(pcm, param, Asound.snd_pcm_access_t.SND_PCM_ACCESS_RW_INTERLEAVED);
                ret = Asound.snd_pcm_hw_params_set_format(pcm, param, Asound.snd_pcm_format_t.SND_PCM_FORMAT_S16_LE);
                ret = Asound.snd_pcm_hw_params_set_channels(pcm, param, channel);
                int val = rate;
                ret = Asound.snd_pcm_hw_params_set_rate_near(pcm, param, ref val, out dir);
                ulong frames = (ulong)type.nb_samples;
                ret = Asound.snd_pcm_hw_params_set_period_size_near(pcm, param, ref frames, out dir);
                ret = Asound.snd_pcm_hw_params(pcm, param);
                Asound.snd_pcm_params_free(param);
            }


            queue.Enqueue(type);
        }