vector<vector<AUDIO_DATA_TYPE>> allMixingSounds
时间: 2023-09-24 20:04:12 浏览: 79
在C语言中,没有直接的内置容器类型像C++中的`vector`。但是,我们可以使用指针和动态分配的内存来模拟二维数组。以下是使用C语言实现`vector<vector<AUDIO_DATA_TYPE>>`的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef short AUDIO_DATA_TYPE;
typedef struct {
AUDIO_DATA_TYPE* data;
size_t size;
} Vector;
typedef struct {
Vector* data;
size_t size;
} VectorVector;
void initVector(Vector* vec) {
vec->data = NULL;
vec->size = 0;
}
void pushBackVector(Vector* vec, AUDIO_DATA_TYPE value) {
vec->data = realloc(vec->data, (vec->size + 1) * sizeof(AUDIO_DATA_TYPE));
vec->data[vec->size] = value;
vec->size++;
}
void initVectorVector(VectorVector* vecVec) {
vecVec->data = NULL;
vecVec->size = 0;
}
void pushBackVectorVector(VectorVector* vecVec, Vector vec) {
vecVec->data = realloc(vecVec->data, (vecVec->size + 1) * sizeof(Vector));
vecVec->data[vecVec->size] = vec;
vecVec->size++;
}
void freeVector(Vector* vec) {
free(vec->data);
}
void freeVectorVector(VectorVector* vecVec) {
for (size_t i = 0; i < vecVec->size; i++) {
freeVector(&(vecVec->data[i]));
}
free(vecVec->data);
}
void MixSoundsBySimplyAdd(VectorVector allMixingSounds,
DWORD RawDataCnt,
Vector* __pRawDataBuffer)
{
typedef int WIDEN_TEMP_TYPE;
const WIDEN_TEMP_TYPE AUDIO_DATA_TYPE_MAX = SHRT_MAX;
const WIDEN_TEMP_TYPE AUDIO_DATA_TYPE_MIN = SHRT_MIN;
WIDEN_TEMP_TYPE Sum = 0;
for (int i = 0; i < RawDataCnt; ++i)
{
Sum = 0;
for (int wavNum = 0; wavNum < allMixingSounds.size; ++wavNum)
{
Sum += allMixingSounds.data[wavNum].data[i];
}
if (Sum > AUDIO_DATA_TYPE_MAX)
Sum = AUDIO_DATA_TYPE_MAX;
else if (Sum < AUDIO_DATA_TYPE_MIN)
Sum = AUDIO_DATA_TYPE_MIN;
pushBackVector(__pRawDataBuffer, AUDIO_DATA_TYPE(Sum));
}
}
int main() {
VectorVector allMixingSounds;
initVectorVector(&allMixingSounds);
// 添加数据到allMixingSounds
Vector vec1;
initVector(&vec1);
pushBackVector(&vec1, 1);
pushBackVector(&vec1, 2);
pushBackVector(&vec1, 3);
pushBackVectorVector(&allMixingSounds, vec1);
Vector vec2;
initVector(&vec2);
pushBackVector(&vec2, 4);
pushBackVector(&vec2, 5);
pushBackVector(&vec2, 6);
pushBackVectorVector(&allMixingSounds, vec2);
// 创建输出缓冲区
Vector rawDataBuffer;
initVector(&rawDataBuffer);
// 调用MixSoundsBySimplyAdd函数
MixSoundsBySimplyAdd(allMixingSounds, 3, &rawDataBuffer);
// 输出结果
for (int i = 0; i < rawDataBuffer.size; i++) {
printf("%d ", rawDataBuffer.data[i]);
}
printf("\n");
// 释放内存
freeVectorVector(&allMixingSounds);
freeVector(&rawDataBuffer);
return 0;
}
```
以上代码演示了如何使用指针和动态分配的内存来模拟`vector<vector<AUDIO_DATA_TYPE>>`。你可以根据自己的需求进行修改和调整。请注意,在使用完后,需要对动态分配的内存进行释放,以避免内存泄漏。
阅读全文