本篇文章给大家谈谈c语言怎么写打开wav,以及c语言用什么打开对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、怎么用c语言播放特定目录里的wav文件?2、OC语言读取wav文件3、怎样用c语言读取WAV文件振幅信息4、c语言中如何播放wav格式的声音函数
怎么用c语言播放特定目录里的wav文件?
用C语言播放音乐,一般需要额外的库或者调用系统的API函数。
以Windows为例,可以通过PlaySound函数播放wav格式的音乐。示例如下,代码在vc6.0中编译通过。
#includestdio.h
#includewindows.h
#includemmsystem.h
#pragma
comment(lib,
“WINMM.LIB”)
int
main()
{
PlaySound(TEXT(“1.wav”),0,SND_FILENAME);
//1.wav是要播放的音乐文件
return
0;
}
如果想播放mp3音乐可以使用如下代码。
注意:生成程序后,请在cmd中执行此程序,不要在vc/vs的IDE中运行。#include
windows.h
#include
mmsystem.h
#include
stdio.h
#pragma
comment(lib,
“Winmm.lib”)
int
main(int
argc,
char
*argv[])
{
//绝对地址形式
TCHAR
fileName[]=”ganlusi.mp3″;
TCHAR
shortName[MAX_PATH];
GetShortPathName(fileName,shortName,sizeof(shortName)/sizeof(TCHAR));
TCHAR
cmd[MAX_PATH+10];
wsprintf(cmd,”play
%s”,shortName);
mciSendString(cmd,NULL,
0,
NULL);
Sleep(5
*
60
*
1000);
//这里是防止一播放就结束做的延迟
return
0;
}
OC语言读取wav文件
用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);
怎样用c语言读取WAV文件振幅信息
C也能像matlab一样读取wav格式文件!
用外置库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 = 0; i num; i += c){
for (j = 0; j c; ++j){
if ((i+j)length) {
output[i+j] = buf[i+j];
}
}
}
}
c语言中如何播放wav格式的声音函数
如果是播放wav文件 。
BOOL PlaySound( LPCSTR lpszSound, HMODULE hmod, DWORD fuSound );
lpszSound 声音文件的路径和文件名
hmod 文件播放为NULL,加载到内存后播放需提供handle. 原文(This parameter must be NULL unless SND_RESOURCE is specified in fdwSound. )
fuSound 播放方式(这个太多,自己查吧。)
Header: Declared in mmsystem.h.
Import Library: Use winmm.lib.
第一次抄MSDN。
c语言怎么写打开wav的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言用什么打开、c语言怎么写打开wav的信息别忘了在本站进行查找喔。