⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wavestream.cpp

📁 Enc 压 缩 wav文件为 A16格式的 文件
💻 CPP
字号:
#include "wavestream.h"
#include "wav.h"
#include "rpxx.h"
#include "raw.h"

wavestream::wavestream(char *fname)
{
    name   	= (char*) malloc(strlen(fname)+1);
    strcpy(name,fname);

    info.fs     = 8000;
    info.ch     = 1;
    info.bits   = 16;
    info.types  = 1;      // 0: MSB first  1: LSB first
    info.offset = 0;
    info.length = -1;
    info.read_write = -1; // 0: read,      1: write     -1: still unknow
    info.pf     = NULL;
    info.decode = 0;      
}

wavestream::wavestream( char *fname,
		        int ifs,
		        int ibits,
			int ich,
			int ioffset,
			int itypes)
{
    name   	= (char*) malloc(strlen(fname)+1);
    strcpy(name,fname);

    info.fs     = ifs;
    info.ch     = ich;
    info.bits   = ibits;
    info.types  = itypes;  // 0: MSB first  1: LSB first
    info.offset = ioffset;
    info.length = -1;
    info.read_write = -1; // 0: read,      1: write     -1: still unknow
    info.pf     = NULL;
}

wavestream::~wavestream()
{
    switch(info.read_write) {
    case 0:
        end_read(); break;
    case 1:
        end_write();
    default:
	break; 
    }
    if(info.pf) {
        fclose(info.pf);
        info.pf = NULL;
    };
    free(name);
}


uint    wavestream::isa()
{
    int id;
    int pos;
    
    pos = ftell(info.pf);

    ::fseek(info.pf,0,SEEK_SET);
    ::fread((void*)&id,4,1,info.pf);
    ::fseek(info.pf,pos,SEEK_SET);

    switch(id){
    case RIFF    : return RIFF;
    case SUNPLUS : return SUNPLUS;
    default      : return RAW;
    }
}

int	wavestream::start_read()
{
    info.pf = fopen(name,"rb");

    if( info.pf == NULL) return 1;

    switch(isa()){
    case RIFF:	  
		pwavefile = new cwave(&info);
		break;
    case SUNPLUS:
		pwavefile = new crpxx(&info);
		break;
    default : 
		pwavefile = new craw(&info);
    }

    if(pwavefile != NULL) {
	return pwavefile->start_read();
    }
    return 1;
}

int	wavestream::start_write(uint format)
{
    if(info.pf) fclose(info.pf);
    info.pf = fopen(name,"wb");

    if(info.pf == NULL) return 1;

    switch(format){
    case RIFF:	  
		pwavefile = new cwave(&info);
		break;
    case SUNPLUS:
		pwavefile = new cwave(&info);
		break;
    default : 
		pwavefile = new craw(&info);
    }

    if(pwavefile != NULL) {
	return pwavefile->start_write(format);
    }
    return 1;
}

void    wavestream::end_read()
{
    if(pwavefile != NULL) {
	pwavefile->end_read();
    }
}

void    wavestream::end_write()
{
    if(pwavefile != NULL) {
	pwavefile->end_write();
    }
}

int    wavestream::read(void* buffer,int samples)
{
    return pwavefile->read(buffer,samples);
}

int    wavestream::write(void* buffer,int samples)
{
    return pwavefile->write(buffer,samples);
}

char*   wavestream::GetDisplay()
{
    switch(isa()){
    case RIFF:	  
		return pwavefile->GetDisplay();
		break;
    default : 
		return NULL;
    }

};
void    wavestream::SetDisplay(char *text)
{
    if(isa() == RIFF) SetDisplay(text);
};

void    wavestream::InitRegion(int Number)
{
    pwavefile->InitRegion(Number);
};

void    wavestream::NewRegion(int index,int id,char *label,int from,int len)
{
    pwavefile->NewRegion(index,id,label,from,len);
};

int     cwavefile::read(void* buffer,int samples)
{
    int  n,count;
    int  tmp;
    word *pwbuf;
//    char *pcbuf;

    count = 0;
    
    if(samples > info->length) samples = info->length;

    if(samples == 0) return 0;

    if(info->pf != NULL) {
        if(info->bits > 8) {
            count = ::fread(buffer,2,samples,info->pf);
            if(info->types == 0) { // MSB first
                pwbuf = (word*) buffer;
		for(n=0;n<samples;n++) {
                    tmp = pwbuf[n];
                    pwbuf[n] = (pwbuf[n]>>8) | (tmp<<8);
                }
            }

            if(info->offset != 0) { // not 2's complement
                pwbuf = (word*) buffer;
		for(n=0;n<samples;n++) {
                    pwbuf[n] += info->offset;
                }
            }
        } else {
	    char *temp = (char*)malloc(samples);
            count = ::fread((void*)temp,1,samples,info->pf);
            pwbuf = (word*)buffer;
	    for(n=0;n<count;n++) {
                pwbuf[n] = (temp[n] + info->offset)<<8;
            }
	    free(temp);
        }
    }
 
    info->length -= count;

    return count;
}

int     cwavefile::write(void* buffer,int samples)
{
    int  n,count;
    int  tmp;
    word *pwbuf;
//    char *pcbuf;

    count = 0;
    if(info->pf != NULL) {
        if(info->bits > 8) {
            if(info->types == 0) { // MSB first
                pwbuf = (word*) buffer;
		for(n=0;n<samples;n++) {
                    tmp = pwbuf[n];
                    pwbuf[n] = (pwbuf[n]>>8) | (tmp<<8);
                }
            }

            if(info->offset != 0) { // not 2's complement
                pwbuf = (word*) buffer;
		for(n=0;n<samples;n++) {
                    pwbuf[n] += info->offset;
                }
            }
            count += ::fwrite(buffer,2,samples,info->pf);
        } else {
	    char *temp=(char*)malloc(samples);
            pwbuf = (word*) buffer;
	    for(n=0;n<samples;n++) {
                temp[n] = ((pwbuf[n])>>8)-info->offset;
            }
            count += ::fwrite(temp,1,samples,info->pf);
	    free(temp);
        }
    }
    info->length += count;
    return count;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -