943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2545
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 28th, 2009
0

Wav file manipulation

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iaaan is offline Offline
23 posts
since Jan 2007
Jan 28th, 2009
2

Re: Wav file manipulation

I'm pretty sure you need to take the individual sample values and scale them up (or down).
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jan 28th, 2009
0

Re: Wav file manipulation

Definitely consider using a compression technique. Otherwise, scaling up can cause distortion. Its gross.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 29th, 2009
0

Re: Wav file manipulation

Thank you for your responses,

How would i compress the files after scaling as I did find scaling them up to cause distortion.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iaaan is offline Offline
23 posts
since Jan 2007
Jan 29th, 2009
1

Re: Wav file manipulation

Compress the sound beforehand, then apply the gain. Try here for some info on dynamic range compression.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Feb 2nd, 2009
0

Re: Wav file manipulation

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iaaan is offline Offline
23 posts
since Jan 2007
Feb 2nd, 2009
0

Re: Wav file manipulation

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?
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Feb 2nd, 2009
0

Re: Wav file manipulation

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.
MHC
Reputation Points: 24
Solved Threads: 0
Newbie Poster
MHC is offline Offline
5 posts
since Feb 2009
Feb 2nd, 2009
1

Re: Wav file manipulation

Click to Expand / Collapse  Quote originally posted by iaaan ...
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.

C++ Syntax (Toggle Plain Text)
  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



C++ Syntax (Toggle Plain Text)
  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.
MHC
Reputation Points: 24
Solved Threads: 0
Newbie Poster
MHC is offline Offline
5 posts
since Feb 2009
Feb 12th, 2009
0

Re: Wav file manipulation

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iaaan is offline Offline
23 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: header file and variable declaring problem
Next Thread in C++ Forum Timeline: Errors in code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC