如何在C上在Linux上生成声音?

我需要一种方法来在Linux上的C程序中播放某些音符。 当使用windows时,可以使用#include <dos.h>并使用sound(note/frequency)delay(time in ms)和自我解释nosound()等直接的函数。 Linux上有什么并行的吗? 谢谢

我喜欢libao上面的提示 – 我只是试了一下,效果很好。 这是使用OpenAL渲染音频的相似水平的复杂性

 // sudo apt-get install libopenal-dev // gcc -o openal_play_monday openal_play_monday.c -lopenal -lm #include <stdio.h> #include <stdlib.h> // gives malloc #include <math.h> #ifdef __APPLE__ #include <OpenAL/al.h> #include <OpenAL/alc.h> #elif __linux #include <AL/al.h> #include <AL/alc.h> #endif ALCdevice * openal_output_device; ALCcontext * openal_output_context; ALuint internal_buffer; ALuint streaming_source[1]; int al_check_error(const char * given_label) { ALenum al_error; al_error = alGetError(); if(AL_NO_ERROR != al_error) { printf("ERROR - %s (%s)\n", alGetString(al_error), given_label); return al_error; } return 0; } void 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 alGenBuffers(1, & internal_buffer); al_check_error("failed call to alGenBuffers"); } void MM_exit_al() { ALenum errorCode = 0; // Stop the sources alSourceStopv(1, & streaming_source[0]); // streaming_source int ii; for (ii = 0; ii < 1; ++ii) { alSourcei(streaming_source[ii], AL_BUFFER, 0); } // Clean-up alDeleteSources(1, &streaming_source[0]); alDeleteBuffers(16, &streaming_source[0]); errorCode = alGetError(); alcMakeContextCurrent(NULL); errorCode = alGetError(); alcDestroyContext(openal_output_context); alcCloseDevice(openal_output_device); } void MM_render_one_buffer() { /* Fill buffer with Sine-Wave */ // float freq = 440.f; float freq = 100.f; float incr_freq = 0.1f; int seconds = 4; // unsigned sample_rate = 22050; unsigned sample_rate = 44100; double my_pi = 3.14159; size_t buf_size = seconds * sample_rate; // allocate PCM audio buffer short * samples = malloc(sizeof(short) * buf_size); printf("\nhere is freq %f\n", freq); int i=0; for(; i<buf_size; ++i) { samples[i] = 32760 * sin( (2.f * my_pi * freq)/sample_rate * i ); freq += incr_freq; // incr_freq += incr_freq; // freq *= factor_freq; if (100.0 > freq || freq > 5000.0) { incr_freq *= -1.0f; } } /* upload buffer to OpenAL */ alBufferData( internal_buffer, AL_FORMAT_MONO16, samples, buf_size, sample_rate); al_check_error("populating alBufferData"); free(samples); /* Set-up sound source and play buffer */ // ALuint src = 0; // alGenSources(1, &src); // alSourcei(src, AL_BUFFER, internal_buffer); alGenSources(1, & streaming_source[0]); alSourcei(streaming_source[0], AL_BUFFER, internal_buffer); // alSourcePlay(src); alSourcePlay(streaming_source[0]); // --------------------- ALenum current_playing_state; alGetSourcei(streaming_source[0], AL_SOURCE_STATE, & current_playing_state); al_check_error("alGetSourcei AL_SOURCE_STATE"); while (AL_PLAYING == current_playing_state) { printf("still playing ... so sleep\n"); sleep(1); // should use a thread sleep NOT sleep() for a more responsive finish alGetSourcei(streaming_source[0], AL_SOURCE_STATE, & current_playing_state); al_check_error("alGetSourcei AL_SOURCE_STATE"); } printf("end of playing\n"); /* Dealloc OpenAL */ MM_exit_al(); } // MM_render_one_buffer int main() { MM_init_al(); MM_render_one_buffer(); } 

如果你想进一步采取OpenAL …在这个问题上的胆小鬼

github源代码

开箱即用的OpenAL播放PCM音频的缓冲区就好了…但是它将演习视为一个流媒体。 在那个github回购中,我写了一个使用OpenAL的音频服务器,它实现了播放流式音频…享受

Windows使用它自己的唯一的声音体系结构,因此您可以访问sound()例程。

不同的Linux机器,根据安装的软件包,可能需要不同的方法。 也许实用程序的beep ( 在stackexchange这个问题出来)可以引导你正确的方向

单程

包括#include<conio.h>和side main()或者你想使用call print(“\ a”)的地方

printf("\a");

第二种方式

包括头文件

 #include <windows.h> 

并调用函数Beep(500, 500);

Beep(freq, dur); 其中freq =嘟嘟声的频率,其中int和dutation也是int