I want a way to play some music notes in Linux on my C program. When using windows, #include & lt; Dos.h & gt;
is possible and use a direct forward function such as sound (note / frequency)
, delay (time MS)
, and explaining to yourself Nosound ()
. What is parallel on Linux? Thank you
I like the top tip about libava - I gave it a try and it Works from The same level of complexity using the openale to render the audio is here
// sudo apt-get install libopenal-dev // gcc -o openal_play_monday openal_play_monday.c -lopenal -lm #include & lt; Stdio h & gt; # Include & lt; Stdlib.h & gt; // gives malloc #include & lt; Math.h> #ifdef __APPLE__ #include & lt; OpenAL / al.h & gt; #include & lt; OpenAL / alc.h & gt; #elif __linux #include & lt; AL / al.h & gt; # Include & lt; AL / alc.h & gt; #endif ALCdevice * openal_output_device; ALCcontext * openal_output_context; Aliunt inner-buffer; Elite Streaming_Source [1]; Int al_check_error (const char * given_label) {alanam al_arar; Al_error = alGetError (); If (AL_NO_ERROR! = Al_error) {printf ("error -% s (% s) \ n", algebra strings (al_rar), given_label); Return al_error; } Return 0; } Zero MM_init_al () {const char * defname = alcGetString (NULL, ALC_DEFAULT_DEVICE_SPECIFIER); Openal_output_device = alcOpenDevice (defname); Openal_output_context = alcCreateContext (openal_output_device, NULL); AlcMakeContextCurrent (openal_output_context); // Setup buffer and source algeb buffers (1, and internal buffer); Al_check_error ("Failed to call algebn buffers"); } Zero MM_exit_al () {alanum errorCode = 0; // Stop the sources alsourcetopv (1, & streaming_source [0]); // stream_source int ii; (Ii = 0; ii & lt; 1; ++ ii) {alsso (streaming_source [ii], ALBUFFER, 0); } // Clean up alDeleteSources (1, & Streaming_source [0]); AlDeleteBuffers (16, and streaming_source [0]); ErrorCode = alGetError (); AlcMakeContextCurrent (zero); ErrorCode = alGetError (); AlcDestroyContext (openal_output_context); AlcCloseDevice (openal_output_device); } Fill buffer with zero MM_render_one_buffer () {/ * sign-wave * / // float freq = 440.f; Float frequency = 100. f; Float ink_freak = 0.1f; Int seconds = 4; // unsigned sample_rate = 22050; Unsigned sample_rate = 44100; Double my_pi = 3.14159; Size_t buf_size = seconds * sample_tate; // allocated PCM audio buffer miniature * samples = molok (size) (small) * buf_size; Printf ("\ nhere freq% f \ n", is freq); Int i = 0; For (; i & lt; buf_size; ++ i) {samples [i] = 32760 * sin ((2.f * my_pi * freq) / sample_yet * i); Freq + = incr_freq; // incr_freq + = incr_freq; // freq * = factor_freq; If (100.0> freq || freq> 5000.0) {incr_freq * = -1.0f; }} / * Upload buffer OpenAL * / alBufferData (internal_buffer, AL_FORMAT_MONO16, samples, buf_size, sample_rate); Al_check_error ("popularizes albepardetta"); Free Sample); / * Set-up sound source and play buffer * / // ALuint src = 0; // alGenSources (1, & src); // algorithm (src, AL_BUFFER, internal_buffer); Algebn Source (1, and Streaming_source [0]); Alosory (streaming_source [0], ALBUFFER, internal buffer); // algorithm (src); AlSourcePlay (streaming_source [0]); // --------------------- alanam current_partition_state; AlGetSourcei (streaming_source [0], AL_SOURCE_STATE, and current_partition_state); Al_check_error ("alGetSourcei AL_SOURCE_STATE"); While (AL_PLAYING == current_playing_state) {printf ("Still Playing ... So So \ n"); Sleep (1); / / Should use a thread sleeping not to sleep () to complete more sensitive alGetSourcei (streaming_source [0], AL_SOURCE_STATE, and current_playing_state); Al_check_error ("alGetSourcei AL_SOURCE_STATE"); } Printf ("play ending \ n"); / * Deloop Openech * / MM_exit_al (); } // MM_render_one_buffer int main () {MM_init_al (); MM_render_one_buffer (); }
If you want to take the OpenEle forward ... make a mess on it
Open a buffer outside the box PCM's audio is just fine ... but it leaves the ability to play a stream in the form of a practice, in that Gitob repo I wrote an audio server using the OpenLL that plays streaming audio ... Enjoy
Comments
Post a Comment