aRts正如标题analog realtime synthesizer,是一个模块化的合成器,可以方便地创建声音、音乐、使用各种过滤器,也可以为aRts写功能模块,并整合到aRts里。
要播放音频,你只需要把波形数据发送给aRts服务器,它就会自动为你处理好并播放出来。
要在你的应用程序中使用aRts,你需要:
1:包含头文件 #include <artsc.h>
2:初始化 arts_init()
3:创建一个播放流 arts_play_stream()
4:设置播放流的属性 arts_stream_set()
5:往流里写数据 arts_write()
6:关闭播放流 arts_close_stream()
7:释放aRts arts_free()
这里以一个wav文件播放器为例解释一下如何使用aRts:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <artsc.h> #define WAV_OK 0 #define WAV_ERROR (-1) #define WAV_CANNOT_OPEN (-2) #define WAV_NOT_SUPPORT (-3) #define WAV_ALREADY_OPEN (-4) #define WAV_8BIT 8 #define WAV_8BIT_UNSIGNED WAV_8BIT #define WAV_16BIT 16 #define WAV_16BIT_SIGNED WAV_16BIT #define WAV_MONO 1 #define WAV_STEREO 2 struct header { char riff_id[4]; unsigned int filesize; char type[4]; char chunk_id[4]; unsigned int size; unsigned short format; unsigned short ch; unsigned int sample_rate; unsigned int data_rate; unsigned short block_size; unsigned short quantize; }; struct chunk { char id[4]; unsigned int size; unsigned char data8[0]; signed short data16[0]; }; unsigned int read_chunk(FILE *fp) { struct chunk chk; while (1) { if (fread(&chk, sizeof(struct chunk), 1, fp) < 1) { return 0; } if (strncmp(chk.id, "data", 4) == 0) { break; } fseek(fp, chk.size, SEEK_CUR); } return chk.size; } int main(int argc, const char *argv[]) { unsigned int leftsize; if (argc != 2) { printf("usage: %s <file.wav>/n", argv[0]); exit (1); } arts_stream_t stream; int errorcode; errorcode = arts_init(); if(errorcode < 0) { fprintf(stderr,"arts_init error: %s/n", arts_error_text(errorcode)); return 1; } FILE* fp = fopen(argv[1], "rb"); if (fp == NULL) { printf("Can not open file ~/a.wav/n"); exit(1); } struct header *hdr = (struct header *)malloc(sizeof(struct header)); if (hdr == NULL) { printf("Malloc error/n"); exit(1); } fread(hdr, sizeof(struct header), 1, fp); if ((strncmp(hdr->riff_id, "RIFF", 4) != 0) || (strncmp(hdr->type, "WAVE", 4) != 0) || (strncmp(hdr->chunk_id, "fmt ", 4) != 0) || (hdr->format != 1) || ((hdr->ch != WAV_MONO) && (hdr->ch != WAV_STEREO)) || ((hdr->quantize != WAV_8BIT) && (hdr->quantize != WAV_16BIT))) { fclose(fp); exit(1); } fseek(fp, (hdr->size - 0x10), SEEK_CUR); if ((leftsize = read_chunk(fp)) == 0) { printf("Wav file error/n"); return WAV_ERROR; } stream = arts_play_stream(hdr->sample_rate, hdr->quantize, hdr->ch, "wavplay"); unsigned int bytes; char buffer[8192]; fseek(fp, 0, SEEK_SET); while((bytes = fread(buffer,1,8192,fp)) > 0) { errorcode = arts_write(stream,buffer,bytes); if(errorcode < 0) { fprintf(stderr,"arts_write error: %s/n",arts_error_text(errorcode)); return 1; } } free(hdr); fclose(fp); arts_close_stream(stream); arts_free(); return 0; }
编译使用:
$ gcc -Wall -o wavplay wavplay.c `artsc-config --cflags` `artsc-config --libs`
觉得麻烦就用Makefile:
all: gcc -o wavplay wavplay.c `artsc-config --cflags` `artsc-config --libs` install: cp wavplay /usr/bin clean: rm -rf *.o wavplay
要测试播放音频,首先要运行aRts的监护进程:
$ artsd
然后启动播放器,并指定你需要播放的文件:
$ wavplay sample.wav
更多资料可以参考:http://www.arts-project.org/