home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Audio / ExportAudio.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  4.4 KB  |  146 lines

  1. /* 
  2.  *  WaveOutput.cpp 
  3.  *         Original code by Timothy J. Weber.
  4.  *
  5.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  6.  *
  7.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  8.  *    
  9.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation; either version 2, or (at your option)
  12.  *  any later version.
  13.  *   
  14.  *  FlasKMPEG is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *  GNU General Public License for more details.
  18.  *   
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with GNU Make; see the file COPYING.  If not, write to
  21.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  22.  *
  23.  */
  24.  
  25. #include <windows.h>
  26. #include "../FlasKMPEG.h"
  27. #include "../resource.h"
  28. #include "../RunState.h"
  29. #include "WaveOutput.h"
  30.  
  31. extern TRunState rs;
  32.  
  33.  
  34. static double percent;
  35. static int    worker_thread_is_finished;
  36. static int    end_conversion_requested;
  37. static WaveFile *wavFile;
  38. static char pFile[1024];
  39.  
  40. DWORD WINAPI WorkerThread(LPVOID lpParameter)
  41. {
  42.     unsigned char *data;
  43.     while( rs.audio->GetSamples(0, (short **)&data, (4096*6)>>2) 
  44.         && !end_conversion_requested)
  45.     {
  46.         wavFile->Write( (const char *)data, 4096*6 );
  47.         percent= ((double)((i64)rs.audio->GetStreamPos() - rs.startFilePos)
  48.                 / (double)((i64)rs.audio->GetStreamSize()- rs.startFilePos))*100;
  49.     }
  50.     wavFile->Close();
  51.     worker_thread_is_finished = 1;
  52.     return 0;
  53. }
  54.  
  55.  
  56.  
  57. LRESULT CALLBACK DlgExporting(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  58. {
  59.     static UINT timer;
  60.     HANDLE myThread = NULL;
  61.  
  62.     switch (message)
  63.     {
  64.         case WM_TIMER:
  65.             if(worker_thread_is_finished)
  66.             {
  67.                 CloseHandle( myThread );
  68.                 EndDialog(hDlg, LOWORD(wParam));
  69.                 return TRUE;
  70.             }
  71.             else
  72.             {
  73.                 SendDlgItemMessage( hDlg, IDC_PROGRESS, PBM_SETPOS, (ui32)percent , 0);            
  74.             }
  75.             break;
  76.         case WM_INITDIALOG:
  77.       {
  78.         char szTemp[1024];
  79.         sprintf(szTemp, "Writing audio to %s", pFile);
  80.       
  81.           SetDlgItemText(hDlg, IDC_STATUS,szTemp );
  82.                 SendDlgItemMessage( hDlg, IDC_PROGRESS, PBM_SETPOS, 0 , 0);    
  83.                 worker_thread_is_finished = false;
  84.                 end_conversion_requested  = false;
  85.                 // Create running thread that will perform the conversio
  86.                 DWORD threadID;
  87.                 myThread = CreateThread(NULL, 0, WorkerThread, NULL, 0, &threadID);
  88.                 if(!myThread)
  89.                    EndDialog(hDlg, LOWORD(wParam));
  90.  
  91.                 timer= SetTimer(hDlg,       // handle of window for timer messages
  92.                                         1,          // timer identifier
  93.                                         100,        // time-out value
  94.                                         NULL        // address of timer procedure
  95.                                         );
  96.       }
  97.                 return TRUE;
  98.  
  99.         case WM_COMMAND:
  100.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  101.             {
  102.                 end_conversion_requested = 1;
  103.                 return TRUE;
  104.             }
  105.             break;
  106.     }
  107.     return FALSE;
  108. }
  109.  
  110. void ExportAudio(void)
  111. {
  112.     // Create wave file
  113.  
  114.     wavFile = new WaveFile();
  115.  
  116.     if(!rs.audio)
  117.         return;
  118.  
  119.  
  120.     strcpy( pFile, rs.conf.audioOutFile );
  121.     strcat( pFile, ".wav" );
  122.  
  123.     // hrm: check for FreqSameAsInput toggle
  124.     int samplefreq;
  125.     if (rs.prof.sampleFreqSameAsInput)
  126.         samplefreq = rs.audio->sampleRate;
  127.     else
  128.         samplefreq = rs.prof.outSampleFrequency;
  129.  
  130.     if( wavFile->OpenWrite(pFile) )
  131.     {
  132.        wavFile->SetFormatType(1);                          // Format is PCM
  133.        wavFile->SetNumChannels(2);                         // Stereo
  134.        wavFile->SetBytesPerSample(4);                      // 2*16 bits
  135.        wavFile->SetBitsPerChannel(16);                     // 16 bits
  136.        wavFile->SetSampleRate(samplefreq); // out sample freq
  137.        wavFile->SetBytesPerSecond( samplefreq * 2 * 2 );
  138.  
  139.        rs.audio->SetStreamPos(rs.startFilePos);
  140.        
  141.        if( rs.audio->Start( samplefreq, DO_AUDIO, &rs.prof.sAudioProperties ) )
  142.             // Now, create modeless dialog box
  143.             DialogBox( rs.hInst, MAKEINTRESOURCE(IDD_EXPORT_AUDIO), rs.hMainWnd, (DLGPROC)DlgExporting );
  144.      }
  145. }
  146.