前言
这学期学完多媒体技术及应用课程后,想把做实验时遇到的坑与技巧和大家分享(学弟学妹)。毕竟,可能 过几天我就全忘了,做的实验代码也可能会删了。
目录
- 实验1《数字音频处理程序设计》
- 实验2《数字图像处理程序设计》
- 实验3《数字视频处理程序设计》
正文
实验一 音频处理
实验内容:
1.打开两个音频文件(限Wav文件,并具有相同的采样频率和量化深度),然后用第 二个音频文件左声道代替第一个音频文件左声道,但保留第一个音频文件的右声道,最 后播放第一个音频文件,并观察结果。
2.设计实现静音效果。
关键步骤
1. 将文件放入与主cpp文件同目录下:
添加代码:
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
WCHAR *wszSourceFile = NULL;
const WCHAR *wszTargetFile = L"out1.wav";
const WCHAR *wszTargetFile2 = L"out2.wav";
2. 实现静音与声道替换的关键代码:
HRESULT WriteWaveData(
HANDLE hFile, // Output file1.
HANDLE hFile2, // Output file2.
IMFSourceReader *pReader, // Source reader.
IMFSourceReader *pReaderAnother, // Another Source reader.
DWORD cbMaxAudioData, // Maximum amount of audio data (bytes).
DWORD cbMaxAudioDataAnother, // Maximum amount of audio data (bytes).
DWORD *pcbDataWritten, // Receives the amount of data written.
DWORD *pcbDataWrittenAnother // Receives the amount of data written.
)
{
//......此处省略默认生成的代码
//Get a pointer to the audio data in the sample.
hr = pSample->ConvertToContiguousBuffer(&pBuffer);
if (FAILED(hr)) { break; }
hr2 = pSampleAnother->ConvertToContiguousBuffer(&pBufferAnother);
if (FAILED(hr2)) { break; }
//锁定内存,得到缓冲地址指针
hr = pBuffer->Lock(&pAudioData, NULL, &cbBuffer);
if (FAILED(hr)) { break; }
hr2 = pBufferAnother->Lock(&pAudioDataAnother, NULL, &cbBufferAnother);
if (FAILED(hr2)) { break; }
// Make sure not to exceed the specified maximum size.
if (cbMaxAudioData - cbAudioData < cbBuffer)
{
cbBuffer = cbMaxAudioData - cbAudioData;
}
if (cbMaxAudioDataAnother - cbAudioDataAnother < cbBufferAnother)
{
cbBufferAnother = cbMaxAudioDataAnother - cbAudioDataAnother;
}
//音频数据处理模块
for (int i = 0; i < cbBuffer; i++)
{
//静音
*(pAudioData + i) = 100;
//用第二支曲子代替左声道,右声道保持原曲不变
if((i-2)%4 == 0 ) *(pAudioData + i) = (*(pAudioDataAnother + i)); //屏蔽左声道的第三个字节
if((i-3)%4 == 0 ) *(pAudioData + i) = (*(pAudioDataAnother + i)); //屏蔽左声道的第四个字节
}
// Write data1 and data2 to the output file1 and output file2.
hr = WriteToFile(hFile, pAudioData, cbBuffer);
hr2 = WriteToFile(hFile2, pAudioDataAnother, cbBufferAnother);
if (FAILED(hr)) { break; }
if (FAILED(hr2)) { break; }
//......此处省略默认生成的代码
}
3. 运行程序,生成结果音频:
如果是静音的话,out1.wav是静音文件,out2.wav是导入的 第二个文件。
如果是声道替换的话,out1.wav文件