#include "Tools.h"
#include "WAVFormat.h"
#include <stdio.h>
#include <fcntl.h>
#include <linux/soundcard.h>
struct WAV_HEADER mWaveHeader;
int music_fd;
void parse(const char *pWaveFile, struct WAV_HEADER *pHeader) {
if((music_fd = open(pWaveFile, O_RDONLY)) == -1)
error("cannot open file: %s", pWaveFile);
if(read(music_fd, pHeader, 20) <= 0)
error("error reading file: %s", pWaveFile);
if(read(music_fd, &pHeader->nEncodeTag, pHeader->nFormatSize) <= 0)
error("error reading format chunk");
if(read(music_fd, &pHeader->cDataHeader, 8) <= 0)
error("error reading data chunk");
if(pHeader->cDataHeader[0] != 'd') {
printf("skip factory chunk\n");
if(lseek(music_fd, pHeader->nDataBytes, SEEK_CUR) == -1)
error("error skipping factor chunk");
if(read(music_fd, &pHeader->cDataHeader, 8) <= 0)
error("error re-read data chunk");
}
}
void print(struct WAV_HEADER *p) {
printChars("riff header: ", p->cRiffHeader, 4);
printf("riff size: %d\n", p->nRiffSize);
printChars("riff type: ", p->cRiffTypeID, 4);
printChars("format header: ", p->cFormatHeader, 4);
printf("format chunk size: %d\n", p->nFormatSize);
printf("format encode tag: %d\n", p->nEncodeTag);
printf("channels: %d\n", p->nChannels);
printf("sample rate: %dHz\n", p->nSampleRate);
printf("bytes per sec: %d bytes\n", p->nBytesPerSec);
printf("block align: %d bytes\n", p->nBlockAlign);
printf("bits per sample: %d bits\n", p->nBitsPerSample);
printChars("data header: ", p->cDataHeader, 4);
printf("data chunk size: %d\n\n", p->nDataBytes);
}
void playback(struct WAV_HEADER *pHeader) {
int fd;
int stereo, sampleFormat, sampleRate;
char buf[1024];
int num_read = 1024, num_write = 1024, offset = 0;
if((fd = open("/dev/dsp", O_WRONLY)) == -1)
error("cannot open audio device");
stereo = pHeader->nChannels > 1;
if(ioctl(fd, SNDCTL_DSP_STEREO, &stereo) == -1)
error("sound card do not support stereo");
sampleFormat = AFMT_S16_LE;
if(pHeader->nBitsPerSample == 8)
sampleFormat = AFMT_U8;
if(ioctl(fd, SNDCTL_DSP_SETFMT, &sampleFormat) == -1)
error("error set audio format");
sampleRate = pHeader->nSampleRate;
if(ioctl(fd, SNDCTL_DSP_SPEED, &sampleRate) == -1)
error("error set sample rate");
printf("playback started...\n");
if(pHeader->nBitsPerSample == 24) {
num_read = 3; num_write = 2; offset = 1;
}
while(read(music_fd, buf, num_read) > 0)
if(write(fd, buf+offset, num_write) == -1)
error("error writing dsp device file");
printf("Playback finished.\n");
}
void play(const char *pWaveFile) {
parse(pWaveFile, &mWaveHeader);
print(&mWaveHeader);
playback(&mWaveHeader);
}
gonza_wolves
- 粉丝: 0
- 资源: 14
最新资源
- 基于SpringBoot+vue的社区医院管理系统.zip
- 锂电池SOC估计基于二阶RC模型的扩展卡尔曼滤波估算SOC 验证工况:HPPC 和 1C放电
- c#轻量级高并发物联网服务器接收程序源码(仅仅是接收硬件数据程序,没有web端,不是java,协议自己写,如果问及这些问题统统不回复 ),对接几万个设备没问题,数据库采用ef6+sqlite,可改e
- CityGPT: Empowering Urban Spatial Cognition of Large Language Models
- 平安夜祝福代码html 代码实现示例.docx
- java(二叉树的基本操作和部分二叉树相关的题)
- Spring Boot 整合 RabbitMQ(在Spring项目中使用RabbitMQ)
- 三菱Fx3U三轴定位控制程序,其中两轴为脉冲输出同步运行360度转盘,3轴为工作台丝杆 1.本程序结构清晰,有公共程序,原点回归,手动点动运行,手动微动运行 报警程序,参数初始化程序等 2.自动
- armv7l的树莓派可以用的onnxruntime版本
- 纸袋检测15-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- Yealink VC Desktop1.28.0.72, 免费,局域网,IP电话, SIP, VOIP, 视频通话,可与手机互通,手机上也安装 yealink
- 全自动棒料加工自动设备sw17全套技术开发资料100%好用.zip
- 用Jenkins 跑gitte仓库中的postman脚本 请求
- Instruction Pre-Training: Language Models are Supervised Multitask Learners
- 图片转PDF_QQ浏览器_20241226.pdf
- STM8驱动的MPU6050陀螺仪源程序
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页