Now I'd like to open file2 in the same sort of manner and append the data to cData as efficiently as possible. I'd love to use a vector because it'd be so easy, but the buffer is going into an OpenAL function which accepts unsignedchar*.
This leads me to two questions:
1. Can I extract a TYPE* from a vector<TYPE> object?
2. Can I increase the size of the cData array without losing the data that was already saved and without copying the entire array which could contain a few million values?
unsignedchar* temp = cData; // Make a new pointer that points to cData, no copying of data is involved.
cData = newunsignedchar[data_size + data_size2]; // cData now points to a bigger array
memcpy(cData, temp, data_size); // Copy the old data to the new space
delete temp; // Delete the old data
file2.read((char*)&cData[data_size], data_size2); // read new data to the new space, starting at an index=datasize
The problem with putting it in a vector is I would somehow need to copy it to a char* for use in the OpenAL functions. I don't think you can just pass a vector into it. I'm not sure how to do that (unless vector<char>::iterator it = MyVector.begin() would actually conform to a char* type).
Here is my whole appending function. It's part of a class that does everything with wav files, so I've displayed member variables with this->.
int wavFile::Append(string fname)
{
std::ifstream fin(fname, std::ifstream::in | std::ifstream::binary);
// Load header
wavFile WavAPP;
this->LoadHdr(fin, WavAPP);
// Compare header
if (WavAPP.nChannels != this->nChannels)
{
cout << "Error: There are a different number of channels in these two files." << endl;
return 1;
}
if (WavAPP.nSamplesPerSec != this->nSamplesPerSec)
{
cout << "Error: The sample rate is different in these two files." << endl;
return 1;
}
if (WavAPP.nAvgBytesPerSec != this->nAvgBytesPerSec)
{
cout << "Error: Byterate doesn't match in these two files." << endl;
return 1;
}
// Load and append data
unsignedchar* temp = this->cData;
this->cData = newunsignedchar[this->data_size + WavAPP.data_size];
memcpy(this->cData, temp, this->data_size);
delete temp;
fin.read((char*)&this->cData[data_size], WavAPP.data_size);
// Adjust header
this->data_size += WavAPP.data_size;
this->size += WavAPP.data_size;
fin.close();
return 0;
}