Code:
#include "stdafx.h"
#include "savebuffer.h"
#include "wave.h"
/*
class CSaveBuffer : public CObject
{
public:
CSaveBuffer();
virtual ~CSaveBuffer();
void add(int techno,int testno,char *data);
void save();
void clear();
private:
bufflist buff;
};
*/
CSaveBuffer::CSaveBuffer()
{
}
LPTSTR FindLastOf(LPCTSTR haystack, TCHAR needle)
{
int i = lstrlen(haystack) - 1;
while (i > 0)
{
if (haystack[i] == needle)
return (LPTSTR) &haystack[i];
i--;
}
return (LPTSTR) &haystack[0];
}
BOOL SpeexEncode(LPCTSTR szInputFile, LPCTSTR szOutputFile)
{
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi = { 0 };
TCHAR szCommandLine[MAX_PATH * 4 + 100];
TCHAR szPath[MAX_PATH];
TCHAR* p;
/* Get the path of the currently running executable
* which is where we expect speexenc to be located. */
GetModuleFileName(NULL, szPath, MAX_PATH);
p = FindLastOf(szPath, TEXT('\\')); p[0] = TEXT('\0');
/* Create command line. */
wsprintf(szCommandLine, TEXT("\"%s\\speexenc\" \"%s\" \"%s\""),
szPath, szInputFile, szOutputFile);
/* Execute command line. */
CreateProcess(NULL, szCommandLine, NULL,
NULL, FALSE, 0, NULL, NULL, &si, &pi);
/* Uncomment this line, if you want to pause here while the command completes. */
// WaitForSingleObject(pi.hProcess, INFINITE);
/* Uncomment this line for debugging, it will show the exact command line used. */
// MessageBox(NULL, szCommandLine, NULL, 0);
/* Cleanup. */
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
void CSaveBuffer::save()
{
CWave wave;
wave.BuildFormat(1,16000,16);
wave.m_buffer.SetNumSamples(16000*6,2);
bufflist::iterator i;
//free the memory
for (i=buff.begin();i!=buff.end();i++)
{
char filename[100];
char temp[10];
itoa(i->techno,temp,10);
strcpy(filename,temp);
strncat(filename,"-",1);
itoa(i->testno,temp,10);
strncat(filename,temp,strlen(temp));
strncat(filename,"-",1);
itoa(i->count,temp,10);
strncat(filename,temp,strlen(temp));
strncat(filename,".spx",4);
BOOL flag=TRUE;
wave.SetBuffer(i->buf,16000*6,TRUE);
while (flag)
{
CFileDialog savefile(FALSE,NULL,filename,OFN_OVERWRITEPROMPT,"Speech File (*.spx)|*.spx|");
if (savefile.DoModal()==IDOK)
{
TCHAR szWavePath[MAX_PATH];
/* Create a temporary path for the wave file. */
GetTempPath(MAX_PATH, szWavePath);
lstrcat(szWavePath, TEXT("wave file.wav"));
/* Save the wave file to temporary location. */
try
{
wave.Save(szWavePath);
break;
}
catch (...)
{
}
/* Compress with Speex to the final save location selected by the user. */
SpeexEncode(szWavePath, savefile.GetPathName());
}
else
{
break; //don't want to save this
}
}
//CFileDialog savefile(FALSE,
}
}
void CSaveBuffer::clear()
{
bufflist::iterator i;
//free the memory
for (i=buff.begin();i!=buff.end();i++)
{
free(i->buf);
}
//free the list
buff.clear();
}
CSaveBuffer::~CSaveBuffer()
{
bufflist::iterator i;
//free the memory
for (i=buff.begin();i!=buff.end();i++)
{
free(i->buf);
}
//free the list
buff.clear();
}
int CSaveBuffer::add(int techno,int testno,char *data)
{
bufflist::iterator i;
int count=1;
//get the count
for (i=buff.begin();i!=buff.end();i++)
{
if ((i->techno==techno) && (i->testno==testno))
{
count=i->count+1;
}
}
buffer_type temp;
temp.buf=malloc(16000*2*6);
if (temp.buf==NULL)
{
return -1;
}
memcpy(temp.buf,data,16000*2*6);
temp.count=count;
temp.techno=techno;
temp.testno=testno;
buff.insert(buff.end(),temp);
return 0;
}