这是读取文件格式的代码...我想播放wav声音(test.wav)? [英] this is code to read file format ...i want to play wav sound (test.wav) ?

查看:268
本文介绍了这是读取文件格式的代码...我想播放wav声音(test.wav)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#include <fstream>
#include "stdafx.h"

using namespace std;
int endWithError(char *msg,int error =0)
{
	cout<< msg << endl;
	while(cin.get() != 10)
	return error;
}
int _tmain(int argc, _TCHAR* argv[])
{
	
	FILE *fp= NULL;
	fp=fopen("test.wav","r");
	if(!fp)
	{
		endWithError("Error : File Not Open");
	}
	char type[4];
	DWORD size,chunkSize;
	short formatType, channels;
	DWORD sampleRate, avgBytesPerSec;
	short bytesPerSample, bitsPerSample;
	DWORD dataSize;

	fread(type, sizeof(char), 4, fp);
	if(!strcmp(type,"RIFF"))
	{
		return endWithError("Error : NOT RIFF EORMAT");
	}
	fread(&size, sizeof(DWORD), 1, fp);
	fread(type, sizeof(char), 4, fp);
	if(!strcmp(type,"WAVE"))
	{
		endWithError("Error : Not Wave Format");
	}
	fread(type, sizeof(char), 4,fp);
	if(!strcmp(type,"fmt"))
	{
		endWithError("Error : Not Fmt ");
	}
	fread(&chunkSize, sizeof(DWORD), 1, fp);
	fread(&formatType, sizeof(short), 1, fp);
	fread(&channels, sizeof(short), 1, fp);
	fread(&sampleRate, sizeof(DWORD), 1, fp);
	fread(&avgBytesPerSec, sizeof(DWORD), 1, fp);
	fread(&bytesPerSample, sizeof(short), 1, fp);
	fread(&bitsPerSample, sizeof(short), 1, fp);
	fread(type, sizeof(char), 4,fp);
	if(!strcmp(type,"data"))
	{
		endWithError("Error : Error Missing Data");
	}
	fread(&dataSize, sizeof(DWORD), 1, fp);
	cout << "chunk Size: " << chunkSize << endl;
	cout <<"Format Type: "<< formatType <<endl;
	cout <<"Channels: "<< channels <<endl;
	cout <<"Sample Rate: "<< sampleRate <<endl;
	cout <<"Average Bytes Per Sec: "<< avgBytesPerSec <<endl;
    cout <<"Bytes Per Sample: "<< bytesPerSample <<endl;
    cout <<"Bits Per Sample: "<< bitsPerSample <<endl;
    cout <<"Press Enter To End"<< endl;

	while(cin.get() != 10);

	return 0;
}

推荐答案

正如Andrew Cherednik写道:
这个简单的函数sndPlaySound()将播放您的test.wav文件.更好的链接: MSDN中的说明

提示:请使用完整路径并检查是否找到了文件.
As Andrew Cherednik wrote:
This simple function sndPlaySound() will play your test.wav file. A better link to it: Explanation in MSDN

Tip: use a full path and check that file is found.


这篇关于这是读取文件格式的代码...我想播放wav声音(test.wav)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆