How To Use GSM Compression in Low-level Wave Recording.

1. How To Use GSM Compression in Low-level Wave Recording

SUMMARY
Realtime GSM recording using the low-level waveXXX API can be accomplished by filling in an

appropriated struct and then calling waveInOpen() with the WAVE_MAPPER option so that the GSM format

specified in the struct will be used.

MORE INFORMATION
Along with the usual set of low-level waveXXX calls used in recording, and illustrated in such samples

as DDREC, the code fragments shown below provide information on the steps needed to record audio

compressed in the GSM format.

Either make the following definitions in one of the project's header files or make sure the project

has a path to an existing header file, such as MMREG.H, with these definitions. The key points here

are to define WAVE_FORMAT_GSM610 as a hexadecimal 31, and to set up a GSM structure that consists of a

WAVEFORMATEX struct followed by a WORD:

#define WAVE_FORMAT_GSM610 (0x0031)
typedef struct gsm610waveformat_tag
{
    WAVEFORMATEX wfx;
    WORD wSamplesPerBlock;
} GSM610WAVEFORMAT;

typedef GSM610WAVEFORMAT FAR *LPGSM610WAVEFORMAT;


Declare a handle for memory allocation and a pointer to a GSM610WAVEFORMAT struct similar to the

following:

HANDLE hgsmwavefmt;
LPGSM610WAVEFORMAT pgsmwavefmt;


Allocate and lock memory for the GSM610WAVEFORMAT struct similar to the following:

hgsmwavefmt = GlobalAlloc(GMEM_MOVEABLE, (UINT)(sizeof(GSM610WAVEFORMAT)));
pgsmwavefmt = (LPGSM610WAVEFORMAT)GlobalLock(hgsmwavefmt);


Fill in the GSM610WAVEFORMAT struct's members using the values shown in assigning data to the struct's

members. The exception is that pgsmwavefmt->wfx.nSamplesPerSec can be a rate other than 8000, in which

case pgsmwavefmt->wfx.nAvgBytesPerSec is computed as (pgsmwavefmt- >wfx.nSamplesPerSec)/320*65. When

using a value for pgsmwavefmt->wfx.nSamplesPerSec other than 8000, be sure it is a multiple of the 320

block size, and falls within the limits supported by the sound card. For example, pgsmwavefmt-

>wfx.nSamplesPerSec = 9920 is valid because it is a multiple of 320 and does not fall below the

minimum sampling rate of 8000 found on many sound cards:


Call waveInOpen() with the WAVE_MAPPER option, and be sure the third parameter is just the address of

the GSM610WAVEFORMAT struct's WAVEFORMATEX struct:

waveInOpen(&hwavein, (UINT)WAVE_MAPPER,
        (LPWAVEFORMATEX)&(pgsmwavefmt->wfx), (DWORD)(UINT)hwnd,
        (DWORD)NULL, CALLBACK_WINDOW);



2. How to add wav file headers for GSM or PCM raw voice data.

 

    DWORD m_WaveHeaderSize =0;
    DWORD m_WaveFormatSize =0;
   
    DWORD dwWrited=0;
    WriteFile(hFile,"RIFF",4,&dwWrited,0);
  
        unsigned int Sec=dwDataLength+m_WaveHeaderSize + 12;
        WriteFile(hFile,&Sec,sizeof(Sec),&dwWrited,0);
    WriteFile(hFile,"WAVE",4,&dwWrited,0);
    WriteFile(hFile,"fmt ",4,&dwWrited,0);
        WriteFile(hFile,&m_WaveFormatSize,sizeof(m_WaveFormatSize),&dwWrited,0);
           
    if(FormatType==WAVE_FORMAT_GSM610)
    {
        WriteFile(hFile,&(pgsmwavefmt->wfx.wFormatTag),sizeof(pgsmwavefmt->wfx.wFormatTag),&dwWrited,0);
        WriteFile(hFile,&(pgsmwavefmt->wfx.nChannels),sizeof(pgsmwavefmt->wfx.nChannels),&dwWrited,0);
        WriteFile(hFile,&(pgsmwavefmt->wfx.nSamplesPerSec),sizeof(pgsmwavefmt->wfx.nSamplesPerSec),&dwWrited,0);
        WriteFile(hFile,&(pgsmwavefmt->wfx.nAvgBytesPerSec),sizeof(pgsmwavefmt->wfx.nAvgBytesPerSec),&dwWrited,0);
        WriteFile(hFile,&(pgsmwavefmt->wfx.nBlockAlign),sizeof(pgsmwavefmt->wfx.nBlockAlign),&dwWrited,0);
        WriteFile(hFile,&(pgsmwavefmt->wfx.wBitsPerSample),sizeof(pgsmwavefmt->wfx.wBitsPerSample),&dwWrited,0);
        WriteFile(hFile,&(pgsmwavefmt->wfx.cbSize),sizeof(pgsmwavefmt->wfx.cbSize),&dwWrited,0);
        WriteFile(hFile,&(pgsmwavefmt->wSamplesPerBlock),sizeof(pgsmwavefmt->wSamplesPerBlock),&dwWrited,0);   
    }
    else if(FormatType==WAVE_FORMAT_PCM)
    {
        WriteFile(hFile,&(wavefmt->wFormatTag),sizeof(wavefmt->wFormatTag),&dwWrited,0);
        WriteFile(hFile,&(wavefmt->nChannels),sizeof(wavefmt->nChannels),&dwWrited,0);
        WriteFile(hFile,&(wavefmt->nSamplesPerSec),sizeof(wavefmt->nSamplesPerSec),&dwWrited,0);
        WriteFile(hFile,&(wavefmt->nAvgBytesPerSec),sizeof(wavefmt->nAvgBytesPerSec),&dwWrited,0);
        WriteFile(hFile,&(wavefmt->nBlockAlign),sizeof(wavefmt->nBlockAlign),&dwWrited,0);
        WriteFile(hFile,&(wavefmt->wBitsPerSample),sizeof(wavefmt->wBitsPerSample),&dwWrited,0);
        WriteFile(hFile,&(wavefmt->cbSize),sizeof(wavefmt->cbSize),&dwWrited,0);
     }
    WriteFile(hFile, "fact", 4, &dwWrited, 0);
    DWORD dwtemp;
    dwtemp = 4;
    WriteFile(hFile, &dwtemp, 4, &dwWrited, 0);
    dwtemp = 8000;
    WriteFile(hFile, &dwtemp, 4, &dwWrited,0);
    WriteFile(hFile,"data",4,&dwWrited,0);
    //WriteFile(hFile,&dwDataLength,sizeof(dwDataLength),&dwWrited,0);
    //WriteFile(hFile,pSaveBuffer,dwDataLength,&dwWrited,0);

 

Best regards.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值