医用设备心跳心率检测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

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp

Java中Switch Case多个条件处理方法举例

《Java中SwitchCase多个条件处理方法举例》Java中switch语句用于根据变量值执行不同代码块,适用于多个条件的处理,:本文主要介绍Java中SwitchCase多个条件处理的相... 目录前言基本语法处理多个条件示例1:合并相同代码的多个case示例2:通过字符串合并多个case进阶用法使用

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2