/* * WaveSample Class * * Allows an 8 bit or 16 bit monoaural wave (.WAV) file to be loaded and played * * Including automatic L-R positioning effects * * Copyright (C) 2008 Alejandro Valenzuela Roca, * * http://mexinetica.com/~lanjoe9 * * This file is part of MotorJ, a free framework for videogame development. * * * * MotorJ is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * MotorJ is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with MotorJ. If not, see . */ #include "wavesample-class.h" #ifdef PC_SDL mjWaveSample::mjWaveSample() { cMixC = NULL; } mjWaveSample::~mjWaveSample() { Mix_FreeChunk(cMixC); } int mjWaveSample::LoadFile(const char * filename) { cMixC = Mix_LoadWAV(filename); if (cMixC) return 0; else return -1; } void mjWaveSample::Play(unsigned vol, unsigned balance) { if (cMixC) { Mix_VolumeChunk(cMixC, vol); channel = Mix_PlayChannel(-1, cMixC, 0); Mix_SetPanning(channel, 256 - balance, balance); } } void mjWaveSample::Play3D(vector3 &observer_pos, vector3 &observer_dirleft, vector3 &soundsource_pos, float attenuation, unsigned maxvol) { vector3 soundsource_dir; float vol_dist; unsigned pan; vol_dist = (1.0-(dist_p(observer_pos, soundsource_pos)*attenuation)); if (vol_dist > 0) { vec_copy(soundsource_pos, &soundsource_dir); vec_sum(-1, observer_pos, &soundsource_dir); pan = 255*(vec_ang(observer_dirleft, soundsource_dir)/3.141592); Mix_VolumeChunk(cMixC, vol_dist*127); channel = Mix_PlayChannel(-1, cMixC, 0); Mix_SetPanning(channel, 256 - pan, pan); } } #endif #ifdef NINTENDO_WII mjWaveSample::mjWaveSample() { } mjWaveSample::~mjWaveSample() { } int mjWaveSample::LoadFile(const char * filename) { return -1; } void mjWaveSample::Play(unsigned vol, unsigned balance) { } void mjWaveSample::Play3D(vector3 &observer_pos, vector3 &observer_dirleft, vector3 &soundsource_pos, float attenuation, unsigned maxvol) { } #endif #ifdef NINTENDO_DS /* * Nintendo DS implementation */ mjWaveSample::mjWaveSample() { wavebuffer = NULL; } mjWaveSample::~mjWaveSample() { if (wavebuffer == NULL) delete wavebuffer; //tsd.data = NULL; } int mjWaveSample::LoadFile(const char * filename) { char buffer[] = "1234"; unsigned long chunksize; unsigned short compression_code; unsigned short n_chans; unsigned long sample_rate; unsigned long bytes_per_sec; bool is_8_bit; wavefile = fopen(filename, "r"); if (wavefile) { // Clear memory if needed if (wavebuffer) delete wavebuffer; // Read RIFF header fread(buffer, 4, 1, wavefile); if (strcmp(buffer, "RIFF")) return -2 ; // Not RIFF // Read chunk size (should we care?) fread(buffer, 4, 1, wavefile); // Read WAVE chunk header fread(buffer, 4, 1, wavefile); if (strcmp(buffer, "WAVE")) return -3; // Not WAVE // Read fmt chunk header fread(buffer, 4, 1, wavefile); if (strcmp(buffer, "fmt ")) return -4; // Not "fmt "? bah! not supported! fread(&chunksize, 4, 1, wavefile); fread(&compression_code, 2, 1, wavefile); if (compression_code != 1) return -99; fread(&n_chans, 2, 1, wavefile); if (n_chans != 1) return -98; fread(&sample_rate, 4, 1, wavefile); fread(&bytes_per_sec, 4, 1, wavefile); fread(buffer, 4, 1, wavefile); // Block align & significant // bits per sample fseek(wavefile, chunksize-16, SEEK_CUR); // Extra format bytes fread(buffer, 4, 1, wavefile); while(strcmp(buffer,"data")) // Jump over misc chunks { fread(&chunksize, 4, 1, wavefile); fseek(wavefile, chunksize, SEEK_CUR); fread(buffer, 4, 1, wavefile); } if (strcmp(buffer, "data")) return -99; fread(&chunksize, 4, 1, wavefile); wavebuffer = new char[chunksize+1]; // if the sound is 8 bit, bytes_per_sec will be exactly equal to // sample_rate is_8_bit = (sample_rate/bytes_per_sec); // Ok so now we're at the beginning of the wave data fread(wavebuffer,chunksize,1,wavefile); tsd.rate = sample_rate; tsd.len = chunksize; tsd.pan = 64; tsd.format = is_8_bit; tsd.data = wavebuffer; fclose(wavefile); return 0; } return -1; } void mjWaveSample::Play(unsigned vol, unsigned balance) { tsd.pan = balance; tsd.vol = vol; //playSound(&tsd); } void mjWaveSample::Play3D(vector3 &observer_pos, vector3 &observer_dirleft, vector3 &soundsource_pos, float attenuation, unsigned maxvol) { vector3 soundsource_dir; float vol_dist; vol_dist = (1.0-(dist_p(observer_pos, soundsource_pos)*attenuation)); if (vol_dist > 0) { vec_copy(soundsource_pos, &soundsource_dir); vec_sum(-1, observer_pos, &soundsource_dir); tsd.vol = vol_dist * maxvol; tsd.pan = 128*(vec_ang(observer_dirleft, soundsource_dir)/3.141592); //playSound(&tsd); } } #endif