用外置库libsndfile,可以读取数据,频率,通道等等
以下是一段读取sample.wav,将数据存入double格式的array:
#include "sndfile.h"
#pragma comment(lib, "libsndfile-1.lib")
void readwav(double output[length]){
SNDFILE *sf
SF_INFO info
int num_channels
int num, num_items
double *buf
int f,sr,c
int i,j
info.format = 0
sf = sf_open("sample.wav",SFM_READ,&info)
if (sf == NULL){
printf("Failed to open the file.\n")
exit(-1)
}
f = info.frames
sr = info.samplerate
c = info.channels
printf("frames=%d\n",f)
printf("samplerate=%d\n",sr)
printf("channels=%d\n",c)
num_items = f*c
printf("num_items=%d\n",num_items)
buf = (double *)malloc(num_items*sizeof(double))
num = sf_read_double(sf,buf,num_items)
sf_close(sf)
printf("Read %d items\n",num)
for (i = 0i <numi += c){
for (j = 0j <c++j){
if ((i+j)<length) {
output[i+j] = buf[i+j]
}
}
}
}
用AudioServicesPlaySystemSound进行播放,代码如下:(wav文件名为end.wav)SystemSoundID ph
id sndpath = [[NSBundle mainBundle]pathForResource:@"end" ofType:@"wav" inDirectory:@"/"]
CFURLRef baseUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:sndpath]
AudioServicesCreateSystemSoundID(baseUrl, &ph)
AudioServicesPlaySystemSound(ph)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)