0

嗨,我正在开发一个关于处理声音的应用程序。但是存在“终止调用抛出异常”的问题

我放了一个断点(所有异常)来看看出了什么问题,结果是“this->buffer = new char[this->data_size];”

这是我的代码

波形文件.mm

"#include "wave_file.h"

#define LITTLE 1;
//#define DEBUG
//const int i = 1;
//#define isBigEndian() ( (*(char*)&i) == 0 )

wave_file::wave_file(){
    opened = false;
    data_size = 0;
}

void wave_file::open(string file_name){
    this->f.open(file_name,ios::binary);
    opened=this->f.is_open();
    opened = true;

    if(!opened){
#ifdef DEBUG
        cout<<"file open fail"<<endl;
#endif
        return;
    }

#ifdef DEBUG
    cout<<"file open success"<<endl;
#endif

    this->f.seekg(0,ios::end);
    this->data_size=(int)this->f.tellg();
    this->f.seekg(0,ios::beg);

    this->buffer = new char[this->data_size];
    int i;
    for(i=0;i<this->data_size;i++)
        buffer[i]=this->f.get();
        f.close();
#ifdef DEBUG
        cout<<"file read size is ";
        if(i==this->data_size)
            cout<<"right i is "<<i<<endl;
        else
            cout<<"false file size tellg is"<<this->data_size<<" i is "<<i<<endl;
#endif
}


bool wave_file::is_open(){
    return this->opened;
}

bool wave_file::is_legal_wave_file()//¨Oß_¨∞WAVEÆʶ°
{
    ///////////////////////////¨∞∂}¿…Æ◊´h¶^∂« false
    if(!this->opened)
        return false;

    ///////////////////////////∂}©lßP¬_
    //////////≠∫•˝ßP¬_¨Oß_¨∞RIFFÆʶ°
    string temp;
    int i;
    char c;
    for(i=0;i<4&&i<this->data_size;i++)
    {
        c=this->buffer[i];
        temp=temp+c;
    }
#ifdef DEBUG
    cout<<temp<<endl;
    if(temp=="RIFF")
        cout<<"equal to RIFF type ok!!"<<endl;
    else
        cout<<"not equal to RIFF"<<endl;
#endif
    if(!(temp=="RIFF"))
        return false;
    ////////±µµ€ßP¬_¨Oß_¨∞WAVE fma
    temp="";
    for(i=0;i<4&&(i+8)<this->data_size;i++)
    {
        c=this->buffer[i+8];
        temp=temp+c;
    }
#ifdef DEBUG
    cout<<temp<<endl;
    if(temp=="WAVE")
        cout<<"equal to WAVE type ok!!"<<endl;
    else
        cout<<"not equal to WAVE"<<endl;
#endif
    if(temp=="WAVE")
        return true;
    else
        return false;
}

int wave_file::read_data(int offset,int bytes,endia e)//±qoffset∂}©l≈™n≠”bytes•Œ            ´¸©w™∫endia™∫§Ë¶°¶s§Jint§§
{
    int temp;
    int i;
    char *c;
    c=(char*)&temp;
    if(e==littlendia)
    {
        temp=0;
        for(i=0;i<bytes&&i<4;i++)
            c[i]=this->buffer[offset+i];
#ifdef DEBUG
        cout<<"LITTLE endia string "<<c<<" is int "<<temp<<endl;
#endif
    }
    else
    {
        temp=this->buffer[offset];
        for(i=1;i<bytes&&i<4;i++)
            temp=(temp<<8)+this->buffer[offset+i];
#ifdef DEBUG
        cout<<"BIG endia string "<<c<<" is int"<<temp<<endl;
#endif
    }
    return temp;
}

short wave_file::read_short_data(int offset,endia e)//±qoffset∂}©l≈™2bytes•Œ´¸©w™∫endia™∫§Ë¶°¶s§Jint§§
{
    short answer;
    char front,behind;
    char *temp;
    temp=(char*)&answer;
    behind=this->buffer[offset];
    front=this->buffer[offset+1];
    if(e==littlendia)
    {
        temp[0]=behind;
        temp[1]=front;
    }
    else
    {
        temp[1]=behind;
        temp[0]=front;
    }
    return answer;
}

int wave_file::find_chunk(int offset,string chunk_name)//±qoffset∂}©l߉≤≈¶X¶W¶r™∫chunk¶^∂«®‰offset¶p™G•¢±—¶^∂«-1
{
    int i=offset;
    int j;
    string temp;
    char c;
    bool is_find =false;

    while((!is_find)&&i<this->data_size)
    {
        temp="";
        for(j=0;j<4;j++)
        {
            c=this->buffer[i+j];
            temp=temp+c;
        }
#ifdef DEBUG
        cout<<temp<<endl;
#endif
        if(chunk_name==temp)
            is_find=true;
        else
        {
        i+=4;
        j=this->read_data(i,4,littlendia);
        i=i+j+4;
        }
    }
    if(is_find)
        return i;
    else
        return -1;

}

和 viewcontroller.mm 的一部分

#import "ViewController.h"
#import <iostream>
#import <fstream>
#import <vector>
using namespace std;
#include"wave_file.h"
#include"chunk_fmt.h"
#include"chunk_data.h"
#include"contrast.h"

#define FILE_NAME "test.wav"
#define PERIOD_BLOCK 350 ///SAMPLE BLOCK
#define SLIDE_QUANTITATIVE 125 ///SAMPLE BLOCK
#define RANGE_OF_COMPARE 4
#define PERSENT 15
#define RANGE 2

//#define DEBUG
//#define SLIDED
//#define OUTPUT_SAMPLE_TO_FILE
#define OUTPUT_SAMPLE_FILE_NAME "tt.txt"
#define OUTPUT_RESULT_TO_FILE
#define OUTPUT_RESULT_FILE_NAME "r.txt"

void normalize(result *r,int range);
bool fix(int data,int average,int persent);


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    wave_file f;
    int i,j;
    bool flag = false;

    f.open(FILE_NAME);

    if(flag==false)
    {
        if(!f.is_legal_wave_file())
        {
            cout<<"not a wave file"<<endl;
            cin>>i;
            flag=true;
        }

        chunk_fmt cf;
        cf.parser(f);

        chunk_data cd;
        cd.parser(f,cf);

        if(flag==false)
        {
            if(!cd.is_parser())
            {
                cout<<"data not read in...."<<endl;
                cin>>i;
                flag=true;
            }
        }
        contrast ct;
        ct.load_data(cd.copy(),cd.get_size());
        cout<<"load data to contrast"<<endl;
        result *rt;
#ifdef SLIDED
        cout<<"corrlation with slided"<<endl;
        rt=ct.multiply_with_assign_slide(PERIOD_BLOCK,SLIDE_QUANTITATIVE);
#else
        cout<<"corrlation without slided"<<endl;
        rt=ct.multiply(PERIOD_BLOCK);
#endif

        //  normalize(rt,RANGE);

        cout<<"now output result...."<<endl;
#ifdef OUTPUT_SAMPLE_TO_FILE
        vector<short*>*k;
        k=cd.copy();
        ofstream of;
        of.open(OUTPUT_SAMPLE_FILE_NAME);
        for(i=0;i<cd.get_size();i++)
        {
            for(j=0;j<cf.num_of_chan();j++)
                of<<(*k)[j][i]<<'\t';
            of<<endl;
        }
        of.close();
#endif

#ifdef OUTPUT_RESULT_TO_FILE
        ofstream ofr;
        ofr.open(OUTPUT_RESULT_FILE_NAME);
        for(i=0;i<rt->result_item_number;i++)
        {
            ofr<<i<<'\t';
            for(j=0;j<(int)(rt->period_result)->size();j++)
                ofr<<(*(rt->period_result))[j][i]<<'\t';
            ofr<<endl;
        }
        ofr.close();
#endif

        cin>>i;
        flag=true;

    }

}

chunck_data.cpp 处理数据

#include "chunk_data.h"

//#define DEBUG
#define LITTLE 1
#define IN_WAVE_CHUNK 12

chunk_data::chunk_data()
{
    this->is_success=false;
    this->num_of_bytes_per_sample=0;
    this->num_of_channel=0;
    this->num_of_sample_per_channel=0;
}

void chunk_data::parser(wave_file &f,chunk_fmt &cf)
{
    this->num_of_bytes_per_sample=cf.block_a()/cf.num_of_chan();
    this->num_of_channel=cf.num_of_chan();

    int data_size;
    int offset;
    offset=f.find_chunk(IN_WAVE_CHUNK,"data");
    if(offset<0)
    {
#ifdef DEBUG
        cout<<"can not find the data chunk"<<endl;
#endif
        return;
    }
    data_size=f.read_data(offset+4,4,littlendia);

    this->num_of_sample_per_channel=data_size/cf.block_a();

#ifdef DEBUG
    cout<<"data size is "<<data_size<<" bytes"<<endl;
    cout<<"num_of_sample_per_channel "<<this->num_of_sample_per_channel<<endl;
    cout<<"num of channel is "<<this->num_of_channel<<endl;
#endif

    int i,j;
    offset+=8;
    for(i=0;i<this->num_of_channel;i++)
        this->channel.push_back(new short[this->num_of_sample_per_channel]);
    for(i=0;i<this->num_of_sample_per_channel;i++)
        for(j=0;j<this->num_of_channel;j++)
            this->channel[j][i]=f.read_short_data(offset+(i*this->num_of_channel+j)*2,littlendia);

    this->is_success=cf.is_parser();
}

很抱歉这些奇怪的词,因为这个项目是我的合作伙伴给的

有人有想法吗?非常感谢!

4

0 回答 0