- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
CSound.cpp
资源名称:brm2.rar [点击查看]
上传用户:liujun12jf
上传日期:2022-07-12
资源大小:638k
文件大小:4k
源码类别:
OpenGL
开发平台:
Visual C++
- #include "CSound.h"
- // brm!
- FMOD_VECTOR f2fmodv(float *pf)
- {
- FMOD_VECTOR fv;
- fv.x = pf[0];
- fv.y = pf[1];
- fv.z = pf[2];
- return fv;
- }
- void CSound::Frame()
- {
- UpdateListener();
- UpdatePositions();
- system->update();
- RemoveStoppedSounds();
- }
- void CSound::SetListener(float *pPos)
- {
- pListenerPos = pPos;
- }
- void CSound::UpdateListener()
- {
- if (!pListenerPos) return;
- FMOD_VECTOR fv = f2fmodv(pListenerPos);
- system->set3DListenerAttributes(0, &fv, 0, 0, 0);
- }
- void CSound::UpdateChannelPosition(FMOD::Channel *ch, float *pos)
- {
- if (!ch || !pos) return;
- FMOD_VECTOR fv = f2fmodv(pos);
- ch->set3DAttributes(&fv, 0);
- }
- void CSound::UpdatePositions()
- {
- vectorq<s_played*>::iterator i;
- for (i = vPlayed.begin() ; i != vPlayed.end() ; i++)
- {
- if (!*i) continue;
- if (!(*i)->pPosition) continue;
- UpdateChannelPosition((*i)->pChannel, &(*i)->pPosition[0]);
- }
- }
- void CSound::RemoveStoppedSounds()
- {
- vectorq<s_played*>::iterator i;
- for (i = vPlayed.begin() ; i != vPlayed.end() ; i++)
- {
- if (!*i) continue;
- bool playing, paused;
- (*i)->pChannel->isPlaying(&playing);
- paused = (*i)->bPaused;
- if (!playing && !paused) vPlayed.rm(i);
- }
- }
- bool CSound::AddSound(std::string name, std::string filename, int priority)
- {
- if (!name.length() || !filename.length()) return 0;
- if (FindSound(name)) return 0;
- s_sound *s = new s_sound;
- s->name = name;
- s->filename = filename;
- s->priority = priority;
- s->pSound = 0;
- vSounds.add(s);
- return 1;
- }
- bool CSound::LoadSound(s_sound *s)
- {
- int mode = 0;
- mode |= FMOD_3D;
- mode |= FMOD_HARDWARE;
- // mode |= FMOD_LOOP_NORMAL;
- result = system->createSound(s->filename.c_str(), mode, 0, &s->pSound);
- if (result != FMOD_OK)
- {
- errf("CSound::LoadSound - error creating sound from file", (char*)s->filename.c_str());
- return 0;
- }
- s->pSound->set3DMinMaxDistance(10, 1000);
- return 1;
- }
- void CSound::loadAndPlayLoop(std::string name)
- {
- s_sound *s = new s_sound;
- int mode = 0;
- mode |= FMOD_3D;
- mode |= FMOD_HARDWARE;
- mode |= FMOD_LOOP_NORMAL;
- result = system->createSound(name.c_str(), mode, 0, &s->pSound);
- if (result != FMOD_OK)
- {
- errf("CSound::LoadSound - error creating sound from file", (char*)s->filename.c_str());
- return;
- }
- s->pSound->set3DMinMaxDistance(50, 1000);
- system->playSound(FMOD_CHANNEL_FREE, s->pSound, 0, 0);
- }
- s_played *CSound::PlaySound(std::string name, float *pPos)
- {
- s_sound *s;
- s = FindSound(name);
- if (!s) return 0;
- if (!s->pSound) LoadSound(s);
- if (!s->pSound) return 0;
- s_played *ps = new s_played;
- if (pPos) ps->pPosition = pPos;
- else ps->pPosition = pListenerPos;
- system->playSound(FMOD_CHANNEL_FREE, s->pSound, 1, &ps->pChannel);
- UpdateChannelPosition(ps->pChannel, &ps->pPosition[0]);
- ps->pChannel->setPaused(0);
- ps->bPaused = 0;
- vPlayed.add(ps);
- return ps;
- }
- s_sound *CSound::FindSound(std::string name)
- {
- vectorq<s_sound*>::iterator i;
- for (i = vSounds.begin() ; i != vSounds.end() ; i++)
- {
- if (*i == 0) continue;
- if (! strcmp( name.c_str(), (*i)->name.c_str() ) ) return (*i);
- }
- return 0;
- }
- void CSound::SetPausedToAll(bool p)
- {
- vectorq<s_played*>::iterator i;
- for (i = vPlayed.begin() ; i != vPlayed.end() ; i++)
- {
- if (!*i) continue;
- (*i)->bPaused = p;
- (*i)->pChannel->setPaused(p);
- }
- }
- CSound::CSound()
- {
- }
- bool CSound::Init()
- {
- // Create the main system object.
- result = FMOD::System_Create(&system);
- CheckError();
- // Allow 100 software mixed voices to be audible at once.
- result = system->setSoftwareChannels(100);
- CheckError();
- // Require the soundcard to have at least 32 2D and 3D hardware voices
- // and clamp it to using 64 if it has more than this.
- // note: 3D sounds are set to zero, cos of my stupid onboard soundcard
- result = system->setHardwareChannels(32, 64, 0, 0);
- CheckError();
- // Initialize FMOD. (100 - # of virtual voices)
- result = system->init(100, FMOD_INIT_NORMAL, 0);
- CheckError();
- return true;
- }
- bool CSound::CheckError()
- {
- if (result != FMOD_OK)
- {
- errf("FMOD error!", FMOD_ErrorString(result));
- err(FMOD_ErrorString(result), "FMOD error!");
- exit(-1);
- }
- return 1;
- }
- void CSound::StopSound(s_played *p)
- {
- vPlayed.rm(p);
- }
- void CSound::KeepPosition(s_played *p)
- {
- s_played *ps;
- ps = *(vPlayed.Find(p));
- if (!ps) return;
- ps->pPosition = 0;
- }