PulseAudioasynchronous库如何用于播放原始PCM数据?【JAVA教程】

!
也想出现在这里? 联系我们
信息

PulseAudioasynchronous库如何用于播放原始PCM数据?,第1张

概述PulseAudioasynchronous库如何用于播放原始PCM数据

pulseAudio文档似乎没有一个明确的解释,我找不到任何简单的例子。

linux下C / C ++的最佳audio回放API?

双耳节拍在C + +

在“控制面板”中添加我自己的应用程序事件 – >声音

USB麦克风描述符为windows XP,7,8应用程序

大多数(如果不是全部的话)audio端点PCM?

像这样

/* pcm-playback: pcm-playback.c gcc -o pcm-playback pcm-playback.c `pkg-config –cflags –libs libpulse` */ #include <stdio.h> #include <assert.h> #include <pulse/pulseaudio.h> #define FORMAT PA_SAMPLE_U8 #define RATE 44100 voID context_state_cb(pa_context* context,voID* mainloop); voID stream_state_cb(pa_stream *s,voID *mainloop); voID stream_success_cb(pa_stream *stream,int success,voID *userdata); voID stream_write_cb(pa_stream *stream,size_t requested_bytes,voID *userdata); int main(int argc,char *argv[]) { pa_threaded_mainloop *mainloop; pa_mainloop_API *mainloop_API; pa_context *context; pa_stream *stream; // Get a mainloop and its context mainloop = pa_threaded_mainloop_new(); assert(mainloop); mainloop_API = pa_threaded_mainloop_get_API(mainloop); context = pa_context_new(mainloop_API,\”pcm-playback\”); assert(context); // Set a callback so we can wait for the context to be ready pa_context_set_state_callback(context,&context_state_cb,mainloop); // Lock the mainloop so that it does not run and crash before the context is ready pa_threaded_mainloop_lock(mainloop); // Start the mainloop assert(pa_threaded_mainloop_start(mainloop) == 0); assert(pa_context_connect(context,NulL,PA_CONTEXT_NOautoSPAWN,NulL) == 0); // Wait for the context to be ready for(;;) { pa_context_state_t context_state = pa_context_get_state(context); assert(PA_CONTEXT_IS_GOOD(context_state)); if (context_state == PA_CONTEXT_READY) break; pa_threaded_mainloop_wait(mainloop); } // Create a playback stream pa_sample_spec sample_specifications; sample_specifications.format = FORMAT; sample_specifications.rate = RATE; sample_specifications.channels = 2; pa_channel_map map; pa_channel_map_init_stereo(&map); stream = pa_stream_new(context,\”Playback\”,&sample_specifications,&map); pa_stream_set_state_callback(stream,stream_state_cb,mainloop); pa_stream_set_write_callback(stream,stream_write_cb,mainloop); // recommended settings,IE server uses sensible values pa_buffer_attr buffer_attr; buffer_attr.maxlength = (uint32_t) -1; buffer_attr.tlength = (uint32_t) -1; buffer_attr.prebuf = (uint32_t) -1; buffer_attr.minreq = (uint32_t) -1; // Settings copIEd as per the chromium browser source pa_stream_flags_t stream_flags; stream_flags = PA_STREAM_START_CORKED | PA_STREAM_INTERPolATE_TIMING | PA_STREAM_NOT_MONOTONIC | PA_STREAM_auto_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY; // Connect stream to the default audio output sink assert(pa_stream_connect_playback(stream,&buffer_attr,stream_flags,NulL) == 0); // Wait for the stream to be ready for(;;) { pa_stream_state_t stream_state = pa_stream_get_state(stream); assert(PA_STREAM_IS_GOOD(stream_state)); if (stream_state == PA_STREAM_READY) break; pa_threaded_mainloop_wait(mainloop); } pa_threaded_mainloop_unlock(mainloop); // Uncork the stream so it will start playing pa_stream_cork(stream,stream_success_cb,mainloop); // Play until we get a character getc(stdin); } voID context_state_cb(pa_context* context,voID* mainloop) { pa_threaded_mainloop_signal(mainloop,0); } voID stream_state_cb(pa_stream *s,voID *mainloop) { pa_threaded_mainloop_signal(mainloop,0); } voID stream_write_cb(pa_stream *stream,voID *userdata) { int bytes_remaining = requested_bytes; while (bytes_remaining > 0) { uint8_t *buffer = NulL; size_t bytes_to_fill = 44100; size_t i; if (bytes_to_fill > bytes_remaining) bytes_to_fill = bytes_remaining; pa_stream_begin_write(stream,(voID**) &buffer,&bytes_to_fill); for (i = 0; i < bytes_to_fill; i += 2) { buffer[i] = (i%100) * 40 / 100 + 44; buffer[i+1] = (i%100) * 40 / 100 + 44; } pa_stream_write(stream,buffer,bytes_to_fill,0LL,PA_SEEK_relative); bytes_remaining -= bytes_to_fill; } } voID stream_success_cb(pa_stream *stream,voID *userdata) { return; }

总结

以上是内存溢出为你收集整理的PulseAudioasynchronous库如何用于播放原始PCM数据?全部内容,希望文章能够帮你解决PulseAudioasynchronous库如何用于播放原始PCM数据?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

© 版权声明
THE END
喜欢就支持一下吧
点赞187 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容