C + + playback audio stream (PCM bare stream)

Source: Internet
Author: User

Directly on the code, if necessary, you can directly build a Win32 console program and then copy the code to change the file name can be used (note the channel and frequency with your own file corresponding OH). Of course, I myself also use VS2008 (VS2013 too much, strongly recommend or use VS2013, but it is too big project, upload it quite laborious, so still with VS2008 rewrite. Wrote an example upload, if you need the following (own no download points, so get 3 points, forgive haha): Click to open the link

This code is to open a file to intercept a piece of data and then play it, you can easily add a thread to change the way the network transmission.

#Include"StdAfx.h"#Include<stdio.h>#Include<Windows.h>#pragma comment (lib,"Winmm.lib")Char buf[1024 *1024 *4];int _tmain (int argc, _tchar* argv[]) {file* THBGM;Fileint cnt; Hwaveout hwo; WAVEHDR WH; WaveFormatEx wfx; HANDLE Wait;wfx.wformattag = WAVE_FORMAT_PCM;Format the waveform sound wfx.nchannels =1;Set the number of channels for an audio file Wfx.nsamplespersec =8000;Set the sample frequency for each channel when playing and recording wfx.navgbytespersec =16000;Sets the average data transfer rate for the request, in unit byte/s. This value is useful for creating buffer sizes Wfx.nblockalign =2;Set block alignment in bytes Wfx.wbitspersample =16;wfx.cbsize =0;The size of the extra information wait = CreateEvent (Null0,0,NULL); Waveoutopen (&hwo, Wave_mapper, &wfx, (dword_ptr) Wait,0L, callback_event);Opens a given waveform audio output device for playback fopen_s (&AMP;THBGM,"PAOMO.PCM","RB"); cnt = Fread (buf,sizeofChar),1024 *1024 *4, THBGM);Read the file 4M data to the memory to play, through this part of the modification, increase the thread can become the real-time transmission of network audio data. Of course, if you want to play the full audio file, but also to change a little bit hereint dolenght =0;int playsize = 1024; while (CNT) {//this section needs to pay special attention to the fact that after the loop back does not take too long to do the reading data and so on, otherwise the gap in each cycle will have "Da da" noise wh.lpdata = BUF + dolenght;wh.dwbufferlength = playsize;wh.dwflags = 0l;wh.dwloops = 1l;waveoutprepareheader (hwo, &WH, sizeof (WAVEHDR)); //prepare a waveform data block for playback waveoutwrite (hwo, &WH, sizeof (WAVEHDR)); //plays the second function in audio media WH specifies the data WaitForSingleObject (wait, INFINITE); //is used to detect the signal state of the Hhandle event, the thread is temporarily suspended when the function is called in a thread, and if, within the suspended infinite milliseconds, the object waiting by the threads becomes signaled. The function immediately returns Dolenght = dolenght + playsize;cnt = cnt-playsize;} Waveoutclose (HWO); fclose (THBGM); return 0;}         

C + + plays audio stream (PCM bare stream)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.