What I would like to do is to capture the sounds you hear from the computer speakers - NOT from the microphone. I.E if you listen to song in youtube with the computer then to capture that song sound. I.E everything that is playing or should be playing on the speakers should be recorded. I found that best will be to use the waveIn* functions, such as

waveInOpen
waveInPrepareHeader
waveInAddBuffer // in loop for all buffers
waveInStart
waveInStop
waveInReset
waveInUnprepareHeader // in loop for all buffers
waveInClose

(this wave* is new to me so sorry for any dumb question)

My problems are:

I think that waveInOpen records what you speak on the mic and not the speakers so i am not sure how to tell it to capture the speakers/soundcard.
I am note sure i understand how to make the software to still be active while the recording is running I.E where to put the Wait().
I do not understand how to create sound file .pcm from the buffers. I.e should i append buf(1) + buf(2) + buf(3) + ... (the buf i sent to waveInAddBuffer) for creating the file?
And I do not understand how to connect everything - i.e., I do NOT want to record X minutes, then to stop and then save to file. I need to transmit this recording online in real time so i need immediate access to the data.

The final goal is to transmit the recording after it is encoded - to another device in real time - currently i am not asking about the encoder since i will deal with it latter.

So I need something like:

byte arr[1024*1024];
while (1)
{
GetCurrentSounds(arr);
Encode(arr);
Transmit(arr)
}

So GetCurrentSounds fill arr with the recording that took place (in another thread i guess) from the time GetCurrentSounds last called .

Yes, I do not really need to save to file - but the reason i asked how to save it to file is for debugging to see that I do everything ok. Hope you can provide explanation to the 3 question and even some code how to do what I need.