Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to play 1 audio sample with the wav header in default audio output device?

Solution with directshow source filter will long time, because i need to write own source filter and put data to it.

But if there is an example of the source filter in c++ i will take it.

That is why i would like to get direction with WinApi.

UPDATE:
I have found the example of directshow source filter in "Samples\multimedia\directshow\filters\async".

But it should be edited for my purpose.

Can anybody give the WinApi link on the way to play audio data (file) in IStream or audio data (file) in the memory?

UPDATE:
CWave class is found and modified.

Now i need the CWave class to play the file synchronously.
Does any one know, how could be this done?

UPDATE:
Also i need to calculate the time current chunk should be playing based on WAV header or based on AM_MEDIA_TYPE and WAVEFORMATEX (seeing only the FORMAT_WaveFormatEx format).
So, i need the formula.
Posted
Updated 24-Dec-15 19:55pm
v4
Comments
Сергей Козлов 24-Dec-15 7:19am    
In my format i have got 2 audio samples per second. I think so.
[no name] 24-Dec-15 10:49am    
What I know, CP is not a code Service. Try it first by yourself. If you face a specific Problem ask here.
Сергей Козлов 24-Dec-15 18:11pm    
I always ask the question and then, while waiting for suggestions, try self.

1 solution

I have found CWave class and modified if for own purposes.

CWave class is found on the link CWave - A Simple C++ Class to Manipulate WAV Files

It seems the formula is
C++
if(local_work_time.GetSeconds()>=1)
					Sleep(1);
				else
				{
					HRESULT hr;

					AM_MEDIA_TYPE mt;
					ZeroMemory(&mt, sizeof(mt));

					hr = local_main_dialog->web_camera_dialog->pAudioGrabber->GetConnectedMediaType(&mt);

					if (SUCCEEDED(hr))
					{
						if(mt.formattype==FORMAT_WaveFormatEx)
						{
							WAVEFORMATEX *wave_format_ex = (WAVEFORMATEX *)mt.pbFormat;

							if(wave_format_ex!=NULL)
							{
								DWORD chunk_playing_time = 
									1000 *
									wave_format_ex->nAvgBytesPerSec/
									wave_format_ex->nSamplesPerSec/
									wave_format_ex->nChannels/
									(wave_format_ex->wBitsPerSample>>3)/
									(wave_format_ex->nBlockAlign>>1)
									;

								Sleep(chunk_playing_time);
							}
						}
						_FreeMediaType(mt);	
					}
					else
						Sleep(1000/CONST_AUDIO_PACKETS_PER_SECOND);		//	CONST_AUDIO_PACKETS_PER_SECOND кадров в секунду
				}


Here is the playback after getting th stream from the netwotk:
C++
//	Здесь воспроизводится аудио сампле
		{
			CWave local_wave;

			local_wave.Load(received_microphone_stream);

			/*/
			{
			std::fstream local_file;
			local_file.open("c:\\temp\\file_received.wav",std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);

			char *local_file_data = new char[CONST_MESSAGE_LENGTH_IMAGE];

			ULONG local_read_bytes = 0;

			LARGE_INTEGER liBeggining = { 0 };

			received_microphone_stream->Seek(liBeggining, STREAM_SEEK_SET, NULL);

			received_microphone_stream->Read(local_file_data,CONST_MESSAGE_LENGTH_IMAGE,&local_read_bytes);

			local_file.write(local_file_data, local_read_bytes);

			delete [] local_file_data;

			local_file.flush();

			local_file.close();
			}
			//*/


			ULARGE_INTEGER zero_size = {0,0};
			received_microphone_stream->SetSize(zero_size);

			{
				LARGE_INTEGER liBeggining = { 0 };

				received_microphone_stream->Seek(liBeggining, STREAM_SEEK_SET, NULL);
			}

			//local_wave.Save(L"c:\\temp\\cwave_received.wav");
			local_wave.Play();

			{
				//*/
				if(local_wave.IsValid()==TRUE)
				{
					DWORD chunk_playing_time = 
						1000 *
						local_wave.GetSize()/
						local_wave.GetSampleRate()/
						local_wave.GetChannels()/
						(local_wave.GetBitsPerSample()>>3);

						Sleep(chunk_playing_time+100);
				}
				//*/
			}
		}


Next improvement will be playing and waiting for end of playing in worker thread:
C++
// in new thread
local_wave.Play();

			{
				//*/
				if(local_wave.IsValid()==TRUE)
				{
					DWORD chunk_playing_time = 
						1000 *
						local_wave.GetSize()/
						local_wave.GetSampleRate()/
						local_wave.GetChannels()/
						(local_wave.GetBitsPerSample()>>3);

						Sleep(chunk_playing_time+100);
				}
				//*/
			}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900