我的 10 线音频播放器
我只想制作最简单的WAV播放器。 这是我的代码。
#include <stdio.h>
int main(){
FILE* speaker = fopen("/dev/audio", "wb");
FILE* music = fopen("bugsbunny2.wav", "rb");
char data[16];
while(!feof(music)){
fread(data, sizeof(char), sizeof(data), music);
fwrite(data, sizeof(char), sizeof(data), speaker);
}
return 0;
}
这会播放声音,但是太吵了。很难理解。 我想我缺少缓冲区/块大小 实际块大小是多少? 或如何获得实际块大小?
I just want to make the Simplest WAV Player.
Here is MyCode.
#include <stdio.h>
int main(){
FILE* speaker = fopen("/dev/audio", "wb");
FILE* music = fopen("bugsbunny2.wav", "rb");
char data[16];
while(!feof(music)){
fread(data, sizeof(char), sizeof(data), music);
fwrite(data, sizeof(char), sizeof(data), speaker);
}
return 0;
}
This Plays the Sound However Its too Noisy. Hard to understand.
I think I am missing on Buffer/Chunk Size
Whats the Actual Chunk Size ?
or How can I get the Actual Chunk Size ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为,您应该对 wav 文件进行一些基本解析(另外,如果需要的话,将字节从 Little Endian 转换为 Big Endian [顺便说一句,可能,这是噪声源]),或者至少跳过这对你来说并不有趣,因为现在你甚至不知道文件中样本的大小(例如,在我得到的测试 wav 文件之一中,样本大小是 24 字节)。
我建议您阅读WAV 文件规范。
I think, that you should to do some basic parsing of the wav file (plus, convert bytes from Little Endian to Big Endian, if needed [BTW, probably, this is a source of the noise]) or, at least, skip the part which is not interesting for you, because now you're even have no idea about size of the samples in the file (for example, in one of test wav file, that I got, sample size is 24 byte).
I'm recommending you to read WAV file specification.
本地
data
没有分配存储空间,但您的代码正在读取和写入它。根据大小,您似乎正在尝试在循环的每次迭代中读取和写入 16 字节的存储。使用以下内容代替The local
data
has no allocated storage but your code is reading and writing to it. Based on the sizes though it looks like you're trying to read and write 16 bytes of storage on every iteration of the loop. Use the following instead怎么样
?
fread
返回实际读取的字节数。然而,我不确定你是否可以将任何 wav 文件放入音频设备中,并听到有用的东西......我认为“音频”不是 WAV 消费者。
也尝试使用 Data[1024] 或 Data[16384]。
How about
?
fread
returns the number of bytes actually read.I am not sure, however, if you can just stuff any wav file into the audio device, and hear useful stuff..."audio" is not a WAV consumer I think.
Try using Data[1024] or Data[16384], too.