请问各位大虾:有没有办法实现WAVE数据的压缩呀!急!最近写了一个程序,使用Microsoft自带的TRueSpeech压缩算法对8KH,单声道,8位的数据进行转换,结果始终压缩比都是100%,听介绍可以实现10:1的压缩嘛,怎么不行,有没有谁可以帮我呀?

解决方案 »

  1.   

    你也做局域网的语音通讯吗?用ACM是可以压缩,见下面的代码。但是噪音很大,不知如何消噪??
    include <windows.h>
    #include <mmsystem.h>
    #include <mmreg.h> // Multimedia registration
    #include <msacm.h> // Audio Compression Manager
    #include <stdio.h>
    #include <math.h>// Locate a driver that supports a given format and return its ID
    typedef struct {
    HACMDRIVERID hadid;
    WORD wFormatTag;
    } FIND_DRIVER_INFO;
    // callback function for format enumeration
    BOOL CALLBACK find_format_enum(HACMDRIVERID hadid, LPACMFORMATDETAILS pafd, DWORD dwInstance, DWORD fdwSupport)
    {FIND_DRIVER_INFO* pdi = (FIND_DRIVER_INFO*) dwInstance;
    if (pafd->dwFormatTag == (DWORD)pdi->wFormatTag) {
    // found it
    pdi->hadid = hadid;
    printf(" %4.4lXH, %s\n", pafd->dwFormatTag, pafd->szFormat);
    return FALSE; // stop enumerating
    }
    //printf(" FORMAT not MATCH.\n");
    return TRUE; // continue enumerating
    }// callback function for driver enumeration
    BOOL CALLBACK find_driver_enum(HACMDRIVERID hadid, DWORD dwInstance, DWORD fdwSupport)
    {FIND_DRIVER_INFO* pdi = (FIND_DRIVER_INFO*) dwInstance;// open the driver
    HACMDRIVER had = NULL;
    HRESULT mmr = acmDriverOpen(&had, hadid, 0);
    if (mmr) {// some error
    return FALSE; // stop enumerating}// enumerate the formats it supports
    DWORD dwSize = 0;
    mmr = acmMetrics(had, ACM_METRIC_MAX_SIZE_FORMAT, &dwSize);
    if (dwSize < sizeof(WAVEFORMATEX)) dwSize = sizeof(WAVEFORMATEX); // for MS-PCM
    WAVEFORMATEX* pwf = (WAVEFORMATEX*) malloc(dwSize);
    memset(pwf, 0, dwSize);
    pwf->cbSize = LOWORD(dwSize) - sizeof(WAVEFORMATEX);
    pwf->wFormatTag = pdi->wFormatTag;
    ACMFORMATDETAILS fd;
    memset(&fd, 0, sizeof(fd));
    fd.cbStruct = sizeof(fd);
    fd.pwfx = pwf;
    fd.cbwfx = dwSize;
    fd.dwFormatTag = pdi->wFormatTag;
    mmr = acmFormatEnum(had, &fd, find_format_enum, (DWORD)(VOID*)pdi, 0); 
    free(pwf);
    acmDriverClose(had, 0);
    if (pdi->hadid || mmr) {
    // found it or some error
    return FALSE; // stop enumerating
    }return TRUE; // continue enumeration
    }// locate the first driver that supports a given format tag
    HACMDRIVERID find_driver(WORD wFormatTag)
    {
    FIND_DRIVER_INFO fdi;
    fdi.hadid = NULL;
    fdi.wFormatTag = wFormatTag;
    MMRESULT mmr = acmDriverEnum(find_driver_enum, (DWORD)(VOID*)&fdi, 0); 
    if (mmr) return NULL;
    return fdi.hadid;
    }// get a description of the first format supported for a given tag
    WAVEFORMATEX* get_driver_format(HACMDRIVERID hadid, WORD wFormatTag)
    {
    // open the driver
    HACMDRIVER had = NULL;
    MMRESULT mmr = acmDriverOpen(&had, hadid, 0);
    if (mmr) {
    return NULL;
    }// allocate a structure for the info
    DWORD dwSize = 0;
    mmr = acmMetrics(had, ACM_METRIC_MAX_SIZE_FORMAT, &dwSize);
    if (dwSize < sizeof(WAVEFORMATEX)) dwSize = sizeof(WAVEFORMATEX); // for MS-PCM
    WAVEFORMATEX* pwf = (WAVEFORMATEX*) malloc(dwSize);
    memset(pwf, 0, dwSize);
    pwf->cbSize = LOWORD(dwSize) - sizeof(WAVEFORMATEX);
    pwf->wFormatTag = wFormatTag;ACMFORMATDETAILS fd;
    memset(&fd, 0, sizeof(fd));
    fd.cbStruct = sizeof(fd);
    fd.pwfx = pwf;
    fd.cbwfx = dwSize;
    fd.dwFormatTag = wFormatTag;// set up a struct to control the enumeration
    FIND_DRIVER_INFO fdi;
    fdi.hadid = NULL;
    fdi.wFormatTag = wFormatTag;mmr = acmFormatEnum(had, &fd, find_format_enum, (DWORD)(VOID*)&fdi, 0); 
    acmDriverClose(had, 0);
    if ((fdi.hadid == NULL) || mmr) {
    free(pwf);
    return NULL;
    }return pwf;
    }
      

  2.   

    int main(int argc, char* argv[])
    {
    // First we create a wave that might have been just recorded.
    // The format is 11.025 kHz, 8 bit mono PCM which is a recording
    // format available on all machines.
    // our sample wave will be 1 second long and will be a sine wave 
    // of 1kHz which is exactly 1,000 cyclesWAVEFORMATEX wfSrc;
    memset(&wfSrc, 0, sizeof(wfSrc));
    wfSrc.cbSize = 0;
    wfSrc.wFormatTag = WAVE_FORMAT_PCM; // pcm
    wfSrc.nChannels = 1; // mono
    wfSrc.nSamplesPerSec = 8000; // 11.025 kHz
    wfSrc.wBitsPerSample = 16;// 8 bit
    wfSrc.nBlockAlign = wfSrc.nChannels * wfSrc.wBitsPerSample / 8;
    wfSrc.nAvgBytesPerSec = wfSrc.nSamplesPerSec * wfSrc.nBlockAlign;
    DWORD dwSrcSamples = wfSrc.nSamplesPerSec;
    DWORD dwSrcBytes = wfSrc.nAvgBytesPerSec;
    unsigned char pSrcData[22050]; // 1 second durationdouble f = 1000.0;
    double pi = 4.0 * atan(1.0);
    double w = 2.0 * pi * f;
    for (DWORD dw = 0; dw < dwSrcBytes; dw++) {
    double t = (double) dw / (double) wfSrc.nSamplesPerSec; 
    pSrcData[dw] = 128 + (unsigned char)(127.0 * sin(w * t));
    }// Select a format to convert to
    // WORD wFormatTag = WAVE_FORMAT_ADPCM;
    // WORD wFormatTag = WAVE_FORMAT_IMA_ADPCM;
    // WORD wFormatTag = WAVE_FORMAT_GSM610;
    // WORD wFormatTag = WAVE_FORMAT_ALAW;
    // WORD wFormatTag = WAVE_FORMAT_MULAW;
    // WORD wFormatTag = 0x32; // MSN
    // WORD wFormatTag = WAVE_FORMAT_DSPGROUP_TRUESPEECH;
    WORD wFormatTag = 0x42; // G.723.1// Now we locate a CODEC that supports the destination format tag
    HACMDRIVERID hadid = find_driver(wFormatTag);
    if (hadid == NULL) {
    printf("No driver found\n");
    exit(1);
    }
    printf("Driver found (hadid: %4.4lXH)\n", hadid);
    // get the details of the format
    // Note: this is just the first of one or more possible formats for the given tag
    //WAVEFORMATEX* pwfDrv = get_driver_format(hadid, wFormatTag);
    WAVEFORMATEX* pwfDrv;
    pwfDrv = (WAVEFORMATEX*)new char[28];
    pwfDrv->wFormatTag = 66;
    pwfDrv->nChannels = 1;
    pwfDrv->nSamplesPerSec = 8000;
    pwfDrv->nAvgBytesPerSec = 800;
    pwfDrv->nBlockAlign = 24;
    pwfDrv->wBitsPerSample = 0;
    pwfDrv->cbSize = 10;
    unsigned char extra[10] ={ 2, 0, 0xce, 0x9a, 0x32, 0xf7, 0xa2, 0xae, 0xde, 0xac };
    for(int i=0;i<pwfDrv->cbSize;i++)
    *((unsigned char *)pwfDrv+18+i) = extra[i];for(i=0;i<28;i++)
    printf("%d,",*((unsigned char *)pwfDrv+i));if (pwfDrv == NULL) {
    printf("Error getting format info\n");
    exit(1);
    }
    printf("Driver format: %u bits, %lu samples per second\n", pwfDrv->wBitsPerSample, pwfDrv->nSamplesPerSec);///////////////////////////////////////////////////////////////////////////////////
    // convert the intermediate PCM format to the final format// open the driver
    HACMDRIVER had = NULL;
    MMRESULT mmr;
    mmr = acmDriverOpen(&had, hadid, 0);
    if (mmr) {
    printf("Failed to open driver\n");
    exit(1);
    }// open the conversion stream
    // Note the use of the ACM_STREAMOPENF_NONREALTIME flag. Without this
    // some software compressors will report error 512 - not possible
    HACMSTREAM hstr1;
    HACMSTREAM hstr2;
    mmr = acmStreamOpen(&hstr1,
    had, // driver handle
    &wfSrc, // source format
    pwfDrv, // destination format
    NULL, // no filter
    NULL, // no callback
    0, // instance data (not used)
    0);//ACM_STREAMOPENF_NONREALTIME); // flags
    if (mmr) {
    printf("Failed to open a stream to do PCM to driver format conversion\n");
    exit(1);
    }
    mmr = acmStreamOpen(&hstr2,
    had, // driver handle
    pwfDrv, // source format
    &wfSrc,// destination format
    NULL, // no filter
    NULL, // no callback
    0, // instance data (not used)
    0);//ACM_STREAMOPENF_NONREALTIME); // flags
    if (mmr) {
    printf("Failed to open a stream to do PCM to driver format conversion\n");
    exit(1);
    }// allocate a buffer for the result of the conversion.
    // compute the output buffer size based on the average byte rate
    // and add a bit for randomness
    // the IMA_ADPCM driver fails the conversion without this extra space
    DWORD dwDstBytes = pwfDrv->nAvgBytesPerSec * dwSrcSamples / wfSrc.nSamplesPerSec;
    dwDstBytes = dwDstBytes ; // add a little room
    unsigned char pDstData[800];//#ifdef _DEBUG
    // fill the dest buffer with zeroes just so we can see if anything got 
    // converted in the debugger
    memset(pDstData, 0, dwDstBytes);
    //#endif// fill in the conversion info
    ACMSTREAMHEADER strhdr1;
    ACMSTREAMHEADER strhdr2;
    memset(&strhdr1, 0, sizeof(strhdr1));
    strhdr1.cbStruct = sizeof(strhdr1);
    strhdr1.pbSrc = pSrcData; // the source data to convert
    strhdr1.cbSrcLength = dwSrcBytes;
    strhdr1.pbDst = pDstData;
    strhdr1.cbDstLength = dwDstBytes;// prep the header
    mmr = acmStreamPrepareHeader(hstr1, &strhdr1, 0); // convert the data
    mmr = acmStreamConvert(hstr1, &strhdr1, 0);
    if (mmr) {
    printf("Failed to do PCM to driver format conversion\n");
    exit(1);
    }printf("Converted OK\n");printf("Source wave had %lu bytes\n", dwSrcBytes);
    printf("Converted wave has %lu bytes\n", strhdr1.cbDstLengthUsed);
    printf("Compression ratio is %f\n", (double) dwSrcBytes / (double) strhdr1.cbDstLengthUsed); 
    memset(&strhdr2, 0, sizeof(strhdr2));
    strhdr2.cbStruct = sizeof(strhdr2);
    strhdr2.pbSrc = pDstData; // the source data to convert
    strhdr2.cbSrcLength = dwDstBytes;
    strhdr2.pbDst = pSrcData;
    strhdr2.cbDstLength = dwSrcBytes;// prep the header
    mmr = acmStreamPrepareHeader(hstr2, &strhdr2, 0); // convert the data
    mmr = acmStreamConvert(hstr2, &strhdr2, 0);
    if (mmr) {
    printf("Failed to do driver to PCM format conversion\n");
    exit(1);
    }
    printf("Converted 2 OK\n");
    mmr = acmStreamUnprepareHeader(hstr1, &strhdr1, 0);
    // mmr = acmStreamUnprepareHeader(hstr2, &strhdr2, 0);
    // close the stream and driver
    mmr = acmStreamClose(hstr1, 0);
    mmr = acmStreamClose(hstr2, 0);
    mmr = acmDriverClose(had, 0);// show the conversion stats
    printf("Source wave had %lu bytes\n", dwDstBytes);
    printf("Converted wave has %lu bytes\n", strhdr2.cbDstLengthUsed);
    printf("Compression ratio is %f\n", (double) dwDstBytes / (double) strhdr2.cbDstLengthUsed); getchar();return 0;
    }
      

  3.   

    我觉得局域网中如果传输8KH,单声道,8位的声音数据不用压缩也可以,一秒钟总共才传输8k的数据。