Beispiel #1
0
        private void ConvertAudioSample(ref WaveDataType input, IntPtr sample)
        {
            int    ret;
            IntPtr swr = AV.swr_alloc_set_opts(IntPtr.Zero,
                                               AV.av_get_default_channel_layout(codecCtx.channels),
                                               AV.AVSampleFormat.AV_SAMPLE_FMT_S16,
                                               codecCtx.sample_rate,
                                               AV.av_get_default_channel_layout(codecCtx.channels),
                                               codecCtx.sample_fmt,
                                               codecCtx.sample_rate,
                                               0,
                                               IntPtr.Zero);

            ret = AV.swr_init(swr);

            int needed_buf_size = AV.av_samples_get_buffer_size(IntPtr.Zero,
                                                                codecCtx.channels,
                                                                input.nb_samples,
                                                                AV.AVSampleFormat.AV_SAMPLE_FMT_S16, 0);
            IntPtr pOutput  = Marshal.AllocCoTaskMem(needed_buf_size);
            IntPtr ppOutput = Marshal.AllocCoTaskMem(Marshal.SizeOf(pOutput));

            Marshal.WriteIntPtr(ppOutput, pOutput);

            int len = AV.swr_convert(swr, ppOutput, input.nb_samples, sample, input.nb_samples);

            int output_len = len * 2 * AV.av_get_bytes_per_sample(AV.AVSampleFormat.AV_SAMPLE_FMT_S16);

            input.managedData = new byte[output_len];
            Marshal.Copy(pOutput, input.managedData, 0, output_len);

            Marshal.FreeCoTaskMem(pOutput);
            Marshal.FreeCoTaskMem(ppOutput);

            IntPtr ppSwr = Marshal.AllocCoTaskMem(Marshal.SizeOf(swr));

            Marshal.WriteIntPtr(ppSwr, swr);
            AV.swr_free(ppSwr);
            Marshal.FreeCoTaskMem(ppSwr);

            input.bit_per_sample = AV.av_get_bits_per_sample_fmt(AV.AVSampleFormat.AV_SAMPLE_FMT_S16);
            input.channel        = codecCtx.channels;
            input.fmt            = AV.AVSampleFormat.AV_SAMPLE_FMT_S16;
            input.size           = needed_buf_size;
        }
Beispiel #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 (waveOut == IntPtr.Zero)
            {
                WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(rate, bit, channel);
                ret = WaveNative.waveOutOpen(out waveOut, -1, fmt, null, 0, WaveNative.CALLBACK_NULL);
                if (ret != WaveNative.MMSYSERR_NOERROR)
                    throw new Exception("can not open wave device");
            }


            queue.Enqueue(type);
        }
Beispiel #3
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);
        }
Beispiel #4
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);
		}
Beispiel #5
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 (waveOut == IntPtr.Zero)
            {
                WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(rate, bit, channel);
                ret = WaveNative.waveOutOpen(out waveOut, -1, fmt, null, 0, WaveNative.CALLBACK_NULL);
                if (ret != WaveNative.MMSYSERR_NOERROR)
                {
                    throw new Exception("can not open wave device");
                }
            }


            queue.Enqueue(type);
        }
Beispiel #6
0
        private void ConvertAudioSample(ref WaveDataType input, IntPtr sample)
        {
            int ret;
            IntPtr swr = AV.swr_alloc_set_opts(IntPtr.Zero,
                                            AV.av_get_default_channel_layout(codecCtx.channels),
                                            AV.AVSampleFormat.AV_SAMPLE_FMT_S16,
                                            codecCtx.sample_rate,
                                            AV.av_get_default_channel_layout(codecCtx.channels),
                                            codecCtx.sample_fmt,
                                            codecCtx.sample_rate,
                                            0,
                                            IntPtr.Zero);
            ret = AV.swr_init(swr);

            int needed_buf_size = AV.av_samples_get_buffer_size(IntPtr.Zero,
                                                                codecCtx.channels,
                                                                input.nb_samples,
                                                                AV.AVSampleFormat.AV_SAMPLE_FMT_S16, 0);
            IntPtr pOutput = Marshal.AllocCoTaskMem(needed_buf_size);
            IntPtr ppOutput = Marshal.AllocCoTaskMem(Marshal.SizeOf(pOutput));
            Marshal.WriteIntPtr(ppOutput, pOutput);

            int len = AV.swr_convert(swr, ppOutput, input.nb_samples, sample, input.nb_samples);

            int output_len = len * 2 * AV.av_get_bytes_per_sample(AV.AVSampleFormat.AV_SAMPLE_FMT_S16);
            input.managedData = new byte[output_len];
            Marshal.Copy(pOutput, input.managedData, 0, output_len);

            Marshal.FreeCoTaskMem(pOutput);
            Marshal.FreeCoTaskMem(ppOutput);

            IntPtr ppSwr = Marshal.AllocCoTaskMem(Marshal.SizeOf(swr));
            Marshal.WriteIntPtr(ppSwr, swr);
            AV.swr_free(ppSwr);
            Marshal.FreeCoTaskMem(ppSwr);

            input.bit_per_sample = AV.av_get_bits_per_sample_fmt(AV.AVSampleFormat.AV_SAMPLE_FMT_S16);
            input.channel = codecCtx.channels;
            input.fmt = AV.AVSampleFormat.AV_SAMPLE_FMT_S16;
            input.size = needed_buf_size;

        }