home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / WavWriter.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  3.0 KB  |  118 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. // WavWriter.cpp: implementation of the WavWriter class.
  20. //
  21. //////////////////////////////////////////////////////////////////////
  22.  
  23. #include "stdafx.h"
  24. #include "vba.h"
  25. #include "WavWriter.h"
  26.  
  27. #include "../System.h"
  28. #include "../Util.h"
  29.  
  30. #ifdef _DEBUG
  31. #undef THIS_FILE
  32. static char THIS_FILE[]=__FILE__;
  33. #define new DEBUG_NEW
  34. #endif
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. WavWriter::WavWriter()
  41. {
  42.   m_file = NULL;
  43.   m_len = 0;
  44.   m_posSize = 0;
  45. }
  46.  
  47. WavWriter::~WavWriter()
  48. {
  49.   if(m_file)
  50.     Close();
  51. }
  52.  
  53. void WavWriter::Close()
  54. {
  55.   // calculate the total file length
  56.   u32 len = ftell(m_file)-8;
  57.   fseek(m_file, 4, SEEK_SET);
  58.   u8 data[4];
  59.   utilPutDword(data, len);
  60.   fwrite(data, 1, 4, m_file);
  61.   // write out the size of the data section
  62.   fseek(m_file, m_posSize, SEEK_SET);
  63.   utilPutDword(data, m_len);
  64.   fwrite(data, 1, 4, m_file);
  65.   fclose(m_file);
  66.   m_file = NULL;
  67. }
  68.  
  69. bool WavWriter::Open(const char *name)
  70. {
  71.   if(m_file)
  72.     Close();
  73.   m_file = fopen(name, "wb");
  74.  
  75.   if(!m_file)
  76.     return false;
  77.   // RIFF header
  78.   u8 data[4] = { 'R', 'I', 'F', 'F' };
  79.   fwrite(data, 1, 4, m_file);
  80.   utilPutDword(data, 0);
  81.   // write 0 for now. Will get filled during close
  82.   fwrite(data, 1, 4, m_file);
  83.   // write WAVE header
  84.   u8 data2[4] = { 'W', 'A', 'V', 'E' };
  85.   fwrite(data2, 1, 4, m_file);
  86.   return true;
  87. }
  88.  
  89. void WavWriter::SetFormat(const WAVEFORMATEX *format)
  90. {
  91.   if(m_file == NULL)
  92.     return;
  93.   // write fmt header
  94.   u8 data[4] = { 'f', 'm', 't', ' ' };
  95.   fwrite(data, 1, 4, m_file);
  96.   u32 value = sizeof(WAVEFORMATEX);
  97.   utilPutDword(data, value);
  98.   fwrite(data, 1, 4, m_file);
  99.   fwrite(format, 1, sizeof(WAVEFORMATEX), m_file);
  100.   // start data header
  101.   u8 data2[4] = { 'd', 'a', 't', 'a' };
  102.   fwrite(data2, 1, 4, m_file);
  103.   
  104.   m_posSize = ftell(m_file);
  105.   // write 0 for data chunk size. Filled out during Close()
  106.   utilPutDword(data, 0);
  107.   fwrite(data, 1, 4, m_file);
  108. }
  109.  
  110. void WavWriter::AddSound(const u8 *data, int len)
  111. {
  112.   if(m_file == NULL)
  113.     return;
  114.   // write a block of sound data
  115.   fwrite(data, 1, len, m_file);
  116.   m_len += len;
  117. }
  118.