home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Audio / AudioPlayback.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-09  |  3.6 KB  |  128 lines

  1. /* 
  2.  *  AudioPlayback.cpp 
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include <windows.h>
  25. #include <mmsystem.h>
  26. #include <malloc.h>
  27. #include <winbase.h>
  28.  
  29. #include "AudioPlayback.h"
  30.  
  31.  
  32. #define BUFFER_COUNT 20
  33. #define BUFFER_SIZE  4096*2
  34.  
  35.  
  36. // Global static module variables
  37. static HWAVEOUT hWaveOut;
  38. static HANDLE   hAudioEv;
  39. static unsigned char buffers[BUFFER_COUNT][BUFFER_SIZE];
  40. static WAVEHDR        wav_hdr[BUFFER_COUNT];
  41.  
  42. // Init sound output system
  43. int AudioPlaybackStart(TAudioPlayback *ap)
  44. {
  45.     WAVEFORMATEX waveFormat;
  46.     int i;
  47.  
  48.     if(!ap)
  49.         return 0;
  50.     // chunk_size must be a multiple of the audio buffer
  51.     if(ap->chunk_size%BUFFER_SIZE != 0)
  52.         return 0;
  53.  
  54.     waveFormat.wFormatTag       = WAVE_FORMAT_PCM;
  55.     waveFormat.nChannels        = ap->n_channels;
  56.     waveFormat.nSamplesPerSec   = ap->sample_freq; 
  57.     waveFormat.wBitsPerSample   = 16;
  58.     waveFormat.nBlockAlign      = waveFormat.nChannels * 
  59.                                   waveFormat.wBitsPerSample /8;
  60.     waveFormat.nAvgBytesPerSec  = waveFormat.nSamplesPerSec *
  61.                                   waveFormat.nBlockAlign; 
  62.     waveFormat.cbSize           = 0; 
  63.  
  64.  
  65.     // Create an event for signaling finished work with audio blocks
  66.     hAudioEv = CreateEvent(NULL, FALSE, FALSE, NULL);
  67.  
  68.     if(    waveOutOpen(  &hWaveOut, 
  69.                          WAVE_MAPPER, 
  70.                          &waveFormat, 
  71.                          (DWORD)hAudioEv, 
  72.                          0, 
  73.                          CALLBACK_EVENT)
  74.         != MMSYSERR_NOERROR )
  75.         return 0;
  76.  
  77.     // Prepare audio blocks
  78.     for(i=0; i<BUFFER_COUNT; i++)
  79.     {
  80.         wav_hdr[i].lpData          = (LPSTR) buffers[i];
  81.         wav_hdr[i].dwBufferLength  = BUFFER_SIZE;
  82.         wav_hdr[i].dwFlags         = wav_hdr[i].dwFlags | WHDR_DONE;
  83.         if( waveOutPrepareHeader( hWaveOut, &wav_hdr[i], sizeof(wav_hdr[i]) )
  84.             != MMSYSERR_NOERROR )
  85.             return 0;
  86.     }
  87.  
  88.  
  89.     return 1;
  90. }
  91.  
  92. int AudioPlaybackWrite(TAudioPlayback *ap, unsigned char *data)
  93. {
  94.     int remaining_bytes = ap->chunk_size;
  95.     int i;
  96.  
  97.     while( remaining_bytes )
  98.     {
  99.         // fill those blocks that have been played
  100.         //      and put them into the queue
  101.         for(i=0; i<BUFFER_COUNT; i++)
  102.             if( (wav_hdr[i].dwFlags & WHDR_DONE) && remaining_bytes)
  103.             {
  104.                 memcpy(buffers[i], &data[ ap->chunk_size - remaining_bytes ], BUFFER_SIZE);
  105.                 waveOutWrite( hWaveOut, &wav_hdr[i], sizeof(wav_hdr[i]) );
  106.                 remaining_bytes -= BUFFER_SIZE;
  107.             }
  108.         // if there is no more data to write, exit
  109.         if(!remaining_bytes)
  110.             break;
  111.         // wait for finished buffers
  112.         WaitForSingleObject(hAudioEv, INFINITE);
  113.     }
  114.     return 1;
  115. }
  116.  
  117. int AudioPlaybackStop(TAudioPlayback *ap)
  118. {
  119.     int i;
  120.     waveOutReset( hWaveOut );
  121.     waveOutClose( hWaveOut );
  122.     // UnPrepare audio blocks
  123.     for(i=0; i<BUFFER_COUNT; i++)
  124.     {
  125.         waveOutUnprepareHeader( hWaveOut, &wav_hdr[i], sizeof(wav_hdr[i]) );
  126.     }
  127.     return 1;
  128. }