我需要一種方法在Linux上的C程序中播放某些音符。 使用窗口時,有可能使用#include <dos.h>
並使用直接轉發功能,如sound(note/frequency)
,delay(time in ms)
和自我解釋nosound()
。 Linux上有什麼並行的東西嗎? 謝謝如何在Linux上製作C語言的聲音?
回答
Windows使用自己的唯一的聲音體系結構,因此您可以訪問sound()
例程。
根據安裝的軟件包,不同的Linux機器可能需要不同的方法。 也許效用beep
(滿分this question on stackexchange)可以指導你正確的方向
我喜歡有關禮包上面的提示 - 我只是給它一個嘗試,它工作得很好。下面是一個使用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 ......採取甘德在這個
開箱即用的OpenAL發揮着複雜的相似水平PCM音頻的緩衝器很好......但是它將演習視爲一種流式播放的能力。在GitHub庫我寫了使用的OpenAL它實現播放流音頻的音頻服務器...享受
alDeleteBuffers(16,&stream_source [0])中的'16'是什麼? – user2029077 2015-09-10 06:13:20
是否要刪除緩衝區刪除源? – user2029077 2015-09-10 09:08:08
單程
包括 #include<conio.h>
和側面的main()或您希望使用的呼叫打印(」 \ A「)
printf("\a");
第二方式
包括頭文件
#include <windows.h>
和調用函數 Beep(500, 500);
Beep(freq, dur);
其中FREQ =蜂鳴聲頻率爲int和在dutation也INT [需要ALSA教程]的
- 1. 如何在Linux上用C語言解析xml文件
- 2. 在Windows上錄製聲音作爲C#程序的一部分
- 3. 如何在android中調用錄製聲音/語音功能
- 4. C語言中的Windows語音識別#
- 5. C#語言枚舉聲明
- 6. 帶有錄製聲音剪輯的android上的語音識別?
- 7. SKMaps導航語言:如何改變從米到碼的聲音
- 8. 如何製作此聲音循環?
- 9. 如何製作多語言C#控制檯應用程序?
- 10. 在互聯網上錄製音頻聲音 - C#ASP.NET
- 11. 在linux上創建可執行的C語言補充程序
- 12. 聲音操作反向/在iPhone上向後播放錄製的聲音
- 13. 在Linux上找出默認語言
- 14. 聲音API Ubuntu Linux
- 15. 如何在整個屏幕上製作語音識別器rwork
- 16. 在C語言中工作
- 17. 如何製作跨平臺的C++內聯彙編語言?
- 18. 用Linux編程C語言的參數
- 19. 在Linux上爲C程序製作makefile
- 20. 信號如何在linux(c)上工作?
- 21. 如何使用錄製的聲音作爲鈴聲
- 22. 如何製作語言綁定?
- 23. 如何製作多語言網站?
- 24. 如何爲目標語言製作CFLocale?
- 25. 如何製作多語言TabBarItem
- 26. 如何製作Crystal Reports多語言?
- 27. 聲音類型系統的語言是強類型語言的子集嗎?
- 28. 在Linux中如何表示操作系統語言
- 29. 如何使用Java製作語音API
- 30. 語音轉換器,如何製作?
可能重複(http://stackoverflow.com/questions/8485553/alsa-tutorial-required) – DevNull 2014-10-27 16:40:55
鏈接的副本沒有真正回答問題。它只是鏈接到一堆教程。 – cpburnz 2014-10-27 17:23:20
您可能會對'libao' [documentation](http://xiph.org/ao/doc/)感興趣。這將允許您播放PCM音頻數據。如果你想要類似蜂鳴器的聲音,那就好了... – francis 2014-10-27 17:33:50