医用设备心跳心率检测mp3文件输出(mp3文件处理)(二)
本系统是通过一套国外先进的医用设备将用户心跳心率存储到 mp3文件中, 该软件系统由于日本以非常高的价格出售, 公司决定由我破译硬件数据,开发一套替换日本软件产品。 该项目共花费2个月时间完成。 如有需要可电邮448520782@qq.com邮箱获取整套代码。
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <unistd.h>
#include <sys/stat.h>
//#include <sys/mman.h>
#include <fcntl.h>
#include <sys/types.h>
//#include <sys/ioctl.h>
//#include <sys/soundcard.h>
#include "SC.h"
#include <windows.h>
#include "mad.h"
#pragma comment(lib, "libmad.lib")
#define BUFSIZE 8192*32
/*
* This is a private message structure. A generic pointer to this structure
* is passed to each of the callback functions. Put here any data you need
* to access from within the callbacks.
*/
struct buffer {
FILE *fp; /*file pointer*/
unsigned int flen; /*file length*/
unsigned int fpos; /*current position*/
unsigned char fbuf[BUFSIZE]; /*buffer*/
unsigned int fbsize; /*indeed size of buffer*/
};
typedef struct buffer mp3_file;
int soundfd; /*soundcard file*/
unsigned int prerate = 0; /*the pre simple rate*/
int writedsp(int c)
{
return 0;//write(soundfd, (char *)&c, 1);
}
void set_dsp()
{
#if 0
int format = AFMT_S16_LE;
int channels = 2;
int rate = 44100;
soundfd = open("/dev/dsp", O_WRONLY);
ioctl(soundfd, SNDCTL_DSP_SPEED,&rate);
ioctl(soundfd, SNDCTL_DSP_SETFMT, &format);
ioctl(soundfd, SNDCTL_DSP_CHANNELS, &channels);
#else
// if((soundfd = open("test.bin" , O_WRONLY | O_CREAT)) < 0)
// {
// fprintf(stderr , "can't open sound device!\n");
// exit(-1);
// }
#endif
}
/*
* This is perhaps the simplest example use of the MAD high-level API.
* Standard input is mapped into memory via mmap(), then the high-level API
* is invoked with three callbacks: input, output, and error. The output
* callback converts MAD's high-resolution PCM samples to 16 bits, then
* writes them to standard output in little-endian, stereo-interleaved
* format.
*/
static int decode(mp3_file *mp3fp);
int main(int argc, char *argv[])
{
long flen, fsta, fend;
int dlen;
mp3_file *mp3fp;
//if (argc != 2)
// return 1;
mp3fp = (mp3_file *)malloc(sizeof(mp3_file));
if((mp3fp->fp = fopen("0135191.mp3", "r")) == NULL)
{
printf("can't open source file.\n");
return 2;
}
fsta = ftell(mp3fp->fp);
fseek(mp3fp->fp, 0, SEEK_END);
fend = ftell(mp3fp->fp);
flen = fend - fsta;
if(flen > 0)
fseek(mp3fp->fp, 0, SEEK_SET);
int ret= fread(mp3fp->fbuf, 1, BUFSIZE, mp3fp->fp);
mp3fp->fbsize = ret;
mp3fp->fpos = 0;
mp3fp->flen = flen;
set_dsp();
decode(mp3fp);
//close(soundfd);
fclose(mp3fp->fp);
Screen("aa.jpg");
return 0;
}
static enum mad_flow input(void *data, struct mad_stream *stream)
{
mp3_file *mp3fp;
int ret_code;
int unproc_data_size; /*the unprocessed data's size*/
int copy_size;
mp3fp = (mp3_file *)data;
if(mp3fp->fpos < mp3fp->flen) {
unproc_data_size = stream->bufend - stream->next_frame;
//printf("%d, %d, %d\n", unproc_data_size, mp3fp->fpos, mp3fp->fbsize);
memcpy(mp3fp->fbuf, mp3fp->fbuf + mp3fp->fbsize - unproc_data_size, unproc_data_size);
copy_size = BUFSIZE - unproc_data_size;
if(mp3fp->fpos + copy_size > mp3fp->flen) {
copy_size = mp3fp->flen - mp3fp->fpos;
}
fread(mp3fp->fbuf+unproc_data_size, 1, copy_size, mp3fp->fp);
mp3fp->fbsize = unproc_data_size + copy_size;
mp3fp->fpos += copy_size;
/*Hand off the buffer to the mp3 input stream*/
mad_stream_buffer(stream, mp3fp->fbuf, mp3fp->fbsize);
ret_code = MAD_FLOW_CONTINUE;
} else {
ret_code = MAD_FLOW_STOP;
}
return (enum mad_flow)(ret_code);
}
/*
* The following utility routine performs simple rounding, clipping, and
* scaling of MAD's high-resolution samples down to 16 bits. It does not
* perform any dithering or noise shaping, which would be recommended to
* obtain any exceptional audio quality. It is therefore not recommended to
* use this routine if high-quality output is desired.
*/
static inline signed int scale(mad_fixed_t sample)
{
/* round */
sample += (1L << (MAD_F_FRACBITS - 16));
/* clip */
if (sample >= MAD_F_ONE)
sample = MAD_F_ONE - 1;
else if (sample < -MAD_F_ONE)
sample = -MAD_F_ONE;
/* quantize */
return sample >> (MAD_F_FRACBITS + 1 - 16);
}
/*
* This is the output callback function. It is called after each frame of
* MPEG audio data has been completely decoded. The purpose of this callback
* is to output (or play) the decoded PCM audio.
*/
//输出函数做相应的修改,目的是解决播放音乐时声音卡的问题。
static enum mad_flow output(void *data, struct mad_header const *header,
struct mad_pcm *pcm)
{
unsigned int nchannels, nsamples;
mad_fixed_t const *left_ch, *right_ch;
// pcm->samplerate contains the sampling frequency
nchannels = pcm->channels;
nsamples = pcm->length;
left_ch = pcm->samples[0];
right_ch = pcm->samples[1];
static int title=0;
static int totalbyte=0;
//short buf[nsamples *2];
short* buf=(short*)malloc(nsamples*2*sizeof(short));
int i = 0;
printf(">>%d\n", nsamples);
while (nsamples--) {
signed int sample;
// output sample(s) in 16-bit signed little-endian PCM
sample = scale(*left_ch++);
buf[i++] = sample & 0xFFFF;
// if (nchannels == 2) {
// sample = scale(*right_ch++);
// buf[i++] = sample & 0xFFFF;
// }
}
//fprintf(stderr, ".");
//write(soundfd, &buf[0], i * 2);
char buff[16]={0};
FILE* flog=fopen("log.txt","a");
unsigned char* cbuff=(unsigned char*)buf;
for(int j=0; j<i;j+=32)
{
memset(buff, 0, 16);
short min=0;
short mintmp=0;
for(int jj=0; jj<64; jj+=8)
{
for(int ijj=0; ijj<16;ijj++)
{
min= buf[j+jj+ijj]<min? buf[j+jj+ijj] : min;
//max= buf[j+jj]>max? buf[j+jj] : max;
}
}
min = (min )/20;
if(min > -600)
{
min+=300;
sprintf(buff, "%d\n", min);//(unsigned char)cbuff[j], (unsigned char)cbuff[j+1]);
fwrite(buff, 1, strlen(buff), flog);
totalbyte++;
}
}
fclose(flog);
printf("\n title: %d total: %d nsamples: %d samplerate: %d sample: %d\n", title++, totalbyte, nsamples, pcm->samplerate, pcm->samples);
free(buf);
return MAD_FLOW_CONTINUE;
}
/*
* This is the error callback function. It is called whenever a decoding
* error occurs. The error is indicated by stream->error; the list of
* possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
* header file.
*/
static enum mad_flow error(void *data,
struct mad_stream *stream,
struct mad_frame *frame)
{
mp3_file *mp3fp = (mp3_file*)data;
fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
stream->error, mad_stream_errorstr(stream),
stream->this_frame - mp3fp->fbuf);
/* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */
return MAD_FLOW_CONTINUE;
}
/*
* This is the function called by main() above to perform all the decoding.
* It instantiates a decoder object and configures it with the input,
* output, and error callback functions above. A single call to
* mad_decoder_run() continues until a callback function returns
* MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
* signal an error).
*/
static int decode(mp3_file *mp3fp)
{
struct mad_decoder decoder;
int result;
/* configure input, output, and error functions */
mad_decoder_init(&decoder, mp3fp,
input, 0 /* header */, 0 /* filter */, output,
error, 0 /* message */);
/* start decoding */
result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
/* release the decoder */
mad_decoder_finish(&decoder);
return result;
}
医用设备心跳心率检测mp3文件输出(mp3文件处理)(二)相关推荐
- ffmpeg 解码音频(aac、mp3)输出pcm文件
ffmpeg 解码音频(aac.mp3)输出pcm文件 播放pcm可以参考: ffplay -ar 48000 -ac 2 -f f32le out.pcm main.c #include <s ...
- Altium Designer生产文件输出与文件归档
一.生产文件输出 Gerber文件是线路板行业软件描述线路板(线路层.阻焊层.字符层等)图像及钻.铣数据的文档格式集合,是线路板行业图像转换的标准格式.作为初识PCB的小白,往往会将PCB源文件发给板 ...
- 光绘文件输出GERBER文件
博主学习时的记录,便于日后忘记可以回顾 光绘文件输出GERBER文件 PCB板layout结束后,制作光绘文件 点击Manufacture→Artwork→film Control 按图示添加需要输出 ...
- python文件输出-python文件流
打开文件 文件的基本方法 迭代文件内容 打开文件 打开文件,可以使用自动导入的模块io中的函数open.函数open将文件名作为唯一必不可少的参数,并返回一个文件对象.如果只指定一个文件名,则获得一个 ...
- python文件输出-Python 文件和输入输出小结
1.打开和关闭文件(open(),file(),close()) 有两种内建函数可以获取文件对象:open和file.他们的用法完全一样.下面只以open()为例子讲解.获取一个文件对象(打开文件)的 ...
- python文件输出-python 文件的输入输出
一:文件的输入: 在前面python基础命令中,我们已经介绍过输入,在此举几个例子解释一下两者的区别 raw_input()函数从标准输入读取一个行,并返回一个字符串(去掉结尾的换行符): input ...
- python文件输出exe文件反汇编_python 反编译exe文件为py文件的实例代码
我们用pyinstaller把朋友文件打包成exe文件,但有时候我们需要还原,我们可以用pyinstxtractor.py 用法: python pyinstxtractor.py xxx.exe 之 ...
- python文件输出exe文件反汇编_【python】控制台python代码导出成exe文件
需要pip install pyinstaller pyinstaller这个库可以将python代码打包成exe,运行在没有安装python环境的Windows上 适合控制台程序进行打包,不适合界面 ...
- CodeSmith实用技巧(八):生成的代码输出到文件中
在CodeSmith中,要把生成的代码文件输出到文件中,你需要在自己的模版中继承OutputFileCodeTemplate类.<?xml:namespace prefix = o ns = & ...
最新文章
- Java 基本功之(二)Java 面向对象
- 北京林大计算机科技应为abc哪类,北京林业大学新生入学要准备什么?
- Linux实用命令总结
- 日志规范之slf4j整合Log4j以及Logback
- 数据结构与算法 | 插值查找
- php经典实例使用正则动态修改配置文件
- md5加密工具类_贼好用的 Java 工具类库! GitHub 星标 10k+,你在用吗?
- EPC Class0 Gen1标签识别代码C++
- 华为鸿蒙系统手机匹配,【图片】华为鸿蒙系统的厉害之处在于 你可能非用不可
!【手机吧】_百度贴吧...
- 从事7年前端开发,有些经验想对转行学习前端的伙伴说说!
- 2021年前端还好找工作吗?
- 区分PC端与移动端代码,涵盖C#、JS、JQuery、webconfig
- 微信登陆报错:redirect_uri域名与后台配置不一致,错误码:10003 微信支付报错 微信登录报错 微信开发
- C++对象在内存中的布局
- Atitit uke人才分类 杰出人才 高级人才、专业技术人才、技能人才 杰出人才移民共包含科技丶艺术丶工商丶运动及教育五大领域 杰出人才定义 编辑 联邦移民法规定「杰出能力」是指在科学丶艺
- 麻省理工python公开课 pdf_MIT麻省理工学院公开课:计算机科学及编程导论 Python 笔记1-3...
- 世界各国GDP排名(1960-2018)
- 阿里天池大赛实战记录之菜鸟-需求预测与分仓规划 1
- 【和UI斗智斗勇的日子】如何实现一个类似哈罗单车APP主页打车模块的卡片式切的View
- Android返回桌面代码,安卓 双击返回键 返回桌面