global buffer or passing by reference?
-
I guess you have a class named
CWavRead
. I guess you want to pass the buffer that this class fills to other functions. I guess all of this takes place somewhere inside yourCSingleDlg
class. Please correct me, if I'm wrong. In this case my header file would approximately look like this:// forward declaration
class CWavRead;// class definition
class CSingleDlg
{
public:...
private:
// my dialog 'knows' an object of the CWavRead class
CWavRead* m_wavReader;
};And my implementation file would approximately look like this:
#include "SingleDlg.h"
#include "WavRead.h"// constructor
CSingleDlg::CSingleDlg()
{
m_wavReader = new CWavRead;
}// destructor
CSingleDlg::~CSingleDlg()
{
delete m_wavReader;
}// read wav file
CSingleDlg::OnFileOpen()
{
m_wavReader->read(path);
}// modify wav data
CSingleDlg::OnEditModify()
{
// get data
long size = m_wavReader->getBufferSize();
BYTE* buffer = m_wavReader->getBuffer();// create local (temporary) object
CWavOperator operator;// pass the read data
operator.DoSomething(buffer, size);
}Is it useful ?
We can do no great things, only small things with great love. - Mother Theresa
Unfortunately I don't know the interface of your
CWavRead
class...
We can do no great things, only small things with great love. - Mother Theresa
-
Unfortunately I don't know the interface of your
CWavRead
class...
We can do no great things, only small things with great love. - Mother Theresa
Thanks. Can you give me an idea of the implementation code for the getBuffer function to make sure I'm doing it right. I wrote one but it kepts resetting. Here's my read .h #include #include #include #define ERROR_LENGTH 1024 class CReadDemoWave { public: BYTE m_pbyDemoBuffer; static int testinteger; void ReadDemoWave(); BYTE *ReadDemoIntoBuffer(); void PlayWav(); long QueueWaveData(HWAVEOUT HWaveOut, WAVEHDR * waveHeader, DWORD WaveDataPlayed, DWORD WaveBufSize, HMMIO HMmio); BYTE *GetDemoBuffer(void); //void WriteDemo(HMMIO recordHandle); //public: // =========== D == E == M == O ======================= // BD create demo buffer. WAVEHDR lpDemoData; DWORD dwDemoDataSize; int m_nDemoBufferSize; //BYTE* m_pbyDemoBuffer; HMMIO m_hmmio; //HMMIO hmmio; char m_szError[ERROR_LENGTH]; int m_nError; MMIOINFO m_mmioInfo; DWORD dwDataSize; DWORD dwCount; HPSTR m_hptr; int m_nBufferSize; //WAVEFORMATEX waveFormat; }; Bill Dennis Orlando, FL
-
Thanks. Can you give me an idea of the implementation code for the getBuffer function to make sure I'm doing it right. I wrote one but it kepts resetting. Here's my read .h #include #include #include #define ERROR_LENGTH 1024 class CReadDemoWave { public: BYTE m_pbyDemoBuffer; static int testinteger; void ReadDemoWave(); BYTE *ReadDemoIntoBuffer(); void PlayWav(); long QueueWaveData(HWAVEOUT HWaveOut, WAVEHDR * waveHeader, DWORD WaveDataPlayed, DWORD WaveBufSize, HMMIO HMmio); BYTE *GetDemoBuffer(void); //void WriteDemo(HMMIO recordHandle); //public: // =========== D == E == M == O ======================= // BD create demo buffer. WAVEHDR lpDemoData; DWORD dwDemoDataSize; int m_nDemoBufferSize; //BYTE* m_pbyDemoBuffer; HMMIO m_hmmio; //HMMIO hmmio; char m_szError[ERROR_LENGTH]; int m_nError; MMIOINFO m_mmioInfo; DWORD dwDataSize; DWORD dwCount; HPSTR m_hptr; int m_nBufferSize; //WAVEFORMATEX waveFormat; }; Bill Dennis Orlando, FL
If your read function fills
m_pbyDemoBuffer
, just writeBYTE* CReadDemoWave::getBuffer() const
{
return m_pbyDemoBuffer;
}Returning a reference (pointer) breaks the privacy concept of your class, but it's handy.
We can do no great things, only small things with great love. - Mother Theresa
-
If your read function fills
m_pbyDemoBuffer
, just writeBYTE* CReadDemoWave::getBuffer() const
{
return m_pbyDemoBuffer;
}Returning a reference (pointer) breaks the privacy concept of your class, but it's handy.
We can do no great things, only small things with great love. - Mother Theresa
I have it like this: BYTE *CReadDemoWave::GetDemoBuffer(void) { return &m_pbyDemoBuffer; } When I take out the refrence (&), I get a compiler error saying it can't convert from BYTE to BYTE*. It's been 15 years since I've worked with pointers so not sure I am doing this correctly. My pointer seems to be resetting itself from the time the buffer is created and the time I call GetDemoBuffer(). Bill Dennis Orlando, FL
-
I have it like this: BYTE *CReadDemoWave::GetDemoBuffer(void) { return &m_pbyDemoBuffer; } When I take out the refrence (&), I get a compiler error saying it can't convert from BYTE to BYTE*. It's been 15 years since I've worked with pointers so not sure I am doing this correctly. My pointer seems to be resetting itself from the time the buffer is created and the time I call GetDemoBuffer(). Bill Dennis Orlando, FL
BYTE m_pbyDemoBuffer
is no array. It's just one number. If you want to create an array, please usenew
anddelete
:int size = 1000;
// allocate memory
BYTE* array = new BYTE[size];// free memory
delete[] array;It's like
malloc
andfree
.
We can do no great things, only small things with great love. - Mother Theresa
-
BYTE m_pbyDemoBuffer
is no array. It's just one number. If you want to create an array, please usenew
anddelete
:int size = 1000;
// allocate memory
BYTE* array = new BYTE[size];// free memory
delete[] array;It's like
malloc
andfree
.
We can do no great things, only small things with great love. - Mother Theresa
In another post, I asked someone to show me how to allocate member for m_pbyDemoBuffer. I am rewading the wave data into a LPSTR variable, then creating and allocating memory for m_pbyDemoBuffer using GlobalAlloc and doing memcpy from lpDemoData.lpData to m_pbyDemoBuffer. I am then returning *m_pbyDemoBuffer in GetDemoBuffer(). So, not using arrays here. Bill Dennis Orlando, FL
-
In another post, I asked someone to show me how to allocate member for m_pbyDemoBuffer. I am rewading the wave data into a LPSTR variable, then creating and allocating memory for m_pbyDemoBuffer using GlobalAlloc and doing memcpy from lpDemoData.lpData to m_pbyDemoBuffer. I am then returning *m_pbyDemoBuffer in GetDemoBuffer(). So, not using arrays here. Bill Dennis Orlando, FL
I am reading the wave data into a LPSTR variable, [...] How do you allocate the memory at the position your LPSTR variable points to ?
We can do no great things, only small things with great love. - Mother Theresa
-
I am reading the wave data into a LPSTR variable, [...] How do you allocate the memory at the position your LPSTR variable points to ?
We can do no great things, only small things with great love. - Mother Theresa
I've pasted part of the code below. I have a lot of junk commented out in the actual .cpp file but if you want that, I can send it without violating my contract. Thanks. ''Allocate space for data in lpDemoData structure. // lpData is LPSTR. if (!( lpDemoData.lpData = (char *)VirtualAlloc(0, dwDemoDataSize<<1, MEM_COMMIT, PAGE_READWRITE))) { printf("ERROR: Can't allocate memory for WAVE buffer!\n"); } // Read Data Chunk into lpData if (readres = mmioRead(hmmio, (HPSTR )lpDemoData.lpData, dwDemoDataSize) != dwDemoDataSize) { AfxMessageBox("Error: Failed to read data chunk."); mmioClose(hmmio,0); return; } //Create byte-based buffer, allocate memory and copy // lpData to it. int lenlpData=strlen(lpDemoData.lpData); m_pbyDemoBuffer=(BYTE)GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, lenlpData ); memcpy((BYTE*)m_pbyDemoBuffer, lpDemoData.lpData, lenlpData); Bill Dennis Orlando, FL
-
I've pasted part of the code below. I have a lot of junk commented out in the actual .cpp file but if you want that, I can send it without violating my contract. Thanks. ''Allocate space for data in lpDemoData structure. // lpData is LPSTR. if (!( lpDemoData.lpData = (char *)VirtualAlloc(0, dwDemoDataSize<<1, MEM_COMMIT, PAGE_READWRITE))) { printf("ERROR: Can't allocate memory for WAVE buffer!\n"); } // Read Data Chunk into lpData if (readres = mmioRead(hmmio, (HPSTR )lpDemoData.lpData, dwDemoDataSize) != dwDemoDataSize) { AfxMessageBox("Error: Failed to read data chunk."); mmioClose(hmmio,0); return; } //Create byte-based buffer, allocate memory and copy // lpData to it. int lenlpData=strlen(lpDemoData.lpData); m_pbyDemoBuffer=(BYTE)GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, lenlpData ); memcpy((BYTE*)m_pbyDemoBuffer, lpDemoData.lpData, lenlpData); Bill Dennis Orlando, FL
Yeah, if your project isn't yet working, I'll have a look at it.
We can do no great things, only small things with great love. - Mother Theresa
-
Yeah, if your project isn't yet working, I'll have a look at it.
We can do no great things, only small things with great love. - Mother Theresa
.h: #include #include #include #define ERROR_LENGTH 1024 class CReadDemoWave { public: BYTE m_pbyDemoBuffer; static int testinteger; void ReadDemoWave(); BYTE *ReadDemoIntoBuffer(); void PlayWav(); long QueueWaveData(HWAVEOUT HWaveOut, WAVEHDR * waveHeader, DWORD WaveDataPlayed, DWORD WaveBufSize, HMMIO HMmio); BYTE *GetDemoBuffer(void); //void WriteDemo(HMMIO recordHandle); //public: // =========== D == E == M == O ======================= // BD create demo buffer. WAVEHDR lpDemoData; DWORD dwDemoDataSize; int m_nDemoBufferSize; //BYTE* m_pbyDemoBuffer; HMMIO m_hmmio; //HMMIO hmmio; char m_szError[ERROR_LENGTH]; int m_nError; MMIOINFO m_mmioInfo; DWORD dwDataSize; DWORD dwCount; HPSTR m_hptr; int m_nBufferSize; //WAVEFORMATEX waveFormat; }; .cpp: // ================DEMO======================== BYTE *ReadDemoWave::ReadDemoIntoBuffer() { // HPSTR demobuffer; // Open file if( (m_hmmio=mmioOpen("AIPL.WAV", NULL,MMIO_ALLOCBUF | MMIO_READ )) == NULL ) { AfxMessageBox("A critical file is missing from your system. Contact AIPL. Singulator will now stop."); m_nError = 1; exit(NULL); } // GET INFO ON FILE I/O BUFFER if (mmioGetInfo(m_hmmio, &m_mmioDemoInfo, 0)) { sprintf(m_szError, "Failed to get I/O buffer info/n"); AfxMessageBox("Failed to get I/O buffer info"); m_nError = 1; mmioClose(m_hmmio,0); return (NULL); } // check if needs advance if (m_mmioDemoInfo.pchNext == m_mmioDemoInfo.pchEndRead){ if (mmioAdvance(m_hmmio,&m_mmioDemoInfo, MMIO_READ)){ //ERROR HERE mmioClose(m_hmmio, 0); sprintf(m_szError, "Failed to Advance I/O buffer info/n"); AfxMessageBox("Failed to advance I/O buffer info"); m_nError = 1; return (NULL); } } HWND hwnd=NULL; LPSTR path = "AIPL.WAV"; PlayWav(); WriteDemo(&m_pbyDemoBuffer,dwDemoDataSize); mmioSetInfo(m_hmmio, &m_mmioDemoInfo, 0); mmioClose(m_hmmio, 0); return (&m_pbyDemoBuffer); } //================PLAYWAV======================= void CReadDemoWave::PlayWav() { HMMIO hmmio; MMCKINFO mmckinfoParent; MMCKINFO mmckinfoSubchunk; DWORD dwFmtSize; DWORD WaveDataPlayed; HWAVEOUT hwaveout=NULL; WAVEFORMATEX waveFormat; /* for reading a fmt chunk's data */ waveFormat.wFormatTag = WAVE_FORMAT_PCM; if((hmmio=mmioOpen("aipl.WAV",NULL,MMIO_READ | MMIO_ALLOCBUF)) == NULL) { AfxMessageBox("Error opening file")