PolySound.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "polycode/core/PolySound.h"
  20. #define OV_EXCLUDE_STATIC_CALLBACKS
  21. #include <vorbis/vorbisfile.h>
  22. #undef OV_EXCLUDE_STATIC_CALLBACKS
  23. #include "polycode/core/PolyString.h"
  24. #include "polycode/core/PolyLogger.h"
  25. #include "polycode/core/PolySoundManager.h"
  26. #include "polycode/core/PolyCore.h"
  27. #include "polycode/core/PolyCoreServices.h"
  28. #include <string>
  29. #include <vector>
  30. #include <stdint.h>
  31. #include <limits>
  32. #ifndef MAX_FLOAT
  33. #define MAX_FLOAT (std::numeric_limits<double>::infinity())
  34. #endif
  35. #ifndef INT32_MAX
  36. #define INT32_MAX (std::numeric_limits<int32_t>::max())
  37. #endif
  38. using namespace std;
  39. using namespace Polycode;
  40. AudioStreamingSource::AudioStreamingSource(unsigned int channels, unsigned int bps, unsigned int freq) : channels(channels), freq(freq), bps(bps) {
  41. }
  42. unsigned int AudioStreamingSource::getNumChannels() {
  43. return channels;
  44. }
  45. unsigned int AudioStreamingSource::getBitsPerSample() {
  46. return bps;
  47. }
  48. unsigned int AudioStreamingSource::getFrequency() {
  49. return freq;
  50. }
  51. unsigned int AudioStreamingSource::streamData(char *buffer, unsigned int size) {
  52. return 0;
  53. }
  54. size_t custom_readfunc(void *ptr, size_t size, size_t nmemb, void *datasource) {
  55. Polycode::CoreFile *file = (Polycode::CoreFile*) datasource;
  56. return file->read(ptr, size, nmemb);
  57. }
  58. int custom_seekfunc(void *datasource, ogg_int64_t offset, int whence){
  59. Polycode::CoreFile *file = (Polycode::CoreFile*) datasource;
  60. return file->seek(offset, whence);
  61. }
  62. int custom_closefunc(void *datasource) {
  63. Polycode::CoreFile *file = (Polycode::CoreFile*) datasource;
  64. Services()->getCore()->closeFile(file);
  65. return 0;
  66. }
  67. long custom_tellfunc(void *datasource) {
  68. CoreFile *file = (CoreFile*) datasource;
  69. return file->tell();
  70. }
  71. Sound::Sound(const String& fileName, bool generateFloatBuffer) : referenceDistance(1), maxDistance(MAX_FLOAT), pitch(1), volume(1), sampleLength(-1), streamingSound(false) {
  72. soundLoaded = false;
  73. loadFile(fileName, generateFloatBuffer);
  74. setIsPositional(false);
  75. }
  76. Sound::Sound(int size, const char *data, int channels, unsigned int freq, int bps, bool generateFloatBuffer) : referenceDistance(1), maxDistance(MAX_FLOAT), pitch(1), volume(1), sampleLength(-1), streamingSound(false) {
  77. //buffer = loadBytes(data, size, freq, channels, bps, generateFloatBuffer);
  78. //soundSource = GenSource(buffer);
  79. setIsPositional(false);
  80. reloadProperties();
  81. soundLoaded = true;
  82. }
  83. Sound::Sound(AudioStreamingSource *streamingSource) : referenceDistance(1), maxDistance(MAX_FLOAT), pitch(1), volume(1), sampleLength(-1), streamingSound(true), streamingSource(streamingSource) {
  84. /*
  85. alGenSources(1, &soundSource);
  86. alSourcef(soundSource, AL_PITCH, 1.0);
  87. alSourcef(soundSource, AL_GAIN, 1.0);
  88. ALfloat sourcePos[] = {0.0, 0.0, 0.0};
  89. ALfloat sourceVel[] = {0.0, 0.0, 0.0};
  90. alSourcefv(soundSource, AL_POSITION, sourcePos);
  91. alSourcefv(soundSource, AL_VELOCITY, sourceVel);
  92. alGenBuffers(STREAMING_BUFFER_COUNT, streamingBuffers);
  93. for(int i=0; i < STREAMING_BUFFER_COUNT; i++) {
  94. if(updateALBuffer(streamingBuffers[i])) {
  95. alSourceQueueBuffers(soundSource, 1, &streamingBuffers[i]);
  96. }
  97. }
  98. Services()->getSoundManager()->registerStreamingSound(this);
  99. alSourcePlay(soundSource);
  100. */
  101. // NOAL_TODO
  102. }
  103. void Sound::updateStream() {
  104. /*
  105. ALint processed = 0;
  106. alGetSourcei(soundSource, AL_BUFFERS_PROCESSED, &processed);
  107. while(processed--) {
  108. ALuint buffer;
  109. alSourceUnqueueBuffers(soundSource, 1, &buffer);
  110. if(updateALBuffer(buffer)) {
  111. alSourceQueueBuffers(soundSource, 1, &buffer);
  112. }
  113. }
  114. ALenum state;
  115. alGetSourcei(soundSource, AL_SOURCE_STATE, &state);
  116. if(state != AL_PLAYING) {
  117. alSourcePlay(soundSource);
  118. }
  119. */
  120. //NOAL_TODO
  121. }
  122. bool Sound::updateALBuffer(unsigned int buffer) {
  123. /*
  124. char data[STREAMING_BUFFER_SIZE];
  125. unsigned int bytesStreamed = streamingSource->streamData(data, STREAMING_BUFFER_SIZE);
  126. if(bytesStreamed == 0) {
  127. return false;
  128. }
  129. ALenum format;
  130. if (streamingSource->getNumChannels() == 1) {
  131. format = (streamingSource->getBitsPerSample() == 8) ? AL_FORMAT_MONO8 : AL_FORMAT_MONO16;
  132. } else {
  133. format = (streamingSource->getBitsPerSample() == 8) ? AL_FORMAT_STEREO8 : AL_FORMAT_STEREO16;
  134. }
  135. alBufferData(buffer, format, data, bytesStreamed, streamingSource->getFrequency());
  136. */
  137. // NOAL_TODO
  138. return true;
  139. }
  140. void Sound::loadFile(String fileName, bool generateFloatBuffer) {
  141. /*
  142. if(soundLoaded) {
  143. alDeleteSources(1,&soundSource);
  144. }
  145. String actualFilename = fileName;
  146. CoreFile *test = Services()->getCore()->openFile(fileName, "rb");
  147. if(!test) {
  148. actualFilename = "default/default.wav";
  149. } else {
  150. Services()->getCore()->closeFile(test);
  151. }
  152. String extension;
  153. size_t found;
  154. found=actualFilename.rfind(".");
  155. if (found!=string::npos) {
  156. extension = actualFilename.substr(found+1);
  157. } else {
  158. extension = "";
  159. }
  160. if(extension == "wav" || extension == "WAV") {
  161. buffer = loadWAV(actualFilename, generateFloatBuffer);
  162. } else if(extension == "ogg" || extension == "OGG") {
  163. buffer = loadOGG(actualFilename, generateFloatBuffer);
  164. }
  165. this->fileName = actualFilename;
  166. soundSource = GenSource(buffer);
  167. reloadProperties();
  168. soundLoaded = true;
  169. checkALError("Sound load: complete");
  170. */
  171. //NOAL_TODO
  172. }
  173. void Sound::reloadProperties() { // Re-set stored properties into sound source.
  174. setVolume(volume);
  175. setPitch(pitch);
  176. setReferenceDistance(referenceDistance);
  177. setMaxDistance(maxDistance);
  178. }
  179. String Sound::getFileName() {
  180. return fileName;
  181. }
  182. Number Sound::getVolume() {
  183. return volume;
  184. }
  185. Number Sound::getPitch() {
  186. return pitch;
  187. }
  188. Sound::~Sound() {
  189. /*
  190. alSourcei(soundSource, AL_BUFFER, 0);
  191. alDeleteSources(1,&soundSource);
  192. checkALError("Destroying sound");
  193. alDeleteBuffers(1, &buffer);
  194. checkALError("Deleting buffer");
  195. if(streamingSound) {
  196. alDeleteBuffers(STREAMING_BUFFER_COUNT, streamingBuffers);
  197. Services()->getSoundManager()->unregisterStreamingSound(this);
  198. }
  199. */
  200. //NOAL_TODO
  201. }
  202. void Sound::soundCheck(bool result, const String& err) {
  203. if(!result)
  204. soundError(err);
  205. }
  206. void Sound::soundError(const String& err) {
  207. Logger::log("SOUND ERROR: %s\n", err.c_str());
  208. }
  209. unsigned long Sound::readByte32(const unsigned char data[4]) {
  210. #if TAU_BIG_ENDIAN
  211. return (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
  212. #else
  213. return (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
  214. #endif
  215. }
  216. unsigned short Sound::readByte16(const unsigned char data[2]) {
  217. #if TAU_BIG_ENDIAN
  218. return (data[0] << 8) + data[1];
  219. #else
  220. return (data[1] << 8) + data[0];
  221. #endif
  222. }
  223. void Sound::Play(bool loop) {
  224. /*
  225. if(!loop) {
  226. alSourcei(soundSource, AL_LOOPING, AL_FALSE);
  227. } else {
  228. alSourcei(soundSource, AL_LOOPING, AL_TRUE);
  229. }
  230. checkALError("Play: loop");
  231. alSourcePlay(soundSource);
  232. checkALError("Play: play");
  233. */
  234. //NOAL_TODO
  235. }
  236. bool Sound::isPlaying() {
  237. /*
  238. ALenum state;
  239. alGetSourcei(soundSource, AL_SOURCE_STATE, &state);
  240. return (state == AL_PLAYING);
  241. */
  242. //NOAL_TODO
  243. return false;
  244. }
  245. void Sound::setVolume(Number newVolume) {
  246. this->volume = newVolume;
  247. /*
  248. alSourcef(soundSource, AL_GAIN, newVolume);
  249. checkALError("Set volume");
  250. */
  251. //NOAL_TODO
  252. }
  253. void Sound::setPitch(Number newPitch) {
  254. this->pitch = newPitch;
  255. /*
  256. alSourcef(soundSource, AL_PITCH, newPitch);
  257. checkALError("Set pitch");
  258. */
  259. //NOAL_TODO
  260. }
  261. void Sound::setSoundPosition(Vector3 position) {
  262. /*
  263. if(isPositional)
  264. alSource3f(soundSource,AL_POSITION, position.x, position.y, position.z);
  265. checkALError("Set sound position");
  266. */
  267. //NOAL_TODO
  268. }
  269. void Sound::setSoundVelocity(Vector3 velocity) {
  270. /*
  271. if(isPositional)
  272. alSource3f(soundSource,AL_VELOCITY, velocity.x, velocity.y, velocity.z);
  273. checkALError("Set sound velocity");
  274. */
  275. //NOAL_TODO
  276. }
  277. void Sound::setSoundDirection(Vector3 direction) {
  278. /*
  279. if(isPositional)
  280. alSource3f(soundSource,AL_DIRECTION, direction.x, direction.y, direction.z);
  281. checkALError("Set sound direction");
  282. */
  283. //NOAL_TODO
  284. }
  285. void Sound::setOffset(int off) {
  286. /*
  287. alSourcei(soundSource, AL_SAMPLE_OFFSET, off);
  288. */
  289. //NOAL_TODO
  290. }
  291. Number Sound::getPlaybackTime() {
  292. /*
  293. float result = 0.0;
  294. alGetSourcef(soundSource, AL_SEC_OFFSET, &result);
  295. return result;
  296. */
  297. //NOAL_TODO
  298. return 0.0;
  299. }
  300. Number Sound::getPlaybackDuration() {
  301. /*
  302. ALint sizeInBytes;
  303. ALint channels;
  304. ALint bits;
  305. ALint bufferID;
  306. alGetSourcei(soundSource, AL_BUFFER, &bufferID);
  307. alGetBufferi(bufferID, AL_SIZE, &sizeInBytes);
  308. alGetBufferi(bufferID, AL_CHANNELS, &channels);
  309. alGetBufferi(bufferID, AL_BITS, &bits);
  310. int lengthInSamples = sizeInBytes * 8 / (channels * bits);
  311. ALint frequency;
  312. alGetBufferi(bufferID, AL_FREQUENCY, &frequency);
  313. Number durationInSeconds = (float)lengthInSamples / (float)frequency;
  314. return durationInSeconds;
  315. */
  316. //NOAL_TODO
  317. return 0.0;
  318. }
  319. int Sound::getOffset() {
  320. /*
  321. ALint off = -1;
  322. alGetSourcei(soundSource, AL_SAMPLE_OFFSET, &off);
  323. return off;
  324. */
  325. //NOAL_TODO
  326. return 0;
  327. }
  328. void Sound::seekTo(Number time) {
  329. /*
  330. if(time > getPlaybackDuration())
  331. return;
  332. alSourcef(soundSource, AL_SEC_OFFSET, time);
  333. checkALError("Seek");
  334. */
  335. //NOAL_TODO
  336. }
  337. int Sound::getSampleLength() {
  338. return sampleLength;
  339. }
  340. void Sound::setPositionalProperties(Number referenceDistance, Number maxDistance) {
  341. setReferenceDistance(referenceDistance);
  342. setMaxDistance(maxDistance);
  343. }
  344. void Sound::setReferenceDistance(Number referenceDistance) {
  345. this->referenceDistance = referenceDistance;
  346. //alSourcef(soundSource, AL_REFERENCE_DISTANCE, referenceDistance);
  347. //checkALError("Set reference distance");
  348. //NOAL_TODO
  349. }
  350. void Sound::setMaxDistance(Number maxDistance) {
  351. this->maxDistance = maxDistance;
  352. //alSourcef(soundSource,AL_MAX_DISTANCE, maxDistance);
  353. // checkALError("Set max distance");
  354. //NOAL_TODO
  355. }
  356. Number Sound::getReferenceDistance() {
  357. return referenceDistance;
  358. }
  359. Number Sound::getMaxDistance() {
  360. return maxDistance;
  361. }
  362. void Sound::setIsPositional(bool isPositional) {
  363. /*
  364. this->isPositional = isPositional;
  365. if(isPositional) {
  366. alSourcei(soundSource, AL_SOURCE_RELATIVE, AL_FALSE);
  367. } else {
  368. alSourcei(soundSource, AL_SOURCE_RELATIVE, AL_TRUE);
  369. alSource3f(soundSource,AL_POSITION, 0,0,0);
  370. alSource3f(soundSource,AL_VELOCITY, 0,0,0);
  371. alSource3f(soundSource,AL_DIRECTION, 0,0,0);
  372. }
  373. checkALError("Set is-positional");
  374. */
  375. //NOAL_TODO
  376. }
  377. void Sound::Stop() {
  378. //NOAL_TODO
  379. }
  380. std::vector<float> *Sound::getFloatBuffer() {
  381. return &floatBuffer;
  382. }
  383. /*
  384. ALuint Sound::loadBytes(const char *data, int size, int freq, int channels, int bps, bool generateFloatBuffer) {
  385. /*
  386. ALenum format;
  387. if (channels == 1)
  388. format = (bps == 8) ? AL_FORMAT_MONO8 : AL_FORMAT_MONO16;
  389. else
  390. format = (bps == 8) ? AL_FORMAT_STEREO8 : AL_FORMAT_STEREO16;
  391. sampleLength = bps > 8 ? size / (bps/8) : -1;
  392. checkALError("LoadBytes: pre-generate buffer");
  393. alGenBuffers(1, &buffer);
  394. checkALError("LoadBytes: generate buffer");
  395. soundCheck(AL_NONE != buffer, "LoadBytes: Did not generate buffer");
  396. alBufferData(buffer, format, data, size, freq);
  397. checkALError("LoadBytes: load buffer data");
  398. if(generateFloatBuffer) {
  399. int32_t *ptr32 = (int32_t*) &data[0];
  400. for(int i=0; i < size/4; i++ ) {
  401. floatBuffer.push_back(((Number)ptr32[i])/((Number)INT32_MAX));
  402. }
  403. }
  404. return buffer;
  405. }
  406. ALuint Sound::loadOGG(const String& fileName, bool generateFloatBuffer) {
  407. // floatBuffer.clear();
  408. vector<char> data;
  409. alGenBuffers(1, &buffer);
  410. int endian = 0; // 0 for Little-Endian, 1 for Big-Endian
  411. int bitStream;
  412. long bytes;
  413. char array[BUFFER_SIZE]; // Local fixed size array
  414. CoreFile *f;
  415. ALenum format;
  416. ALsizei freq;
  417. // Open for binary reading
  418. f = Services()->getCore()->openFile(fileName.c_str(), "rb");
  419. if(!f) {
  420. soundError("Error loading OGG file!\n");
  421. return buffer;
  422. }
  423. vorbis_info *pInfo;
  424. OggVorbis_File oggFile;
  425. ov_callbacks callbacks;
  426. callbacks.read_func = custom_readfunc;
  427. callbacks.seek_func = custom_seekfunc;
  428. callbacks.close_func = custom_closefunc;
  429. callbacks.tell_func = custom_tellfunc;
  430. ov_open_callbacks( (void*)f, &oggFile, NULL, 0, callbacks);
  431. // ov_open(f, &oggFile, NULL, 0);
  432. // Get some information about the OGG file
  433. pInfo = ov_info(&oggFile, -1);
  434. // Check the number of channels... always use 16-bit samples
  435. if (pInfo->channels == 1)
  436. format = AL_FORMAT_MONO16;
  437. else
  438. format = AL_FORMAT_STEREO16;
  439. // end if
  440. // The frequency of the sampling rate
  441. freq = pInfo->rate;
  442. do {
  443. // Read up to a buffer's worth of decoded sound data
  444. bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);
  445. // Append to end of buffer
  446. data.insert(data.end(), array, array + bytes);
  447. } while (bytes > 0);
  448. ov_clear(&oggFile);
  449. sampleLength = data.size() / sizeof(unsigned short);
  450. alBufferData(buffer, format, &data[0], static_cast<ALsizei>(data.size()), freq);
  451. if(generateFloatBuffer) {
  452. int32_t *ptr32 = (int32_t*) &data[0];
  453. for(int i=0; i < data.size()/4; i++ ) {
  454. floatBuffer.push_back(((Number)ptr32[i])/((Number)INT32_MAX));
  455. }
  456. }
  457. return buffer;
  458. }
  459. ALuint Sound::loadWAV(const String& fileName, bool generateFloatBuffer) {
  460. long bytes;
  461. vector <char> data;
  462. ALsizei freq;
  463. // Local resources
  464. CoreFile *f = NULL;
  465. char *array = NULL;
  466. checkALError("loadWAV: pre-generate buffer");
  467. // Open for binary reading
  468. f = Services()->getCore()->openFile(fileName.c_str(), "rb");
  469. if (!f) {
  470. soundError("LoadWav: Could not load wav from " + fileName);
  471. return buffer;
  472. }
  473. // buffers
  474. char magic[5];
  475. magic[4] = '\0';
  476. unsigned char data32[4];
  477. unsigned char data16[2];
  478. // check magic
  479. soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  480. soundCheck(String(magic) == "RIFF", "LoadWav: Wrong wav file format. This file is not a .wav file (no RIFF magic): "+ fileName );
  481. // skip 4 bytes (file size)
  482. f->seek(4,SEEK_CUR);
  483. // check file format
  484. soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  485. soundCheck(String(magic) == "WAVE", "LoadWav: Wrong wav file format. This file is not a .wav file (no WAVE format): "+ fileName );
  486. // check 'fmt ' sub chunk (1)
  487. soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  488. soundCheck(String(magic) == "fmt ", "LoadWav: Wrong wav file format. This file is not a .wav file (no 'fmt ' subchunk): "+ fileName );
  489. // read (1)'s size
  490. soundCheck(f->read(data32,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  491. unsigned long subChunk1Size = readByte32(data32);
  492. soundCheck(subChunk1Size >= 16, "Wrong wav file format. This file is not a .wav file ('fmt ' chunk too small, truncated file?): "+ fileName );
  493. // check PCM audio format
  494. soundCheck(f->read(data16,2,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  495. unsigned short audioFormat = readByte16(data16);
  496. soundCheck(audioFormat == 1, "LoadWav: Wrong wav file format. This file is not a .wav file (audio format is not PCM): "+ fileName );
  497. // read number of channels
  498. soundCheck(f->read(data16,2,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  499. unsigned short channels = readByte16(data16);
  500. // read frequency (sample rate)
  501. soundCheck(f->read(data32,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  502. unsigned long frequency = readByte32(data32);
  503. // skip 6 bytes (Byte rate (4), Block align (2))
  504. f->seek(6,SEEK_CUR);
  505. // read bits per sample
  506. soundCheck(f->read(data16,2,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  507. unsigned short bps = readByte16(data16);
  508. // check 'data' sub chunk (2)
  509. soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  510. soundCheck(String(magic) == "data", "LoadWav: Wrong wav file format. This file is not a .wav file (no data subchunk): "+ fileName );
  511. soundCheck(f->read(data32,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
  512. unsigned long subChunk2Size = readByte32(data32);
  513. // The frequency of the sampling rate
  514. freq = frequency;
  515. soundCheck(sizeof(freq) == sizeof(frequency), "LoadWav: freq and frequency different sizes");
  516. array = new char[BUFFER_SIZE];
  517. while (data.size() != subChunk2Size) {
  518. // Read up to a buffer's worth of decoded sound data
  519. bytes = f->read(array, 1, BUFFER_SIZE);
  520. if (bytes <= 0)
  521. break;
  522. if (data.size() + bytes > subChunk2Size)
  523. bytes = subChunk2Size - data.size();
  524. // Append to end of buffer
  525. data.insert(data.end(), array, array + bytes);
  526. };
  527. delete []array;
  528. array = NULL;
  529. Services()->getCore()->closeFile(f);
  530. f = NULL;
  531. return loadBytes(&data[0], data.size(), freq, channels, bps, generateFloatBuffer);
  532. // if (buffer)
  533. // if (alIsBuffer(buffer) == AL_TRUE)
  534. // alDeleteBuffers(1, &buffer);
  535. //
  536. // if (array)
  537. // delete []array;
  538. //
  539. // if (f)
  540. // OSBasics::close(f);
  541. //
  542. // throw (e);
  543. }
  544. */
  545. //NOAL_TODO