// play void BassSound3D::Play() { m_channel = BASS_SampleGetChannel(m_handle, FALSE); if (m_looping) { BASS_ChannelFlags(m_channel, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); } else { BASS_ChannelFlags(m_channel, 0, BASS_SAMPLE_LOOP); } BASS_ChannelSetAttribute(m_channel, BASS_ATTRIB_VOL, m_volume); BASS_ChannelSet3DAttributes(m_channel, -1, m_minDistance, m_maxDistance, -1, -1, -1); BASS_3DVECTOR pos(m_Position.x, m_Position.y, m_Position.z); BASS_3DVECTOR vel(m_velocity.x, m_velocity.y, m_velocity.z); BASS_ChannelSet3DPosition(m_channel, &pos, NULL, &vel); BASS_Apply3D(); BASS_ChannelPlay(m_channel, FALSE); }
// SetPosition void BassSound3D::SetPosition(const math::Vector &v) { m_Position = v; BASS_3DVECTOR pos(m_Position.x, m_Position.y, m_Position.z); BASS_ChannelSet3DPosition(m_channel, &pos, NULL, NULL); BASS_Apply3D(); }
// SetVelocity void BassSound3D::SetVelocity(const math::Vector& v) { m_velocity = v; BASS_3DVECTOR vel(m_velocity.x, m_velocity.y, m_velocity.z); BASS_ChannelSet3DPosition(m_channel, NULL, NULL, &vel); BASS_Apply3D(); }
// SetMinMaxDistance void BassSound3D::SetMinMaxDistance(float _min, float _max /*= 10000.0f*/) { m_minDistance = _min; m_maxDistance = _max; BASS_ChannelSet3DAttributes(m_channel, -1, m_minDistance, m_maxDistance, -1, -1, -1); BASS_Apply3D(); }
gboolean Update(gpointer data) { int c,x,y,cx,cy; GtkWidget *dc=GetWidget("drawingarea1"); GdkGC *gc=dc->style->fg_gc[GTK_WIDGET_STATE(dc)]; GdkGCValues gcsave; gdk_gc_get_values(gc,&gcsave); cx=dc->allocation.width/2; cy=dc->allocation.height/2; { // clear the display GdkColor c={0,0xffff,0xffff,0xffff}; gdk_gc_set_rgb_fg_color(gc,&c); gdk_draw_rectangle(dc->window,gc,TRUE,0,0,dc->allocation.width,dc->allocation.height); } { // Draw the listener GdkColor c={0,0x8000,0x8000,0x8000}; gdk_gc_set_rgb_fg_color(gc,&c); gdk_draw_arc(dc->window,gc,TRUE,cx-4,cy-4,8,8,0,360*64); } for (c=0;c<chanc;c++) { // If the channel's playing then update it's position if (BASS_ChannelIsActive(chans[c].channel)==BASS_ACTIVE_PLAYING) { // Check if channel has reached the max distance if (chans[c].pos.z>=MAXDIST || chans[c].pos.z<=-MAXDIST) chans[c].vel.z=-chans[c].vel.z; if (chans[c].pos.x>=MAXDIST || chans[c].pos.x<=-MAXDIST) chans[c].vel.x=-chans[c].vel.x; // Update channel position chans[c].pos.z+=chans[c].vel.z*TIMERPERIOD/1000; chans[c].pos.x+=chans[c].vel.x*TIMERPERIOD/1000; BASS_ChannelSet3DPosition(chans[c].channel,&chans[c].pos,NULL,&chans[c].vel); } { // Draw the channel position indicator static GdkColor cols[2]={{0,0xffff,0xc000,0xc000},{0,0xffff,0,0}}; gdk_gc_set_rgb_fg_color(gc,&cols[chan==c]); x=cx+(int)((cx-10)*chans[c].pos.x/MAXDIST); y=cy-(int)((cy-10)*chans[c].pos.z/MAXDIST); gdk_draw_arc(dc->window,gc,TRUE,x-4,y-4,8,8,0,360*64); } } // Apply the 3D changes BASS_Apply3D(); gdk_gc_set_values(gc,&gcsave,GDK_GC_FOREGROUND); return TRUE; }
/// <summary> /// Actualiza la posición del listener. /// </summary> void CSoundManager::UpdateListenerPosition(Vect3f _Listener) { if (!m_pListener) return; bool result = false; //Vect3f posCamera = m_pListener->GetPosition(); //Vect3f posCamera = g_PhysXController->GetPosition(); // dejo el nombre de "posCamera" porque asi lo tenia Javier, pero en realidad paso como paramero la posicion del controller del player // TODO: (Andres) finalmente, creo que se podria pasar como parametro un object3D para no tener que calcular los vectores, front y top. (o algo asi) Vect3f posCamera = _Listener; // 1. Convert Vect3f& Pos to BASS_3DVECTOR BASS_3DVECTOR pos; pos.x = posCamera.x; pos.y = posCamera.y; pos.z = posCamera.z; BASS_3DVECTOR top; top.x = 0; top.y = 1; top.z = 0; //top += pos; top.x = top.x + pos.x; //top.y = top.y + pos.y; top.y = top.y + 1; top.z = top.z + pos.z; BASS_3DVECTOR front; front.x = cos(m_pListener->GetYaw()); front.y = 0; front.z = sin(m_pListener->GetYaw()); //front += pos; front.x = front.x + pos.x; front.y = front.y + pos.y; front.z = front.z + pos.z; // 2. Set listener's new position result = BASS_Set3DPosition(&pos,NULL, &front, &top)?true:false; BASS_Apply3D(); }
/// <summary> /// Inicializa los canales de sonido. /// </summary> ///<param name="initSoundXML">Ubicación del fichero xml.</param> ///<returns>Boleano que representa si la operación se realizó exitosamente.</returns> bool CSoundManager::Init (const std::string& initSoundXML) { m_bIsOk = BASS_Init(1, 44100, BASS_DEVICE_3D, 0, NULL) ? true : false; if (m_bIsOk == true) { // Parsea el xml m_bIsOk = Load(initSoundXML); if (m_bIsOk == true) { m_bIsOk = BASS_Set3DPosition(NULL, NULL, NULL, NULL) ? true : false; if (m_bIsOk == true) { m_bIsOk = BASS_Set3DFactors(1.0, 1.0, 1.0) ? true : false; BASS_Apply3D(); if (m_bIsOk == true) { // Asigna el master volume. Usar 1 para volumen máximo //m_bIsOk = BASS_SetVolume(1.0f) ? true : false; if (m_bIsOk == true) { m_bIsOk = BASS_Start() ? true : false; if (m_bIsOk == true) { std::string msg_info = "CSoundManager::Init: Sonidos cargados exitosamente."; LOGGER->AddNewLog(ELL_INFORMATION, msg_info.c_str()); return m_bIsOk; } } } } } } std::string msg_error = "CSoundManager::Init: Error cargando sonidos."; LOGGER->AddNewLog(ELL_INFORMATION, msg_error.c_str()); return m_bIsOk; }
/// <summary> /// Reproduce sonidos usando el volumen dado. /// </summary> ///<param name="SoundName">Nombre del sonido a reproducir.</param> void CSoundManager::PlaySoundVolume (const std::string &SoundName, float _fVolume) { bool pass = false; std::map<std::string, SSoundData>::iterator iter = m_Resources.find(SoundName); if(iter == m_Resources.end()) { // Guarda mensaje de error en el log std::string msg_error = "CSoundManager::PlaySound->No se encontró el identificador del sonido: "; msg_error.append(SoundName.c_str()); LOGGER->AddNewLog(ELL_ERROR, msg_error.c_str()); return; } SSoundData sData = iter->second; if (sData.m_type == false) // False si es sample { sData.m_Id = BASS_SampleGetChannel(sData.m_Id, FALSE); //Agregarlo a canal de sample en el vector m_Channels; //Canales 2 - 19 if (m_iSamplerChannels == 19) { // El número de canales de sonidos ha llegado a su máximo // Liberar el canal 2 y asignar este sonido a este canal m_iSamplerChannels = 2; } else { // Asignar esta música al canal que corresponde ++m_iSamplerChannels; } m_Channels[m_iSamplerChannels].m_dwSource = sData.m_Id; m_Channels[m_iSamplerChannels].m_fVolumen = _fVolume; } // True si es música else { // En caso de que los dos canales de música ya estén asignados if (m_iMusicChannels == 1) { m_iMusicChannels = 0; } else { // Asignar esta música al canal 1 ++m_iMusicChannels; } //Agregarlo a canal de música en el vector m_Channels; m_Channels[m_iMusicChannels].m_dwSource = sData.m_Id; m_Channels[m_iMusicChannels].m_fVolumen = _fVolume; } // Añade la configuración del canal pass = BASS_ChannelSetAttribute(sData.m_Id, BASS_ATTRIB_VOL, _fVolume) ? true : false; if (pass == false) { int errorCode = BASS_ErrorGetCode(); errorCode = 0; } // Looping sound if (sData.m_Loop == true) { //BASS_ChannelFlags(sData.m_Id, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set LOOP flag to true } if (sData.m_ThreeDimensional == true) { BASS_3DVECTOR vector; vector.x = sData.m_Position.x; vector.y = sData.m_Position.y; vector.z = sData.m_Position.z; bool result = BASS_ChannelSet3DPosition(sData.m_Id, &vector, NULL, NULL)?true:false; if (result == true) { BASS_Apply3D(); } else { int errorCode = BASS_ErrorGetCode(); errorCode = 0; } } if (pass) { // Reproducción del canal pass = BASS_ChannelPlay(sData.m_Id, FALSE)?true:false; if (pass) { std::string msg_debug = "Sonido reproducido con éxito"; } } }
void main(int argc, char **argv) { BUFSTUFF b,b2; BASS_3DVECTOR p={0,0,0}; printf("BASS 2 stereo channels on 4 speakers example : MOD/MPx/OGG/WAV\n" "--------------------------------------------------------------\n" " Set your soundcard's output to 4 or 5.1 speakers\n"); /* check that BASS 1.6 was loaded */ if (BASS_GetVersion()!=MAKELONG(1,6)) { printf("BASS version 1.6 was not loaded\n"); return; } if (argc!=3) { printf("\tusage: 4speaker <file1> <file2>\n"); return; } /* setup output - default device, 44100hz, stereo, 16 bits */ if (!BASS_Init(-1,44100,BASS_DEVICE_3D,0)) Error("Can't initialize device"); /* try initializing the 1st (front) file */ if (!(b.dstr=BASS_StreamCreateFile(FALSE,argv[1],0,0,BASS_STREAM_DECODE))) if (!(b.dstr=BASS_MusicLoad(FALSE,argv[1],0,0,BASS_MUSIC_DECODE|BASS_MUSIC_RAMPS))) Error("Can't play the 1st file"); if (BASS_ChannelGetFlags(b.dstr)&BASS_SAMPLE_MONO) Error("The 1st stream is mono!"); /* try initializing the 2nd (rear) file */ if (!(b2.dstr=BASS_StreamCreateFile(FALSE,argv[2],0,0,BASS_STREAM_DECODE))) if (!(b2.dstr=BASS_MusicLoad(FALSE,argv[2],0,0,BASS_MUSIC_DECODE|BASS_MUSIC_RAMPS))) Error("Can't play the 2nd file"); if (BASS_ChannelGetFlags(b2.dstr)&BASS_SAMPLE_MONO) Error("The 2nd stream is mono!"); printf("front : %s\n",argv[1]); printf("rear : %s\n",argv[2]); /* Get sample rates and allocate buffers */ BASS_ChannelGetAttributes(b.dstr,&b.freq,0,0); b.buf=malloc(b.freq*4); // 1 sec buffer BASS_ChannelGetAttributes(b2.dstr,&b2.freq,0,0); b2.buf=malloc(b2.freq*4); // 1 sec buffer /* Create streams to play the 1st decoded data, and link them */ b.lstr=BASS_StreamCreate(b.freq,BASS_SAMPLE_MONO|BASS_SAMPLE_3D,(STREAMPROC*)&stream_left,(DWORD)&b); b.rstr=BASS_StreamCreate(b.freq,BASS_SAMPLE_MONO|BASS_SAMPLE_3D,(STREAMPROC*)&stream_right,(DWORD)&b); BASS_ChannelSetLink(b.lstr,b.rstr); /* Create streams to play the 2nd decoded data, and link them */ b2.lstr=BASS_StreamCreate(b2.freq,BASS_SAMPLE_MONO|BASS_SAMPLE_3D,(STREAMPROC*)&stream_left,(DWORD)&b2); b2.rstr=BASS_StreamCreate(b2.freq,BASS_SAMPLE_MONO|BASS_SAMPLE_3D,(STREAMPROC*)&stream_right,(DWORD)&b2); BASS_ChannelSetLink(b2.lstr,b2.rstr); /* position the streams */ p.z=3; // front p.x=-1.5; // left BASS_ChannelSet3DPosition(b.lstr,&p,0,0); p.x=1.5; // right BASS_ChannelSet3DPosition(b.rstr,&p,0,0); p.z=-3; // rear p.x=-1.5; // left BASS_ChannelSet3DPosition(b2.lstr,&p,0,0); p.x=1.5; // right BASS_ChannelSet3DPosition(b2.rstr,&p,0,0); BASS_Apply3D(); BASS_Start(); /* start it! */ b.writepos=b.readposl=b.readposr=0; BASS_StreamPlay(b.lstr,FALSE,0); // start front b2.writepos=b2.readposl=b2.readposr=0; BASS_StreamPlay(b2.lstr,FALSE,0); // start rear while (!_kbhit() && (BASS_ChannelIsActive(b.lstr) || BASS_ChannelIsActive(b2.lstr))) { /* display some stuff and wait a bit */ printf("pos %09I64d %09I64d - cpu %.1f%% \r", BASS_ChannelGetPosition(b.lstr)*2,BASS_ChannelGetPosition(b2.lstr)*2,BASS_GetCPU()); Sleep(50); } BASS_Free(); free(b.buf); }