Search code examples
c++cvoicevoice-recordingwinmm

Audio manipulation and delete some part of the audio


I'm new in voice codding, now I am succeed to recording microphone in the files and save each 10 seconds in a file with SaveRecordtoFile function(doing this with no problem)

Now I want to delete for example 2 seconds from the recorded data so my output will be 8 seconds instead of 10, in the randomTime array 0 is the number of seconds witch I want to be delete...

In a for-loop I copy the data of waveHeader->lpData in a new buffer if (randomTime[i] == '1')

It seems this is a true algorithm and should works but the problem is the outputs, some of the outputs are good (about 70% or more) but some of them are corrupted

I think I have a mistake in the code but I debug this code for some days and I don't understand what is the problem?

And as my 70% or more of outputs are good I think It's not because of bytes or samples


Solution

  • Your code can break a sample apart, after that the stream is out of sync and you hear a loud noise.

    How it happens? Your sample size is 4 bytes. So you must never copy anything that is not a multiple of 4. 10 seconds of audio will take 10x48000×4=1920000 bytes. However Sleep(10000) will always be near 10 seconds but not exactly 10 seconds. So you can get 1920012 bytes. Then you do:

    dwSamplePerSec = waveHeader->dwBytesRecorded / 10; // 10 Secs
    

    that returns 192001 (which is not multiple of 4) and the steam gets out of sync. If you're lucky you receive 1920040 bytes for 10 second and that remains multiple of 4 after division on 10 and you're ok.