开放式阅读WAV文件lib
OpenAL reading a WAV file lib
我刚刚开始使用我正在构建的游戏引擎的开放式。我的理解是,有些库可以帮助您打开使用.wav文件。我知道Alut被弃用了,但我听说提到了一个更为当前的图书馆,称为Libaudio。但是,我无法在任何地方找到该图书馆。
我的问题是:在哪里可以找到libaudio?还是可以使用我可以使用的更好,维护得更多的图书馆?如果我能提供帮助,我真的不想学习如何打开.wav文件。任何建议都很棒。
我根据这个很棒的教程进行了手动编写:https://www.youtube.com/watch?v=tmvrpnfp9ys
这是代码:
//check big vs little endian machine
static bool IsBigEndian(void)
{
int a = 1;
return !((char*)&a)[0];
}
static int ConvertToInt(char* buffer, int len)
{
int a = 0;
if(!IsBigEndian())
{
for(int i = 0; i < len; ++i)
{
((char*)&a)[i] = buffer[i];
}
}
else
{
for(int i = 0; i < len; ++i)
{
((char*)&a)[3-i] = buffer[i];
}
}
return a;
}
//Location and size of data is found here: http://www.topherlee.com/software/pcm-tut-wavformat.html
static char* LoadWAV(string filename, int& channels, int& sampleRate, int& bps, int& size)
{
char buffer[4];
std::ifstream in(filename.c_str());
in.read(buffer, 4);
if(strncmp(buffer, "RIFF", 4) != 0)
{
std::cout << "Error here, not a valid WAV file, RIFF not found in headern This was found instead: "
<< buffer[0] << buffer[1] << buffer[2] << buffer[3] << std::endl;
}
in.read(buffer, 4);//size of file. Not used. Read it to skip over it.
in.read(buffer, 4);//Format, should be WAVE
if(strncmp(buffer, "WAVE", 4) != 0)
{
std::cout << "Error here, not a valid WAV file, WAVE not found in header.n This was found instead: "
<< buffer[0] << buffer[1] << buffer[2] << buffer[3] << std::endl;
}
in.read(buffer, 4);//Format Space Marker. should equal fmt (space)
if(strncmp(buffer, "fmt ", 4) != 0)
{
std::cout << "Error here, not a valid WAV file, Format Marker not found in header.n This was found instead: "
<< buffer[0] << buffer[1] << buffer[2] << buffer[3] << std::endl;
}
in.read(buffer, 4);//Length of format data. Should be 16 for PCM, meaning uncompressed.
if(ConvertToInt(buffer, 4) != 16)
{
std::cout << "Error here, not a valid WAV file, format length wrong in header.n This was found instead: "
<< ConvertToInt(buffer, 4) << std::endl;
}
in.read(buffer, 2);//Type of format, 1 = PCM
if(ConvertToInt(buffer, 2) != 1)
{
std::cout << "Error here, not a valid WAV file, file not in PCM format.n This was found instead: "
<< ConvertToInt(buffer, 4) << std::endl;
}
in.read(buffer, 2);//Get number of channels.
//Assume at this point that we are dealing with a WAV file. This value is needed by OpenAL
channels = ConvertToInt(buffer, 2);
in.read(buffer, 4);//Get sampler rate.
sampleRate = ConvertToInt(buffer, 4);
//Skip Byte Rate and Block Align. Maybe use later?
in.read(buffer, 4);//Block Align
in.read(buffer, 2);//ByteRate
in.read(buffer, 2);//Get Bits Per Sample
bps = ConvertToInt(buffer, 2);
//Skip character data, which marks the start of the data that we care about.
in.read(buffer, 4);//"data" chunk.
in.read(buffer, 4); //Get size of the data
size = ConvertToInt(buffer, 4);
if(size < 0)
{
std::cout << "Error here, not a valid WAV file, size of file reports 0.n This was found instead: "
<< size << std::endl;
}
char* data = new char[size];
in.read(data, size);//Read audio data into buffer, return.
in.close();
return data;
}
相关文章:
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- CMake 生成的 MSVC 项目找不到符号,即使为其相关 dll 正确生成了 lib 文件也是如此
- 从 .lib 文件创建一个.dll文件
- 看不到文件'SDL2.lib'
- 可执行文件C++包括.obj,.lib和.dll吗?
- 我应该安装 .cmake 文件来共享/cmake 还是 lib/cmake?
- 链接:致命错误LNK1104:无法打开文件"python37_d.lib"
- 在 Windows 10 下使用 GTest 的 CMake - 致命错误 LNK1104:无法打开文件"gtest.lib",但存在调试"gtestd.lib"
- 链接器找不到导入的 DLL 的 LIB 文件
- LNK1104无法打开文件"legacy_stdio_definitions.lib"
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 在Qt中链接具有非标准名称的lib文件
- bitbake-grpc交叉编译/配置失败,错误为c-ares::care引用文件/usr/lib/libcares.s
- C++-在没有自定义.lib文件的情况下从Lua C模块调用Lua函数
- LNK 1104 无法打开文件 'glfw3.lib
- 是否可以将 EXE 文件作为 lib 文件链接到另一个项目?
- C++ CLR dll 需要 .lib 文件,但非 CLR 不需要
- G++ 无法链接 curl lib 文件
- 开放式阅读WAV文件lib
- LNK1104无法打开文件'...lib.obj'