📄 ac_5p1_wav_file.cpp
字号:
/*//////////////////////////////////////////////////////////////////////////////
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright(c) 2005-2006 Intel Corporation. All Rights Reserved.
//
*/
#include <stdio.h>
#include <memory.h>
#include "ac_5p1_wav_file.h"
WavFile::WavFile()
{
m_is_first_time = 1;
m_is_info_valid = 0;
m_file_handle = NULL;
}
WavFile::~WavFile()
{
}
Ipp32s WavFile::Open(vm_char *p_filename, Ipp32u mode)
{
Ipp32s res;
FILE *p_file = NULL;
if (mode & AFM_CREATE) {
p_file = fopen( VM_STRING(p_filename), "wb");
} else {
p_file = fopen( VM_STRING(p_filename), "rb");
}
if (p_file == NULL) {
return -1;
}
m_file_handle = p_file;
if (!(mode & AFM_CREATE)) {
res = ReadHeaderInfo();
if (res < 0) {
if (mode & AFM_NO_CONTENT_WRN) {
fseek((FILE *) m_file_handle, 0, SEEK_SET);
return 1;
}
fclose((FILE *) m_file_handle);
return -1;
}
}
return 0;
}
const Ipp32u ctRiff = 0x46464952;
const Ipp32u ctWave = 0x45564157;
const Ipp32u ctFmt = 0x20746D66;
const Ipp32u ctData = 0x61746164;
struct t_chunk {
union {
Ipp32u m_ulId;
Ipp8s m_sId[4];
};
Ipp32u m_ulSize;
};
const Ipp32u ctFmtSize = (2 + 2 + 4 + 4 + 2 + 2);
typedef struct {
Ipp32s id_riff;
Ipp32s len_riff;
Ipp32s id_chuck;
Ipp32s fmt;
Ipp32s len_chuck;
Ipp16s type;
Ipp16s channels;
Ipp32s freq;
Ipp32s bytes;
Ipp16s align;
Ipp16s bits;
Ipp32s id_data;
Ipp32s len_data;
} sWaveHeader;
struct wav_header {
t_chunk riff_chunk;
Ipp32u wave_signature;
t_chunk fmt_chunk;
Ipp16u nFormatTag;
Ipp16u nChannels;
Ipp32u nSamplesPerSec;
Ipp32u nAvgBytesPerSec;
Ipp16u nBlockAlign;
Ipp16u wBitPerSample;
t_chunk data_chunk;
};
struct wav_header_ex {
t_chunk riff_chunk;
Ipp32u wave_signature;
t_chunk fmt_chunk;
Ipp16u nFormatTag;
Ipp16u nChannels;
Ipp32u nSamplesPerSec;
Ipp32u nAvgBytesPerSec;
Ipp16u nBlockAlign;
Ipp16u wBitPerSample;
Ipp16u cbSize;
union {
Ipp16u wValidBitsPerSample; /* bits of precision */
Ipp16u wSamplesPerBlock; /* valid if wBitsPerSample==0 */
Ipp16u wReserved; /* If neither applies, set to zero. */
};
Ipp32u dwChannelMask;
Ipp8u guid[16];
t_chunk data_chunk;
};
Ipp32s WavFile::Read(void *p_data, size_t size)
{
Ipp32s n;
if (m_file_handle == NULL) {
return -1;
}
n = fread(p_data, 1, size, (FILE *) m_file_handle);
return n;
}
Ipp32s WavFile::Write(void *p_data, size_t size)
{
size_t n;
size_t header_size;
if (m_is_first_time) {
m_is_first_time = 0;
header_size = sizeof(sWaveHeader);
fseek((FILE *) m_file_handle, header_size, SEEK_SET);
m_riff_size = header_size - 4;
m_data_size = 0;
}
if (m_file_handle == NULL) {
return -1;
}
n = fwrite(p_data, 1, size, (FILE *) m_file_handle);
m_riff_size += size;
m_data_size += size;
return n;
}
Ipp32s WavFile::Close()
{
if (m_file_handle == NULL) {
return -1;
}
fclose((FILE *) m_file_handle);
return 0;
}
Ipp32s WavFile::SetInfo(Info * p_info)
{
m_is_info_valid = 1;
memcpy(&m_info, p_info, sizeof(m_info));
return 0;
}
Ipp32s WavFile::GetInfo(Info * p_info)
{
if (m_is_info_valid) {
memcpy(p_info, &m_info, sizeof(m_info));
return 0;
}
return -1;
}
Ipp32s WavFile::ReadHeaderInfo()
{
wav_header_ex header;
t_chunk xChunk;
Ipp32u ulTemp = 0;
Ipp32u ulOffset = 0;
Ipp32u ulDataChunkOffset = 0;
Ipp32s iFmtOk = 0;
Ipp32s iDataOk = 0;
if (fread(&xChunk, sizeof(xChunk), 1, (FILE *) m_file_handle) != 1) {
// / IO error
return -1;
}
if (xChunk.m_ulId != ctRiff) {
// / File does not contain 'Riff' chunk !
return -2;
}
if (fread(&ulTemp, sizeof(ulTemp), 1, (FILE *) m_file_handle) != 1) {
// / IO error
return -1;
}
if (ulTemp != ctWave) {
// / File does not contain 'Wave' signature !
return -3;
}
while (1) {
if (iFmtOk && iDataOk)
break;
ulOffset = ftell((FILE *) m_file_handle);
if (fread(&xChunk, sizeof(xChunk), 1, (FILE *) m_file_handle) != 1) {
// / IO error
return -1;
}
switch (xChunk.m_ulId) {
case ctFmt:
if (fread(&header.nFormatTag, ctFmtSize, 1, (FILE *) m_file_handle) != 1) {
// / IO error
return -1;
}
if (xChunk.m_ulSize > ctFmtSize) {
fseek((FILE *) m_file_handle, xChunk.m_ulSize - ctFmtSize, SEEK_CUR);
}
iFmtOk = 1;
break;
case ctData:
ulDataChunkOffset = ulOffset;
fseek((FILE *) m_file_handle, xChunk.m_ulSize, SEEK_CUR);
iDataOk = 1;
break;
default:
fseek((FILE *) m_file_handle, xChunk.m_ulSize, SEEK_CUR);
break;
}
}
fseek((FILE *) m_file_handle, ulDataChunkOffset + 8, SEEK_SET);
m_info.format_tag = header.nFormatTag;
m_info.sample_rate = header.nSamplesPerSec;
m_info.resolution = header.wBitPerSample;
m_info.channels_number = header.nChannels;
m_info.channel_mask = 0;
m_is_info_valid = 1;
// m_info
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -