我的 10 线音频播放器

发布于 2024-10-04 01:58:31 字数 434 浏览 4 评论 0原文

我只想制作最简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┊风居住的梦幻卍 2024-10-11 01:58:31

我认为,您应该对 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.

本王不退位尔等都是臣 2024-10-11 01:58:31

本地data没有分配存储空间,但您的代码正在读取和写入它。根据大小,您似乎正在尝试在循环的每次迭代中读取和写入 16 字节的存储。使用以下内容代替

char data[16];
while(!feof(music)){
    fread(data, sizeof(char), sizeof(data), music);
    fwrite(data, sizeof(char), sizeof(data), speaker);
}

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

char data[16];
while(!feof(music)){
    fread(data, sizeof(char), sizeof(data), music);
    fwrite(data, sizeof(char), sizeof(data), speaker);
}
花落人断肠 2024-10-11 01:58:31

怎么样

#include <stdio.h> 

int main(){ 
    FILE* speaker = fopen("/dev/audio", "wb"); 
    FILE* music = fopen("bugsbunny2.wav", "rb"); 
    char data[16]; 
    while(!feof(music)){ 
        fwrite(data, sizeof(data), 
            fread(data, sizeof(char), sizeof(data), music),
            speaker); 
    } 
    return 0; 
} 

fread 返回实际读取的字节数。

然而,我不确定你是否可以将任何 wav 文件放入音频设备中,并听到有用的东西......我认为“音频”不是 WAV 消费者。

也尝试使用 Data[1024] 或 Data[16384]。

How about

#include <stdio.h> 

int main(){ 
    FILE* speaker = fopen("/dev/audio", "wb"); 
    FILE* music = fopen("bugsbunny2.wav", "rb"); 
    char data[16]; 
    while(!feof(music)){ 
        fwrite(data, sizeof(data), 
            fread(data, sizeof(char), sizeof(data), music),
            speaker); 
    } 
    return 0; 
} 

?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文