Compression/Decompression Wave File and MP3

This is a discussion on Compression/Decompression Wave File and MP3 within the Projects and Job Recruitment forums, part of the Community Boards category; Ok, replace the entire savebuffer.cpp file contents with this (you have backed everything up haven't you?). The changes I've made ...

  1. #31
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Ok, replace the entire savebuffer.cpp file contents with this (you have backed everything up haven't you?). The changes I've made are highlighted in red.
    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;
    }

  2. #32
    Registered User
    Join Date
    Apr 2006
    Posts
    28
    Hi, Anonytmouse, thank you very much for your reply, appreciate.

    Yes, I have back up.

    I have tried to execute the software and after I click button 'Save Wave", the software prompts me to save .spx file but I could not find any spx file in the location I choose to save. I tried indifferent locations or directory, the result is still the same.

    I have put the speexenc.exe and speexdec.exe on the same folder with project debug folder.

    I wonder where I did wrong, please forgive me, I will try again

  3. #33
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    OK, time for a little debugging. Uncomment this line (that is, remove the two "//" at the start of the line):
    Code:
    	// MessageBox(NULL, szCommandLine, NULL, 0);
    This will make a popup appear telling you the exact command line used. You can then make sure speexenc is in the correct position and the input file exists. If you like, you can copy the command line (by pressing CTRL+C after clicking on the message box) and paste it here.

  4. #34
    Registered User
    Join Date
    Apr 2006
    Posts
    28
    Hi, Anonytmouse, thank you for your post.

    I have uncommented the above line but I still couldnt get any popup about the command line

  5. #35
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Try adding a message box here:
    Code:
    				MessageBox(NULL, savefile.GetPathName(), szWavePath, 0);
    
    				/* Compress with Speex to the final save location selected by the user. */
    				SpeexEncode(szWavePath, savefile.GetPathName());
    IGNORE THIS POST. SEE NEXT POST.
    Last edited by anonytmouse; 04-27-2006 at 10:39 PM.

  6. #36
    Registered User
    Join Date
    Apr 2006
    Posts
    28
    Hi, Anonytmouse, thanks for your post.

    After I added the above lines, I get 4 compile errors.

    error C2065: 'savefile' : undeclared identifier
    error C2228: left of '.GetPathName' must have class/struct/union type
    error C2065: 'szWavePath' : undeclared identifier
    error C2228: left of '.GetPathName' must have class/struct/union type
    Error executing cl.exe.

    Speech.exe - 4 error(s), 0 warning(s)

  7. #37
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I've spotted the problem.

    Replace the red line:
    Code:
    					wave.Save(szWavePath);
    					break;
    with:
    Code:
    					flag = FALSE;

  8. #38
    Registered User
    Join Date
    Apr 2006
    Posts
    28
    Hi, Anonytmouse, I have add flag =False and the software is error free.
    After I executed, I still cant see any spx file in the location I saved

  9. #39
    Registered User
    Join Date
    Apr 2006
    Posts
    28
    Hi, Anonytmouse, thanks for your post.

    Please forgive me for being not clever enough.

    I wonder where I did wrong...

    What I have done is I added the speexenc.exe and sppexdec.exe at the folder of debug and I record, save wave, but no file is saved

  10. #40
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Well you should be getting the popup with the command line now as mentioned in my "OK, time for a little debugging." post.

  11. #41
    Registered User
    Join Date
    Apr 2006
    Posts
    28
    Hi, Anonytmouse, thank you very much for your post.

    What I have done is uncomment the MessageBox(...) and replaced the 'break' with 'flag=FALSE".

    And I have executed the software, I still cant get the popup.

  12. #42
    Registered User
    Join Date
    Apr 2006
    Posts
    28
    Hi, Anonytmouse, I have get the popup command line now.

    Sorry, Please forgive me, it is my mistake forget to delete the 'break' word.

    But, after the popup command, I get the error message box as attached
    Attached Images Attached Images  

  13. #43
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    IS it working now? Is the file appearing in A: drive?

    If not, you need to confirm that both the speexenc path in the popup is correct and that the input file path is correct (ie. The file "D:\documents and settings\administrator.spa\Local Settings\Temp\wave file.wav" exists).

  14. #44
    Registered User
    Join Date
    Apr 2006
    Posts
    28
    Hi, Anonytmouse, thank you very much for your reply, appreciate.

    I can see the spx file in the A floppy disk but I could only find one wave file in the location D:\documents and settings\administrator.spa\Local Settings\Temp\wave file.wav" instead of the 15 wave files that I recorded
    Last edited by cindy_16051988; 04-27-2006 at 11:16 PM.

  15. #45
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    It uses the same file name for recording each wave file (ie. The wave file is overwritten each recording). This alllows you to not have to worry about deleting several temporary wave files at some point. You should be getting all the .spx files. I assume you don't need the original wave files.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening an MP3 file for bit access
    By gaza2k1 in forum C Programming
    Replies: 13
    Last Post: 02-24-2008, 09:08 PM
  2. mp3 file renamer
    By Loic in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2007, 01:52 PM
  3. MP3 file searching question
    By panfilero in forum C Programming
    Replies: 4
    Last Post: 11-27-2005, 01:19 PM
  4. Edit mp3 File Names
    By willc0de4food in forum C Programming
    Replies: 20
    Last Post: 04-11-2005, 12:45 AM
  5. wave player or mp3 player using C
    By lliero in forum C Programming
    Replies: 5
    Last Post: 02-27-2002, 11:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21