Home

documentation - OHM ECCE - Ateneo de Manila University

image

Contents

1. unless playback is stopped wait until there s a free audio block while freeBlockCount if kbhit getch waveOutReset hWaveOut pause time bli rpause time rbli rbli audioBuffer end break current block is next block imagine array is circular waveCurrentBlock pliss waveCurrentBlock BLOCK_COUNT unless playback is stopped wait for all blocks to complete while freeBlockCount lt BLOCK COUNT if kbhit getch waveOutReset hWaveOut break CLEAN UP unprepare any blocks that are still prepared for int i 0 i lt freeBlockCount i if waveBlocks i dwFlags amp WHDR PREPARED waveOutUnprepareHeader hWaveOut amp waveBlocks i sizeof WAVEHDR DeleteCriticalSection amp criticalSection waveOutClose hWaveOut delete buffer for int i 0 i lt BLOCK COUNT i delete waveBlocks i lpData delete waveBlocks first run 1 return true Lb d KKK k k oko oko oko kok RA ok k ok kok k k k k k k ko kok k k k k ok kk k k LL LLL A I CALLBACK Procedure for WAV Audio Playback BRR KKK KK KKK KK iii A k k k k k k k k k k k k A A A k k k k k k k k k k k k k kk k CE k kk ELE void CALLBACK waveOutProc HWAVEOUT hWaveOut UINT uMsg DWORD dwInstance DWORD dwParaml DWORD dwParam2 33 WAV WAV ptr WAV dwInstance if uMsg WOM DONE return EnterCriticalSection amp WAV ptr gt criticalSection WAV ptr
2. project documentation pdf file progress reports contains progress reports of group members e conanan e suarez e villarica proposal contains approved project proposal e proposal odt o html contains project website files 5 2 Using the Software When the program is run an opening screen Figure 5 1 is displayed Press a key to enter and proceed to the main menu 19 de tt PLAYER Press a key to enter Figure 5 1 Opening Screen On the main menu Figure 5 2 the player commands are displayed Load audio Play audio Play reverse audio FLEXIBLE AUDIO PLAYER Main Menu lt L gt Load lt P gt Play lt R gt Play lt esc gt Exit Press code audio audio reverse audio program to execute command Figure 5 2 Main Menu From the main menu you may exit the program by pressing lt esc gt This will display the closing screen Figure 5 3 and then exits the program 20 PEPE ve PLAYER Exiting Figure 5 3 Closing Screen 5 2 1 Loading Audio To load audio from a WAV file press lt L gt You will be prompted to enter a filename Enter the complete filename of the WAV file you want to load Note Filenames should not contain spaces If an invalid filename is entered you are given the option to enter another filename or abort the load operation and return to the main menu If the filename that you entered is
3. val 16 val 24 return false wfx wBitsPerSample val wfx nBlockAlign wfx nAvgBytesPerSec return true wfx wBitsPerSample wfx nChannels gt gt 3 wfx nBlockAlign wfx nSamplesPerSec bool WAV SetSamplesPerSecond int val if val 8000 if val lt 8000 return false 11025 val val 22050 val gt 44100 I val 44100 wfx nSamplesPerSec val wfx nBlockAlign wfx wBitsPerSample wfx nChannels wfx nAvgBytesPerSec wfx nBlockAlign wfx nSamplesPerSec return true Pe OF int WAV GetChannels return wfx nChannels int WAV GetBits return wfx wBitsPerSample int WAV GetSamplesPerSecond return wfx nSamplesPerSec BRR KK KKK KR KK KK A RA k k k k k k k k k k k k k k A A A k ok kk k kk k k k k A kk k k k kk kk k WAV CLASS SPECIAL FUNCTIONS A RRR KKK KKK k k KK KK A kok k k k k k k k k A ok k kk kk k k k kok II bool WAV Load string filename 27 const int BLOCK SIZE 1024 char ChunkID 4 Format 4 SubchunklID 4 Subchunk2ID 4 int ChunkSize SubchunklSize SampleRate ByteRate Subchunk2Size short AudioFormat NumChannels BlockAlign BitsPerSample char Data char tempData int lastBlockSize int numBlocks char buffer char tempBuffer int blockSize 1024 Read the wave file header FILE fhandle fopen filename c str rb fread ChunkID 1 4 fhandle fread amp ChunkSize 4 1 fhandle fread F
4. gt freeBlockCount LeaveCriticalSection 4 WAV ptr gt criticalSection A1 2 UserInterface Class A1 2 1 UserInterface Class Definition userinterface h ifndef USERINTERFACE H_ define USERINTERFACE H_ include wav h include lt string gt include lt conio h gt include lt iostream gt using std string using std cout using std cin class UserInterface private XMCS Sound 2 bool state int recChannels int recBits int recSamplesPerSecond int recRadix void PrintPlayerMenu char GetYesNoAnswer string InputString void Player void rPlayer void Loader public UserInterface void PrintEnterScreen void PrintExitScreen void PrintUserInterface char GetAndExecuteCommand fendif 34 A1 2 2 UserInterface Class Implementation userinterface cpp include userinterface h include lt iostream gt include lt stdio h gt using namespace std UserInterface UserInterfa recChannels recBits recSamplesPerSecond recRadix state ce La 8 11025 16 false void UserInterface PrintEnterScreen system CLS cout lt lt lt lt lt lt lt lt lt lt lt lt lt lt zn lt lt lt lt lt lt lt lt lt lt getch PLAYER Press a key to enter voi
5. userinterface h int main int argc char argv UserInterface UI UI PrintEnterScreen do UI PrintUserInterface while UI GetAndExecuteCommand 0x1b UI PrintExitScreen Sleep 1000 return EXIT SUCCESS 39 Bibliography Baterina Anna Veronica and Jeric Macalintal XMCS Player Recorder Davidson Nathan How to Load a Wave File Internet Available from http www cpp home com tutorials 333_1 htm accessed 22 August 2010 Digital Audio Creating a WAV RIFF file Internet Available from http www topherlee com software pcm tut wavformat html accessed 30 August 2010 Overton David Playing Audio in Windows using waveOut Interface Internet Available from http www downloadfreescript com source code detail text playing audio in windows using waveout interface by david overton in graphics sound c c htm accessed October 9 2010 Reisdorph Kent Low level wave audio part 2 Internet Available from http bcbjournal org articles vol2 9808 Low level_wave_audio__part_2 htm accessed 15 August 2010 Wilson Scott WAVE PCM soundfile format Internet Available from https ccrma stanford edu courses 422 projects WaveFormat accessed 8 August 2010 40
6. do system CLS cout lt lt lt lt FLEXIBLE AUDIO PLAYER Load audio from WAV file xn lt lt lt lt Enter complete filename of WAV file filename InputString c N if filename tu Invalid filename Enter another filename cout lt lt n n lt lt n n Y N c GetYesNoAnswer while toupper c Y if filename cout lt lt n lt lt n lt lt n lt lt Cancelled operation my else cout lt lt n lt lt Loading audio Please wait if Sound state amp 1 Load filename cout lt lt n lt lt n 37 n n n Xn lt lt Loaded W lt lt filename lt lt W else cout lt lt n SSN lt lt Loading failed getch char UserInterface GetAndExecuteCommand char c getch switch toupper c case Pe Player break case R rPlayer break case L if state Loader break default return Cc break return c string UserInterface InputString char buff 100000 cin getline buff 100000 n if strlen buff 99999 return else for int i 0 i lt strlen buff i if buff i return return string buff char UserInterface GetYesNoAnswer char c do c getch while toupper c Y amp amp toupper c N return c 38 A1 3 main function main cpp include
7. order to obtain these samples that will be stored in the linked list we first have to use the fread function to read the contents of the wave file and then store them temporarily into a buffer After this the contents of the buffer are transferred to the list When the list is filled with all the audio data samples only then can the contents be sent to the audio output device First the audio file is read starting from the header and then the data chunks The data obtained from the header is extracted and incorporated into the WAVEFORMATEX structure which defines the properties of the entire audio data After this is done the data read from the file is copied to a buffer and then stored into a list For playback to be achieved first the audio output device has to be opened and then the data within the list is sent to the audio output device and then played one at a time But before sending the data to the device the blocks are first prepared in multiple buffers in order to minimize the amount of time spent in between playback of one bock of data and the next By queuing the blocks being sent to the audio device we are effectively playing a group of samples and after they are done a new set of queued data is ready to be played and thus continuously provides a much more efficient method of playback Also using variables that compare the total number of blocks with the current blocks being processed it is possible to determine when the pl
8. was pressed void PrintPlayerMenu This is a private function that displays the available commands for the player void Player This is a private function that performs the tasks necessary for audio playback including the display of the interface It calls the WAV class function Play void rPlayer This is a private function that performs the tasks necessary for reverse audio playback including the display interface It calls the WAV class function rPlay void Loader This is a private function that performs the necessary tasks for loading an audio to the player including the display of the interface It calls the WAV class function Load char GetYesNoAnswer This is is a private function that reads a key press and repeatedly does so until the key pressed is either Y or N string InputString This is a private function that reads a string from the user It is used for inputting a string to ensure that the input has a valid form before being processed further 18 5 User s Manual 5 1 CDROM Contents e CD root directory gt o index html homepage of project website redirector o software wav h WAV class definition wav cpp WAV class implementation userinterface h UserInterface class definition userinterface cpp UserInterface class implementation main cpp main function documentation odt project documentation odt file documentation pdf
9. Flexible Audio Player A Project by Jason Franco S Conanan Miguel Emmanuel D J Suarez Nico Angelo F Villarica Submitted to Luisito L Agustin Instructor ELC152 In Partial Fulfillment of the Requirements for the Course ELC152 Signal Processing Department of Electronics Computer and Communications Engineering School of Science and Engineering Loyola Schools Ateneo de Manila University Quezon City Philippines October 2010 Abstract A console based flexible audio player is implemented using C Along with this is the design and implementation of an audio class that contains functions for playback reverse playback and pause and resume The flexible audio player is capable of playing 8 bit 16 bit 24 bit or 32 bit single and dual channel WAV audio with sampling rates of up to 44 1 KHz Acknowledgements We would like to acknowledge the following people who made significant contributions to this work First and foremost we would like to express our thanks to our God who is the source of all Wisdom and Knowledge Thanks to Mr Luisito Agustin for his unwavering guidance as we completed this project Thanks to our classmates for their support and suggestions which helped in the refining of our project Thanks to the XMCS group Anna Veronica C Baterina amp Jeric V Macalintal whose source code and documentation served as a guide to our project Table of Contents Introduction a ee bara idilli
10. O Userintertace Class cus d tolo eo ooo osla oak ooo ose 34 A1 2 1 UserInterface Class Definition userinterface h 34 A1 2 2 UserInterface Class Implementation userinterface cpp 35 21 3 AUTOR omai ia 39 Bibliograpliys udo eire oil etus edo lobo aliis ii dii fio af ia 40 1 Introduction 1 1 Project Objectives The objective of this project is to create an audio player that can perform forward playback and reverse playback of audio files in the wav format The player must also include pause and resume options during forward or reverse playback 1 2 Project Scope and Limitations The flexible audio player has support for 8 bit 16 bit 24 bit or 32 bit WAV format audio Forward and reverse playback of audio is limited to single channel and dual channel audio The sampling rates for forward and reverse playback ranges from 8 KHz to 44 1 kHz Forward and reverse playback can be paused and resumed by the user at anytime during playback However if the user wishes to stop playback he must the load the file again The audio for playback is read from a WAV audio file has the wav file extension 1 3 Overview of Components The creation of the Flexible Audio Player involves the implementation of the following classes e An WAV class for the main components of this project The WAV class would provide forward and reverse playback functionalities as well as functions for pausing and res
11. aca alia 6 IL Project ODJECUVES rotaia 6 T2 Project o CODO eee ti c mersit id ee ee 6 1 3 Overview 0EComponenisiz sonia diesis 6 1 4 Supported Audio File The WAV File Format eene 7 2 WAV Audio Playback Implementation e a deett tiene ena deant od eonun 9 2 1 Forward Playback uoce Bt lella goes Ret ance do aes t oa IE vo Ra NE 9 2 2 Reverse Playback entr en PLA SY th FS rit 10 2 3 Forward Playback and Reverse Playback Procedure Flowcharts 11 SM A VAL M T aio 15 3 TL Constructor and Destructor race oa bade le Bates 13 35 2 Data Members e e E SM t C 13 33 ACCOSSOrS audita iode te uen uti teas ide teca saei equ reo e 14 SA File Input PUNTO scarna ela 15 3 5 Friend CALLBACK FUlctOn 2 rre liana aa 16 A The Us rInt rface Glass role ae eine 17 4 1 Constructor and Destr ctot uniti ini 17 42 0bjects and Data Types s ioco ess e tiit esatta deuda ai 17 A Be IEUBGUDHS oci oca osi tetsns tola Seite sive eae teehee so cd oh seco adco M escape itat 17 3e Hser sVEIDALE oo adea ee oce de tue mca atm eae ce Ce 19 Sel CDROM Contents arsenali 19 5 2 Usine the Sol wate aiuola 19 6 Results and Recommendanonis sti iii 23 Appendix 1 Source Code i ERI RAR re 24 AN WAV CAS c iias eio gerante ese aaa ege eiia eS taak 24 A1 1 1 WAV Class Definition wav h sese 24 A1 1 2 WAV Class Implementation wav cpp eene 26 AT
12. astBufferSize delete raudioBuffer begin raudioBuffer erase raudioBuffer begin 29 lastBufferSize maxBufferSize 0 return true bool WAV Play const int BLOCK COUNT 10 number of audio blocks for playback HWAVEOUT hWaveOut handle WAVEHDR waveBlocks array of wave headers int waveCurrentBlock index of current block being played int block size size of block to be played char buffer temporary buffer INITIALIZATION AND MEMORY ALLOCATION block size maxBufferSize waveBlocks new WAVEHDR BLOCK COUNT for int i 0 i lt BLOCK COUNT i memset amp waveBlocks i 0 sizeof WAVEHDR waveBlocks i lpData new char block size freeBlockCount BLOCK COUNT waveCurrentBlock 0 InitializeCriticalSection amp criticalSection PLAYBACK open audio output device if waveOutOpen amp hWaveOut WAVE MAPPER amp wfx DWORD PTR waveOutProc DWORD PTR this CALLBACK FUNCTION l MMSYSERR_NOERROR return false if first run 0 pause time audioBuffer begin rpause time raudioBuffer end bli pause time rpause time rpause time r buffer new char block_size unless playback is stopped play everything contained in the list while bli audioBuffer end if kbhit getch waveOutReset hWaveOut pause time bli rpause time rbli bli audioBuffer end break copy data to be
13. ayback of the entire data chunk is done After the playback is finished the audio output device should be closed These steps are also followed for Reverse Playback discussed below To implement the pause and resume function for forward playback a forward iterator is used to keep track of what audio block has been played As an audio block is played the iterator increments When the user pauses the playback the iterator indicates what block will be played when the user resumes the forward playback 2 2 Reverse Playback The concept of reverse playback is very much similar to the concept of forward playback discussed above A separate linked list is prepared that will serve as a storage for the reversed audio samples and these will later be sent to the audio output device one block at a time In order to obtain these reversed samples we first have to use the fread function to read the contents of the wave file and then store them temporarily into a separate buffer The program then reverses the samples and stores them in the separate linked list When the list is filled with all the reversed audio samples only then can the contents be sent to the audio output device To implement the pause and resume function for reverse playback a separate reverse iterator is used to keep track of what audio block has been played As an audio block is played the reverse iterator increments When the user pauses the playback the reverse iterator in
14. d UserInterface PrintExitScreen system CLS cout lt lt lt lt lt lt lt lt lt lt lt lt ut lt lt lt lt lt lt lt lt xn lt lt PLAYER Exiting void UserInterface PrintPlayerMenu cout lt lt lt L gt lt lt lt P gt lt lt lt R gt lt lt n lt lt n lt lt n lt lt Play Load audio Play audio reverse audio 35 n n n n n n n n n n n n n n n n n n n n n n n n n n n n void UserInterface PrintUserInterface system CLS cout lt lt n lt lt FLEXIBLE AUDIO PLAYER Main Menu n lt lt n lt lt n PrintPlayerMenu cout lt lt n lt lt esc Exit program yan lt lt n ZA n lt lt Press code to execute command FORWARD PLAYBACK PLAYER void UserInterface Player char c do system CLS cout lt lt n lt lt WAV PLAYER Play WAV audio Mn lt lt n lt lt n lt lt Playing n lt lt Press a key to stop playing if Sound state amp 1 Play cout lt lt n lt lt n n lt lt XMCS Information n lt lt n lt lt channels lt lt Sound state amp 1 G
15. dicates what block will be played when the user resumes the reverse playback 10 2 3 Forward Playback and Reverse Playback Procedure Flowcharts N m v Prepare n blocks of audio buffers to be used for playing where n gt 1 free lt 1 L Open an audio output device ptime begin rptime end bli ptime rbli rptime Playback paused Stop playback Close output gt gt device Playback paused END ae N Il the buffers in the list already queued for playback Copy the contents of the next unplayed buffer in the list to the next free buffer Note forward iterator increases reverse iterator decreases free lt free 1 Queue the next non empty buffer for playback Figure 2 1 Forward Playback Procedure 11 Prepare n blocks of audio buffers to be used for playing where n gt 1 free lt 1 v Open an audio output device Y ptime end rptime begin Y Playback paused bli ptime rbli rptime Stop playback C E END Close output gt device NV o Playback paused All the buffers in th list already queued for playback Copy the contents of the next unplayed buffer in the list to the next free buffer Note forward
16. e other messages that are sent to it 16 4 The UserInterface Class The UserInterface class provides the functionalities needed for user interaction It contains two WAV objects that provide forward and reverse playback functionalities and functions for the program s user interface 5 1 Constructor and Destructor UserInterface The constructor initializes the audio parameters to the default values UserInterface The destructor was not explicitly defined the provided default destructor is used for object destruction 5 2 Objects and Data Types int recChannels int recBits int recSamplesPerSecond These are private members used to store the initial parameters The values of these will change depending on the header of the loaded WAV file 5 3 Functions void PrintEnterScreen This is a public function that prints the opening screen of the program void PrintExitScreen This is a public function that prints the closing screen of the program 4 Reference 1 17 void PrintUserInterface This is a public function that prints the main menu It calls the PrintPlayerMenu to print the specific commands applicable only to the current mode char GetAndExecuteCommand This is a public function that waits for a key to be pressed Once a key is pressed it executes the task corresponding to the key if such task is defined Otherwise it does nothing It always returns the value of the key that
17. e time audioBuffer end rpause time raudioBuffer begin bli pause time rpause time rpause time buffer new char block size unless playback is stopped play everything contained in the list while rbli raudioBuffer end if kbhit getch waveOutReset hWaveOut pause time bli rpause time rbli rbli raudioBuffer end break copy data to be used to buffer memcpy buffer rbli block size rref rbli t if the last element of the list size may not be equal to the maximum if rref raudioBuffer end block size lastBufferSize unprepare header to be used for playback in case not yet unprepared if waveBlocks waveCurrentBlock dwFlags amp WHDR PREPARED waveOutUnprepareHeader hWaveOut amp waveBlocks waveCurrentBlock sizeof WAVEHDR specify buffer length and audio samples waveBlocks waveCurrentBlock dwBufferLength sizeof char block size memcpy waveBlocks waveCurrentBlock lpData buffer sizeof char block size 32 prepare header to be used for playback if waveOutPrepareHeader hWaveOut amp waveBlocks waveCurrentBlock sizeof WAVEHDR MMSYSERR NOERROR return false play waveOutWrite hWaveOut amp waveBlocks waveCurrentBlock sizeof WAVEHDR one less temporary buffer available EnterCriticalSection amp criticalSection freeBlockCount LeaveCriticalSection amp criticalSection
18. etChannels lt lt n lt an bits per sample lt lt Sound stateg amp l GetBits lt lt n lt lt samples per second lt lt Sound state amp 1 GetSamplesPerSecond lt lt n NIU lt lt n lt lt Playback successful n lt lt n else cout lt lt n lt lt n lt lt Playback failed n lt lt Nr cout lt lt Repeat Y N c GetYesNoAnswer while toupper c Y REVERSE PLAYBACK PLAYER void UserInterface rPlayer chay gr do system CLS cout lt lt n lt lt WAV PLAYER Play WAV audio Mn lt lt n 36 lt lt n lt lt Playing Nnm ac Press a key to stop playing if Sound state amp 1 rPlay cout lt lt n lt lt n lt lt n lt lt XMCS Information n lt lt n en channels lt lt Sound state amp 1 GetChannels lt lt n lt lt bits per sample lt lt Sound state amp 1 GetBits lt lt n lt lt samples per second lt lt Sound state amp 1 GetSamplesPerSecond lt lt n lt lt n lt lt n lt lt Playback successful n lt lt Nini else cout lt lt n lt lt n lt lt Playback failed n n cout lt lt Repeat Y N c GetYesNoAnswer while toupper c Y void UserInterface Loader string filename chat
19. fer 28 char charBuffer char reverseBuffer blockSize int reverse counter 0 load character pointers into reverse list for int i 0 i lt numBlocks i if i numBlocks 1 blockSize lastBlockSize reverseBuffer new char blockSize for int j 0 j lt blockSize charBuffer new char charPerSample charBuffer reverse counter Data i j reverse countertt counts if there have been charPerSample chars loaded if reverse counter charPerSample loads from charbuffer to reverseBuffer in correct order for int k 0 k lt charPerSample k reverseBuffer blockSize j 1 k charBuffer k reverse counter 0 once reverseBuffer is full it loads into the linked list raudioBuffer raudioBuffer insert raudioBuffer end reversBuffer free allocated memory delete tempBuffer delete charBuffer delete reverseBuffer delete for int i 0 i lt numBlocks i delete Datali delete Data bool WAV Unload if laudioBuffer size return false int i audioBuffer size int j 0 while audioBuffer size amp amp maxBufferSize if audioBuffer size 1 lastBufferSize delete audioBuffer begin audioBuffer erase audioBuffer begin if raudioBuffer size return false int i raudioBuffer size integ 0 while raudioBuffer size amp amp maxBufferSize if raudioBuffer size 1 l
20. ffer ifstream amp int char others shared by functions like global variables short int lineCharsRead CRITICAL SECTION criticalSection volatile int freeBlockCount public constructors destructor WAV WAV WAV amp WAV load audio samples from a file filename raudioBuffer endif bool Load string filename empty the list audioBuffer and raudioBuffer bool Unload play audio samples in the list audioBuffer bool Play bool rPlay accessors bool SetChannels int int GetChannels bool SetBits int int GetBits bool SetSamplesPerSecond int int GetSamplesPerSecond callback procedure for playback to both lists audioBuffer and friend void CALLBACK waveOutProc HWAVEOUT hWaveOut UINT uMsg DWORD dwInstance DWORD dwParaml DWORD dwParam2 25 A1 1 2 WAV Class Implementation wav cpp include wav h KKK KR KKK kok kok k k Koko k k k k kok kok k kk kok k kk k kok k kk kok k kk E I III XMCS CLASS CONSTRUCTORS AND DESTRUCTOR BRR KK KK RK KK KA RRA A A kok kok k k k k k k k k k k k k k k k k k k k k k kok A k k k k k kk kk k WAV WAV audio format wfx nChannels IL wfx wBitsPerSample 16 wfx nSamplesPerSec 22050 wfx cbSize 0 wfx wFormatTag WAVE FORMAT PCM wfx nBlockAlign wfx wBitsPerSample wfx nChannels gt
21. gt 3 wfx nAvgBytesPerSec wfx nBlockAlign wfx nSamplesPerSec others radix 16 maxBufferSize 0 lastBufferSize 0 WAV WAV WAV amp wav wfx wav wfx radix wav radix maxBufferSize wav maxBufferSize lastBufferSize wav lastBufferSize int block size maxBufferSize char buffer for bli wav audioBuffer begin bli wav audioBuffer end bli ref bli if ref wav audioBuffer end block size lastBufferSize buffer new char block size memcpy buffer bli block size audioBuffer insert audioBuffer end buffer for rbli wav raudioBuffer begin rbli wav raudioBuffer end rbli rref rbli if rref wav raudioBuffer end block size lastBufferSize buffer new char block size memcpy buffer rbli block size raudioBuffer insert raudioBuffer end buffer WAV WAV Unload Lb d ARR RARA kok oko oko ok k k A k k k A A k k k k k k k k k k k k k k k k k ok k k kk k k kk kok RARA k k k kk kk k WAV CLASS ACCESSOR FUNCTIONS TA BRK KKK KKK KR KK KK A A k k k k k k k k k k k k k k AA k k k k k k k k k k k k k k k EE 26 bool WAV SetChannels int if val val return false wfx nChannels wfx nBlockAlign wfx nAvgBytesPerSec val 2 val wfx wBitsPerSample wfx nChannels wfx nBlockAlign wfx nSamplesPerSec 2x35 return true bool WAV SetBits int val if val
22. iterator decrements reverse iterator increments free lt free 1 Queue the next non empty buffer for playback Figure 2 2 Reverse Playback Procedure 12 3 The WAV Class The WAV class provides the WAV forward and reverse playback and pause and resume functionalities It is the main class component of this project This class was based on the XMCS class of the XMCS Player and Recorder project 3 1 Constructors and Destructor WAVI The default constructor initializes the fields of the WAVEFORMATEX structure to their default values some of these values are constant It also initializes the buffer size indicators to O WAV WAV amp wav The WAV audio is stored using a linked list with each element containing a character pointer character array The copy constructor was defined so that copying of audio from another XMCS object is done properly WAV The destructor unloads the currently loaded audio if any which causes any previously allocated memory for the audio samples to be deallocated 3 2 Data Members WAVEFORMATEX wfx This structure is a private member that contains the format of the XMCS audio Some of its fields are kept constant Reference 1 13 list lt char gt audioBuffer This is a private member that is used to store the audio samples int maxBufferSize This is a private member that is used to store the maximum buffer size in audioB
23. mber of samples per second When an audio file has a sampling rate of 44 100 Hz this means that 1 second of audio has 44 100 samples Bit size determines how much information can be stored in a file Most WAV files today have a bit size of 16 bits 8 bit WAV files are smaller but have less resolution Bit size deals with amplitude The greater the resolution of the file the greater the dynamic range of the file Channels are the number of separate recording elements in the data One channel is mono and dual channel is stereo The data is the individual audio samples An individual sample is the bit size times the number of channels Samples are placed end to end to form the data Reference 3 2 WAV Audio Playback Implementation This chapter provides a discussion on WAV audio playback specifically the implementation used in this project The implementation supports forward and reverse playback The WAV file contains audio samples which the program divides into blocks for efficient memory usage The blocks of audio samples are stored in a list as elements Whether it is forward or reverse playback these blocks are sent one by one to the audio output device 2 1 Forward Playback The concept of playback revolves around the idea of sending blocks or samples of audio data into an output audio device A linked list is prepared that will serve as a storage for the audio data and is later sent to the audio output device one block at a time In
24. not associated with an existing file or with a WAV file no audio will be loaded The loading of the file may take some time Avoid performing other commands until the result of the file loading operation is displayed Files to be loaded must be 8 bit 16 bit 24 bit or 32 bit single or dual channel audio with sampling rates ranging from 8 kHz to 44 1 kHz 21 5 2 2 Playing Loaded Audio You are given the option to play the audio forwards or backwards To play the loaded audio forwards press lt P gt To play the loaded audio in reverse press lt R gt Anytime during forward or reverse playback you have the option to pause the playback by pressing a key Press a key again to resume playback After completion of forward or reverse playback the result of the operation and the WAV audio information would be displayed 22 6 Results and Recommendations A Flexible Audio Player was not completely developed The following improvement over the XMCS player was completed e Load WAV file into audio buffer The group was able to complete the following e Load WAV file of any file size and any bit rate as long as it is a multiple of 8 e Successfully reverse the audio samples for reverse playback e Successfully open audio device As an improvement over this project it is recommended that the following features be revised e Play and rPlay functions e Implement a pause function that does not use iterators u
25. ormat 1 4 fhandle fread SubchunklID 1 4 fhandle fread amp Subchunk1Size 4 1 fhandle fread amp AudioFormat 2 1 fhandle fread amp NumChannels 2 1 fhandle lt fread amp SampleRate 4 1 fhandle fread amp ByteRate 4 1 fhandle fread amp BlockAlign 2 1 fhandle fread amp BitsPerSample 2 1 fhandle fread amp Subchunk2ID 1 4 fhandle fread amp Subchunk2Size 4 1 fhandle lastBlockSize Subchunk2Size blockSize charPerSample BitsPerSample 8 if lastBlockSize 0 numBlocks Subchunk2Size blockSize 1 else numBlocks Subchunk2Size blockSize lastBufferSize lastBlockSize maxBufferSize blockSize dynamically allocate the array of blocks Data new char numBlocks dynamically allocate each block array for int i 0 i lt numBlocks i Data i new char blockSize load audio data into the dynamic array Data by block for int i 0 i lt numBlocks i tempData new char blockSize fread tempData 1 blockSize fhandle memcpy Data i tempData blockSize fclose fhandle from the Data array load the samples into the forward list audioBuffer for int i 0 i lt numBlocks i if i numBlocks 1 blockSize lastBlockSize tempBuffer new char blockSize buffer new char blockSize for int j 0 j lt blockSize j tempBuffer j Data il jl memcpy buffer tempBuffer blockSize audioBuffer insert audioBuffer end buf
26. plete while freeBlockCount lt BLOCK COUNT if kbhit getch waveOutReset hWaveOut break CLEAN UP unprepare any blocks that are still prepared for int i 0 i lt freeBlockCount i if waveBlocks i dwFlags amp WHDR PREPARED waveOutUnprepareHeader hWaveOut amp waveBlocks i sizeof WAVEHDR DeleteCriticalSection amp criticalSection waveOutClose hWaveOut delete buffer for int i 0 i lt BLOCK COUNT i delete waveBlocks i lpData delete waveBlocks first_run 1 return true bool WAV rPlay 31 const int BLOCK COUNT 10 number of audio blocks for playback HWAVEOUT hWaveOut handle WAVEHDR waveBlocks array of wave headers int waveCurrentBlock index of current block being played int block size size of block to be played char buffer temporary buffer INITIALIZATION AND MEMORY ALLOCATION block size maxBufferSize waveBlocks new WAVEHDR BLOCK COUNT for int i 0 i lt BLOCK COUNT i memset amp waveBlocks i 0 sizeof WAVEHDR waveBlocks i lpData new char block size freeBlockCount BLOCK COUNT waveCurrentBlock 0 InitializeCriticalSection amp criticalSection PLAYBACK open audio output device if waveOutOpen amp hWaveOut WAVE MAPPER amp wfx DWORD PTR waveOutProc DWORD PTR this CALLBACK FUNCTION l MMSYSERR_NOERROR return false if first run 0 paus
27. sing the waveOutPause and waveOutRestart functions 23 Appendix 1 Source Code A1 1 WAV Class A1 1 1 WAV Class Defintion wav h ifndef WAV H define WAV H include include include include include include include utility h lt windows h gt lt mmsystem h gt lt fstream gt lt string gt lt list gt lt conio h gt using std string using std ifstream using std ofstream using std list class XMCS private to store audio format WAVEFORMATEX wfx list for audio samples and buffer size indicators list lt char gt audioBuffer list lt char gt raudioBuffer int maxBufferSize int lastBufferSize int charPerSample 1 declare iterators for pause play and rplay public list lt char gt iterator bli public list lt char gt iterator ref public list lt char gt iterator rbli public list lt char gt iterator rref public list lt char gt iterator pause time public list lt char gt iterator rpause time variable for checking the first run of either Play or rPlay set to 0 to indicate first run has happened public int first run 0 24 file radix needed for reading and writing int radix file input functions char GetNextNonWhiteSpace ifstream amp void DiscardUpToEol ifstream short int GetNextSample ifstream amp bool ReadFileHeader ifstream amp int ReadBlockToBu
28. the function returns false 15 3 4 File Input Functions The file input function is a private function that is used to read WAV files It accepts an input stream as one argument which is associated with the file bool Load string filename This is a public function that loads the contents of the file associated with filename if it is a WAV file into the object The audio samples are stored in audioBuffer and raudioBuffer while the other information are stored in wfx The indicators maxBufferCount and lastBufferCount are also set to their appropriate values If the input file is a WAV file and the loading is successful it returns true If the input file is not a WAV file or the loading failed it returns false If the input file format is invalid then its contents are not loaded and the currently loaded audio is erased 3 5 Friend CALLBACK Function friend void CALLBACK waveOutProc HWAVEOUT hWaveOut UINT uMsg DWORD dwlnstance DWORD dwParaml DWORD dwParam2 This function is the callback procedure for audio playback This is not a member of the WAV class but since it is a friend function it is included the discussion of the WAV class Because it needs access to a private variable in the WAV class it was made a friend function When this function receives the message WOM_DONE through uMsg which indicates that playback of the current buffer is done it increments the value of the free block counter freeBlockCount It ignores th
29. the number of channels which is contained in wfx bool SetChannels int channels This function checks if channels contains a valid and acceptable value for the number of channels If it is this function changes the number of channels stored in wfx to channels and returns true Also it updates the fields in wfx whose value depends on the number of channels Otherwise the current value in wfx is retained and the function returns false int GetBits This function returns the number of bits per sample which is contained in wfx bool SetBits int bits This function checks if bits contains a valid and acceptable value for the number of bits per sample If it is this function changes the number of bits per sample stored in wfx to bits and returns true Also it updates the fields in wfx whose value depends on the number of bits Otherwise the current value in wfx is retained and the function returns false int GetSamplesPerSecond This function returns the number of samples per second which is contained in wfx bool SetSamplesPerSecond int samplesPerSec This function checks if samplesPerSec contains a valid and acceptable value for the sampling rate in Hz If it is this function changes the number of samples per second stored in wfx to samplesPerSec and returns true Also it updates the fields in wfx whose value depends on the number of samples per second Otherwise the current value in wfx is retained and
30. uffer the number of bytes in the first n 1 elements where n is the size of audioBuffer int lastBufferSize This is a private member that is used to store the number of audio bytes in the last buffer of audioBuffer This may be less than or equal to maxBufferSize volatile int freeBlockCount This is a private member that is used to store information needed for audio playback It contains the number of free buffers available The information it provides is used to either determine whether or not a free buffer is already available or determine whether or not playback of the whole file is already done It is modified in more than one thread of execution in the program thus it was declared with the volatile keyword CRITICAL SECTION criticalSection This is used to create a critical section The critical section is entered every time the value of freeBlockCount is modified since freeBlockCount is modified in two functions which may execute concurrently This ensures that only one function could modify it at a given time 3 3 Accessors The accessors are public functions used to either read or modify the WAV information which are stored as private data members These functions prevent invalid values to be assigned to the fields or data types For every field or data type that stores a WAV audio or file information there is a corresponding pair of get and set accessors 14 int GetChannels This function returns
31. uming and stopping of playback in either direction e A UserInterface class for the user interface of the program The UserInterface class provides the functions needed for the program s console based interface The three classes above are used to create the Flexible Audio Player 1 4 Supported Audio File The WAV File Format The WAV file format is a subset of Microsoft s RIFF specification for the storage of multimedia files A WAV file contains a header and the raw data chunks in time format Figure 1 1 shows a diagram of the canonical WAV file format endian big little big big little little little little little little little big little little File offset bytes 20 22 24 28 32 34 36 40 44 field name AudioFormat SampleRate ByteRate BlockAlign BitsPerSample Field Size bytes R Subchunk Size The RIFF chunk descriptor The Format of concern here is WAVE which requires two sub chunks fmt and data The fmt sub chunk describes the format of the sound information in the data sub chunk The data sub chunk Indicates the size of the sound information and contains the raw sound data Figure 1 1 The Canonical WAV File Format The header the beginning of a WAV file is used to provide specifications on the file type Reference 6 sample rate sample size bit size of the file and the file s overall length Sample rate is the nu
32. used to buffer memcpy buffer bli block size ref blitt if the last element of the list size may not be equal to the maximum if ref audioBuffer end block_size lastBufferSize unprepare header to be used for playback in case not yet unprepared if waveBlocks waveCurrentBlock dwFlags amp WHDR PREPARED 30 sizeof WAVEHDR waveOutUnprepareHeader hWaveOut amp waveBlocks waveCurrentBlock specify buffer length and audio samples waveBlocks waveCurrentBlock dwBufferLength sizeof char block size memcpy waveBlocks waveCurrentBlock lpData buffer sizeof char block size prepare header to be used for playback if waveOutPrepareHeader hWaveOut amp waveBlocks waveCurrentBlock sizeof WAVEHDR MMSYSERR NOERROR return false play waveOutWrite hWaveOut amp waveBlocks waveCurrentBlock sizeof WAVEHDR one less temporary buffer available EnterCriticalSection amp criticalSection freeBlockCount LeaveCriticalSection amp criticalSection unless playback is stopped wait until there s a free audio block while freeBlockCount if kbhit getch waveOutReset hWaveOut pause time bli rpause time rbli bli audioBuffer end break current block is next block imagine array is circular waveCurrentBlock rbli waveCurrentBlock BLOCK COUNT unless playback is stopped wait for all blocks to com

Download Pdf Manuals

image

Related Search

Related Contents

超音波式レベルスイッチ KUT3001  Bedienungsanleitung  M a rio K art™ 8  Guia usário  UG-232 - Analog Devices  manualistica troncatrice disco alluminio  HA - 4 8 15 16 23 42  dc-oc-12u  Gigabyte GV-R557D3-1GI AMD Radeon HD5570 1GB graphics card  Xerox C60/C70 Quick Guide  

Copyright © All rights reserved.
Failed to retrieve file