AudioFormat_MOD.cpp
Go to the documentation of this file.00001 //*** AudioFormat_MOD.cpp *** 00002 00003 #include "AudioFormat_MOD.h" 00004 #include "Debug.h" 00005 #include "Asset.h" 00006 #include "StandardLibrary.h" 00007 00008 #include "modplug/modplug.h" 00009 00010 00011 //*** Register *** 00012 00013 void AudioFormat_MOD::Register() 00014 { 00015 AudioFormat::RegisterAudioFormat(TestAsset,Create); 00016 } 00017 00018 00019 //*** Create *** 00020 00021 AudioFormat* AudioFormat_MOD::Create(const Asset& asset) 00022 { 00023 return new AudioFormat_MOD(asset); 00024 } 00025 00026 00027 //*** TestAsset *** 00028 00029 bool AudioFormat_MOD::TestAsset(const Asset& asset) 00030 { 00031 if (asset.Open()) 00032 { 00033 int size=asset.GetSize(); 00034 unsigned char* buffer=static_cast<unsigned char*>(Malloc(size)); 00035 asset.Open(); 00036 asset.Read(buffer,size); 00037 asset.Close(); 00038 00039 ModPlugFile* mod=ModPlug_Load(buffer,size); 00040 Free(buffer); 00041 if (mod) 00042 { 00043 ModPlug_Unload(mod); 00044 return true; 00045 } 00046 } 00047 00048 return false; 00049 } 00050 00051 00052 00053 //*** Constructor *** 00054 00055 AudioFormat_MOD::AudioFormat_MOD(const Asset& asset): 00056 modFile_(0), 00057 position_(0) 00058 { 00059 if (asset.Open()) 00060 { 00061 int size=asset.GetSize(); 00062 unsigned char* buffer=static_cast<unsigned char*>(Malloc(size)); 00063 asset.Read(buffer,size); 00064 asset.Close(); 00065 modFile_=ModPlug_Load(buffer,size); 00066 Free(buffer); 00067 } 00068 // Report missing file 00069 #ifdef _DEBUG 00070 else 00071 { 00072 const char* filename=asset.GetFilename().GetString(); 00073 if (filename) 00074 { 00075 char errorMessage[1024]; 00076 SNPrintF(errorMessage,1024,"File not found: %s",filename); 00077 Assert(false,errorMessage); 00078 } 00079 else 00080 { 00081 Assert(false,"An asset could not be accessed."); 00082 } 00083 } 00084 #endif 00085 } 00086 00087 00088 //*** Destructor *** 00089 00090 AudioFormat_MOD::~AudioFormat_MOD() 00091 { 00092 if (modFile_) 00093 { 00094 ModPlug_Unload(modFile_); 00095 } 00096 } 00097 00098 00099 //*** GetChannels *** 00100 00101 int AudioFormat_MOD::GetChannels() 00102 { 00103 return 2; 00104 } 00105 00106 00107 //*** GetFrequency *** 00108 00109 int AudioFormat_MOD::GetFrequency() 00110 { 00111 return 44100; 00112 } 00113 00114 00115 //*** GetBitsPerSample *** 00116 00117 int AudioFormat_MOD::GetBitsPerSample() 00118 { 00119 return 16; 00120 } 00121 00122 00123 //*** GetSize *** 00124 00125 int AudioFormat_MOD::GetSize() 00126 { 00127 if (!modFile_) 00128 { 00129 return 0; 00130 } 00131 return (int)((ModPlug_GetLength(modFile_)/1000.0f)*GetChannels()*(GetBitsPerSample()/8)*GetFrequency()); 00132 } 00133 00134 00135 //*** GetPosition *** 00136 00137 int AudioFormat_MOD::GetPosition() 00138 { 00139 return position_; 00140 } 00141 00142 00143 //*** SetPosition *** 00144 00145 void AudioFormat_MOD::SetPosition(int position) 00146 { 00147 position_=position; 00148 if (modFile_) 00149 { 00150 float sampleSize=(float)(GetChannels()*GetFrequency()*(GetBitsPerSample()/8)); 00151 float time=position/sampleSize; 00152 ModPlug_Seek(modFile_,(int)(time*1000.0f)); 00153 } 00154 } 00155 00156 00157 //*** CopySoundChunk *** 00158 00159 int AudioFormat_MOD::CopySoundChunk(void* buffer,int bytes) 00160 { 00161 if (!modFile_) 00162 { 00163 return 0; 00164 } 00165 00166 int read=ModPlug_Read(modFile_,buffer,bytes); 00167 position_+=read; 00168 return read; 00169 } 00170 00171
Reproduction/republishing of any material on this site without permission is strictly prohibited.
