Wav file manipulation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 21
Reputation: iaaan is an unknown quantity at this point 
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Wav file manipulation

 
0
  #1
Jan 28th, 2009
Hey all.

Im working on a project that manipulates Wav files such as being able to add an echo to it and so on. So far ive managed to read in the Wav, split it up into the header file and data and manipulate the data by adding an echo to it and flange type effect. However, I am struggling with volume. Id like to know how I can increase and decrease the volume of a Wav file. Ive googled around and found nothing that can help me so any help on where to start would be appreciated.

Thanks, Ian
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Wav file manipulation

 
0
  #2
Jan 28th, 2009
I'm pretty sure you need to take the individual sample values and scale them up (or down).
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Wav file manipulation

 
0
  #3
Jan 28th, 2009
Definitely consider using a compression technique. Otherwise, scaling up can cause distortion. Its gross.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 21
Reputation: iaaan is an unknown quantity at this point 
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Re: Wav file manipulation

 
0
  #4
Jan 29th, 2009
Thank you for your responses,

How would i compress the files after scaling as I did find scaling them up to cause distortion.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Wav file manipulation

 
1
  #5
Jan 29th, 2009
Compress the sound beforehand, then apply the gain. Try here for some info on dynamic range compression.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 21
Reputation: iaaan is an unknown quantity at this point 
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Re: Wav file manipulation

 
0
  #6
Feb 2nd, 2009
Thanks for your replies, am looking into it as we speak.

One last question (for now!)

Im currently playing the loaded wav file using :

sndPlaySound(Filepath, SND_ASYNC);

Im aware that to add effects and change the volume on the fly that I need to play the wav file from the data loaded into the program. I have googled and have yet to find any way to do this. Is there anyone that can point me in the right direction?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Wav file manipulation

 
0
  #7
Feb 2nd, 2009
Wave files follow the IFF standard which makes it easier to read. This page describes the inner structure of wave files. I trust you can do low-level file operations?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: MHC is an unknown quantity at this point 
Solved Threads: 0
MHC MHC is offline Offline
Newbie Poster

Re: Wav file manipulation

 
0
  #8
Feb 2nd, 2009
Hi!
You can make this very simple. Just you should multiply each sample by some value. If you work with signed samples. For example, suppose you have an array of signed short (16 bit audio PCM). a[5] = {10000, 9500, 3200, -2700, -3100}; Multiply each sample by 1.5 for an increase of volume. {15000, 14250, 4800, -4050, -4650}.
Or multiply by 0.7 for a decrease it. if value will be more (less) of maximum(minimum) -- just set this sample to maximum(min) of signed short: 32767(-32768). But this method works with signed samples only. For unsigned PCM audio methods are some different, but quite similar.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: MHC is an unknown quantity at this point 
Solved Threads: 0
MHC MHC is offline Offline
Newbie Poster

Re: Wav file manipulation

 
1
  #9
Feb 2nd, 2009
Originally Posted by iaaan View Post
Im aware that to add effects and change the volume on the fly that I need to play the wav file from the data loaded into the program. I have googled and have yet to find any way to do this. Is there anyone that can point me in the right direction?
iaaan, use the waveOut* functions.
This way is little more complex, but much better.
first you should init sound device, wave-headers etc. and then only use waveOutWrite. Use MSDN
if needs -- look at this pieces of my code. it use two buffers, but i think, will be better for you to use one buffer. it will be so easier.

  1.  
  2. int CSoundPlayer::InitSndDevice()
  3. {
  4. memset(&m_Format, 0, sizeof(m_Format));
  5. m_Format.nSamplesPerSec = m_MAX_FREQUENCY;
  6. m_Format.nChannels = STEREO;
  7. m_Format.wBitsPerSample = 16;
  8. m_Format.nBlockAlign = (m_Format.nChannels * m_Format.wBitsPerSample + 7) / 8;
  9. m_Format.nAvgBytesPerSec = m_Format.nBlockAlign * m_Format.nSamplesPerSec;
  10. m_Format.wFormatTag = WAVE_FORMAT_PCM;
  11. UINT res = waveOutOpen(&m_hWaveOut, m_DeviceID, &m_Format, (DWORD_PTR) waveOutProc,(DWORD_PTR) this, CALLBACK_FUNCTION);
  12. if (MMSYSERR_NOERROR != res)
  13. {
  14. ShowError((int)res, "Error waveOutOpen");
  15. return -1;
  16. }
  17. memset(&m_MainSndBuffer, 0, sizeof(m_MainSndBuffer));
  18. memset(&m_ExtSndBuffer, 0, sizeof(m_ExtSndBuffer));
  19.  
  20. m_hMainWaveHdr.dwFlags = 0;
  21. m_hExtWaveHdr.dwFlags = 0;
  22. m_hMainWaveHdr.dwLoops = m_hExtWaveHdr.dwLoops = 0;
  23. m_hMainWaveHdr.lpData = m_MainSndBuffer;
  24. m_hExtWaveHdr.lpData = m_ExtSndBuffer;
  25. m_hMainWaveHdr.dwBufferLength = m_hExtWaveHdr.dwBufferLength = PLAYING_SOUND_BUFF_SIZE;
  26.  
  27. res = waveOutPrepareHeader(m_hWaveOut, &m_hMainWaveHdr, sizeof(m_hMainWaveHdr));
  28. if (MMSYSERR_NOERROR != res)
  29. {
  30. ShowError((int)res, "Error with waveOutPrepareHeader N1");
  31. return -1;
  32. };
  33. res = waveOutPrepareHeader(m_hWaveOut, &m_hExtWaveHdr, sizeof(m_hExtWaveHdr));
  34. if (MMSYSERR_NOERROR != res)
  35. {
  36. ShowError((int)res, "Error with waveOutPrepareHeader N2");
  37. return -1;
  38. };
  39. return 0;
  40. }
  41. //-----------------------------------------------------------------------------
  42.  
  43. void CSoundPlayer::StartPlay()
  44. {
  45. MMRESULT res = waveOutWrite(m_hWaveOut, &m_hMainWaveHdr, sizeof(m_hMainWaveHdr));
  46. if (MMSYSERR_NOERROR != res) { ShowError(res, "Error with waveOutWrite"); };
  47. m_LoopNumIsPlaying = 0;
  48. res = waveOutWrite(m_hWaveOut, &m_hExtWaveHdr, sizeof(m_hExtWaveHdr));
  49. if (MMSYSERR_NOERROR != res) { ShowError(res, "Error with waveOutWrite"); };
  50. return;
  51. }
  52. //-----------------------------------------------------------------------------
  53.  
  54. void CALLBACK waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance, DWORD dwParam1, DWORD dwParam2)
  55. {
  56. dwParam1; // compiler warning :[]
  57. dwParam2;
  58. WAVEHDR *main_header = &((CSoundPlayer*)dwInstance)->m_hMainWaveHdr;
  59. WAVEHDR *ext_header = &((CSoundPlayer*)dwInstance)->m_hExtWaveHdr;
  60. char *main_buff = ((CSoundPlayer*)dwInstance)->m_MainSndBuffer;
  61. char *ext_buff = ((CSoundPlayer*)dwInstance)->m_ExtSndBuffer;
  62. if ((WOM_DONE == uMsg) && ((CSoundPlayer*)dwInstance)->m_DoWriteBuffer)
  63. {
  64. if (!(((CSoundPlayer*)dwInstance)->m_LoopNumIsPlaying % 2)) // even
  65. {
  66. memset(main_buff, 0, PLAYING_SOUND_BUFF_SIZE);
  67. MMRESULT res = waveOutWrite(hwo, main_header, sizeof(*main_header));
  68. if (MMSYSERR_NOERROR != res) {
  69. ShowError(res, "Error waveOutWrite");
  70. };
  71. }
  72. else
  73. {
  74. memset(ext_buff, 0, PLAYING_SOUND_BUFF_SIZE);
  75. MMRESULT res = waveOutWrite(hwo, ext_header, sizeof(*ext_header));
  76. if (MMSYSERR_NOERROR != res) {
  77. ShowError(res, "Error waveOutWrite");
  78. };
  79. }
  80. ((CSoundPlayer*)dwInstance)->m_LoopNumIsPlaying++;
  81. ((CSoundPlayer*)dwInstance)->m_PacketsCounter++;
  82. if (((CSoundPlayer*)dwInstance)->m_PacketsCounter == 0)
  83. ((CSoundPlayer*)dwInstance)->GetNextPosition();
  84. }
  85. else
  86. {
  87. ((CSoundPlayer*)dwInstance)->m_LoopNumIsPlaying += 0;
  88. }
  89. };
  90. //-------------------------------------------------------------------------------------

upd.

and some member-variables declarations



  1. void CALLBACK waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance, DWORD dwParam1, DWORD dwParam2);
  2.  
  3. ..............
  4. HWAVEOUT m_hWaveOut;
  5. WAVEFORMATEX m_Format;
  6. HANDLE m_PlayedEvent;
  7. LPDWORD m_PlayingThreadId;
  8. MMTIME m_CurrTime;
  9.  
  10. DWORD m_LastPos;
  11. DWORD m_PositionShift;
  12. DWORD m_LoopNumIsPlaying;
  13. WAVEHDR m_hMainWaveHdr;
  14. WAVEHDR m_hExtWaveHdr;
  15. char m_MainSndBuffer[PLAYING_SOUND_BUFF_SIZE];
  16. char m_ExtSndBuffer[PLAYING_SOUND_BUFF_SIZE];
  17. DWORD m_MAX_FREQUENCY;
  18. bool m_DoWriteBuffer;
  19. DWORD m_DeviceID;


good luck!
Last edited by MHC; Feb 2nd, 2009 at 7:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 21
Reputation: iaaan is an unknown quantity at this point 
Solved Threads: 0
iaaan iaaan is offline Offline
Newbie Poster

Re: Wav file manipulation

 
0
  #10
Feb 12th, 2009
Thank you MHC for your help. Ive managed to get it to Play/Pause and currently working on volume using the waveOut functions.

Cheers mate
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC