医用设备心跳心率检测mp3文件输出(mp3文件处理)(二)

2024-04-22 18:48

本文主要是介绍医用设备心跳心率检测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文件处理)(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/926580

相关文章

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Golang 日志处理和正则处理的操作方法

《Golang日志处理和正则处理的操作方法》:本文主要介绍Golang日志处理和正则处理的操作方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录1、logx日志处理1.1、logx简介1.2、日志初始化与配置1.3、常用方法1.4、配合defer

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

Python处理大量Excel文件的十个技巧分享

《Python处理大量Excel文件的十个技巧分享》每天被大量Excel文件折磨的你看过来!这是一份Python程序员整理的实用技巧,不说废话,直接上干货,文章通过代码示例讲解的非常详细,需要的朋友可... 目录一、批量读取多个Excel文件二、选择性读取工作表和列三、自动调整格式和样式四、智能数据清洗五、

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

Python Pandas高效处理Excel数据完整指南

《PythonPandas高效处理Excel数据完整指南》在数据驱动的时代,Excel仍是大量企业存储核心数据的工具,Python的Pandas库凭借其向量化计算、内存优化和丰富的数据处理接口,成为... 目录一、环境搭建与数据读取1.1 基础环境配置1.2 数据高效载入技巧二、数据清洗核心战术2.1 缺失