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