libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moSoundManager.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moSoundManager.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28 
29 *******************************************************************************/
30 #undef generic
31 #include "moSoundManager.h"
32 
33 #include "moArray.h"
34 moDefineDynamicArray( moSoundArray )
35 moDefineDynamicArray( moSoundEffectArray )
36 moDefineDynamicArray( moSoundBufferArray )
37 
38 #ifdef MO_WIN32
39 /*
40  #include <AL/alut.h>
41 */
42 #endif
43 
44 #ifdef MO_LINUX
45  #include <AL/al.h>
46  #include <AL/alut.h>
47  #include <AL/alext.h>
48 #endif
49 
50 #ifdef MO_MACOSX
51  //#include <al.h>
52  //#include <alut.h>
53  //#include <alext.h>
54 #endif
55 
56 
58 
59  SetName("");
60  SetFileName("");
61  m_pAudioGraph = NULL;
62  m_bIsPlaying = false;
63  m_pAudioGraph = new moGsGraph();
64  m_BufferSize = 0;
65  m_ActualSample = 0;
66  m_OldSample = 0;
67 }
68 
70  Finish();
71 }
72 
73 
75  if (m_pAudioGraph) {
76  delete m_pAudioGraph;
77  m_pAudioGraph = NULL;
78  }
80  return (m_pAudioGraph == NULL);
81 }
82 
84  if (m_pAudioGraph) {
86  }
87  return m_bInitialized;
88 }
89 
91  return m_SoundName;
92 }
93 
94 void moSound::SetName( moText name ) {
95  m_SoundName = name;
96 }
97 
99  return m_FileName;
100 }
101 
102 void moSound::SetFileName( moText filename ) {
103  m_FileName = filename;
104 }
105 
107  return m_SoundType;
108 }
109 
111  if (m_pAudioGraph) {
112  if (m_pAudioGraph->Initialized()) {
113  if (m_pAudioGraph->BuildLiveSound( filename )) {
114  SetFileName(filename);
115  return true;
116  }
117  } else {
118  MODebug2->Error("moSound::LoadFromFile > Error: AudioGraph not initialized.");
119  }
120  }
121  return false;
122 }
123 
125  moFile soundFile(p_filename);
126  if ( soundFile.GetExtension()==".mp3"
127  || soundFile.GetExtension()==".wav"
128  || soundFile.GetExtension()==".m4a"
129  || soundFile.GetExtension()==".ogg" ) {
130  return true;
131  }
132  return true;
133 }
134 
136  if (m_pAudioGraph) {
137  m_pAudioGraph->Play();
138  }
139 }
140 
142  if (m_pAudioGraph)
143  m_pAudioGraph->Pause();
144 }
145 
147  if (m_pAudioGraph) {
148  m_pAudioGraph->Stop();
149  }
150 }
151 
152 
154  if (m_pAudioGraph)
155  m_pAudioGraph->Stop();
156 }
157 
159 
160  moStreamState stream_state = MO_STREAMSTATE_UNKNOWN;
161 
162  if (m_pAudioGraph) {
163 
164  stream_state = m_pAudioGraph->GetState();
165 
166  switch(stream_state) {
168  m_bIsPlaying = true;
169  break;
170  default:
171  m_bIsPlaying = false;
172  break;
173  }
174 
175  }
176 
177  return stream_state;
178 }
179 
181 
182  if (m_pAudioGraph) {
184  }
185  return m_bIsPlaying;
186 }
187 
188 
190  // if (m_pAudioGraph)
191  // m_pAudioGraph->Stop();
192 }
193 
195  if (m_pAudioGraph)
196  return m_pAudioGraph->GetPositionMS();
197  return 0;
198 }
199 
201  if (m_pAudioGraph)
203  return 0;
204 }
205 
206 
208  if (m_pAudioGraph)
209  return m_pAudioGraph->GetDuration();
210  return 0;
211 }
212 void moSound::Seek( int position, float rate ) {
213  if (m_pAudioGraph)
214  m_pAudioGraph->Seek( position, rate );
215 }
216 
218  if (m_pAudioGraph)
219  return m_pAudioGraph->IsEOS();
220  return false;
221 }
222 
223 void moSound::SetVolume( float gain ) {
224  if (m_pAudioGraph)
225  m_pAudioGraph->SetVolume(gain);
226 
227  m_Volume = gain;
228 
229 }
230 
231 
233  return m_Volume;
234 
235 }
236 
237 
238 void moSound::SetPitch( float pitch ) {
239  if (m_pAudioGraph) {
240  m_pAudioGraph->SetPitch(pitch);
241  m_Pitch = pitch;
242  }
243 
244 }
245 
246 
248  return m_Pitch;
249 }
250 
251 
252 void moSound::SetBalance( float balance ) {
253  if (m_pAudioGraph) {
254  m_pAudioGraph->SetBalance( balance );
255  m_Balance = balance;
256  }
257 }
258 
259 
261  return m_Balance;
262 }
263 
264 
265 
266 void moSound::SetEchoDelay( float delay ) {
267 
268  if (m_pAudioGraph) {
269  m_pAudioGraph->SetEchoDelay(delay);
270  m_EchoDelay = delay;
271  }
272 }
273 
274 
276  return m_EchoDelay;
277 }
278 
279 
280 void moSound::SetEchoIntensity( float intensity ) {
281  if (m_pAudioGraph) {
282  m_pAudioGraph->SetEchoIntensity(intensity);
283  m_EchoIntensity = intensity;
284  }
285 }
286 
287 
289  return m_EchoIntensity;
290 }
291 
292 
293 void moSound::SetEchoFeedback( float feedback ) {
294  if (m_pAudioGraph) {
295  m_pAudioGraph->SetEchoFeedback(feedback);
296  m_EchoFeedback = feedback;
297  }
298 }
299 
300 
302  return m_EchoFeedback;
303 }
304 
305 void
307  MODebug2->Error("Not implemented in moSound, must use an effect as Sound3D to get a full moSound3D interface.");
308 }
309 
310 
311 
319 }
320 
322  Finish();
323 }
324 
326  // Clean up by deleting Source(s) and Buffer(s)
327  return true;
328 }
329 
330 
332  m_BufferSize = 0;
333  m_ActualSample = 0;
334  m_OldSample = 0;
335  return true;
336 }
337 
338 
339 
340 void moSound3D::SetPosition( float x, float y, float z ) {
341 /*
342  alSource3f( m_SourceId, AL_POSITION, x, y, z );
343 */
344  x = 0;
345  y = 0;
346  z = 0;
347 }
348 
349 void moSound3D::SetVelocity( float x, float y, float z ) {
350 /*
351  alSource3f( m_SourceId, AL_VELOCITY, x, y, z );
352 */
353  x = 0;
354  y = 0;
355  z = 0;
356 }
357 
358 void moSound3D::SetDirection( float x, float y, float z ) {
359 /*
360  alSource3f( m_SourceId, AL_DIRECTION, x, y, z );
361 */
362  x = 0;
363  y = 0;
364  z = 0;
365 }
366 
367 
368 
369 /*======================*/
370 /* moSoundManager */
371 /*======================*/
373 #ifdef MO_WIN32
374  //alEffectf( m_EffectId, param, flValue );
375 #endif
376  param = 0;
377  flValue = 0;
378 }
379 
380 void moSoundEffect::GetParameterF( MOint param, MOfloat *pflValue ) {
381 #ifdef MO_WIN32
382  //alGetEffectf( m_EffectId, param, pflValue );
383 #endif
384  param = 0;
385  pflValue = NULL;
386 }
387 
391 
392  SetName("soundmanager");
393  SetLabelName("soundmanager");
394 }
395 
396 
398  Finish();
399 }
400 
401 
403 
404  if (!m_pResourceManager) return false;
405 
407 
408 #ifdef MO_WIN32
409  #ifdef MO_USING_ALFW
410  ALenum eBufferFormat = 0;
411 
412  ALFWInit();
413 
414  if (!ALFWInitOpenAL())
415  {
416  ALFWprintf("Failed to initialize OpenAL\n");
417  ALFWShutdown();
418  return 0;
419  }
420 
421  // For this example, we are going to play a 5.1 test file ...
422  /*eBufferFormat = alGetEnumValue("AL_FORMAT_51CHN16");
423  if (!eBufferFormat)
424  {
425  ALFWprintf("No support for 5.1 playback!\n");
426  ALFWShutdownOpenAL();
427  ALFWShutdown();
428  return 0;
429  }
430 */
431 
432  if (ALFWIsXRAMSupported()) {
433  //continue
434  }
435  if (ALFWIsEFXSupported()) {
436 
437  //esto deberia configurarse desde el config
438  moSoundEffect* pneweffect = new moSoundEffect();
439  /*
440  ALuint genfxid;
441  if (pneweffect) {
442  alGenEffects(1, &genfxid);
443  pneweffect->m_EffectId = genfxid;
444  alEffecti(pneweffect->m_EffectId, AL_EFFECT_TYPE, AL_EFFECT_COMPRESSOR);
445  m_effects_array.Add(pneweffect);
446  }
447 
448  pneweffect = new moSoundEffect();
449  genfxid;
450  if (pneweffect) {
451  alGenEffects(1, &genfxid);
452  pneweffect->m_EffectId = genfxid;
453  alEffecti(pneweffect->m_EffectId, AL_EFFECT_TYPE, AL_EFFECT_EQUALIZER);
454  m_effects_array.Add(pneweffect);
455  }*/
456  }
457  #endif
458  #endif
459 
460  #ifdef MO_LINUX
461 
462  #endif
463 
464  m_sounds_array.Init( 0, NULL );
465 
466  m_bInitialized = true;
467 
468  return m_bInitialized;
469 }
470 
471 moSoundEffectArray*
473 
474  return &m_effects_array;
475 
476 }
477 
479  return m_sounds_array.Count();
480 }
481 
482 
484 
485  if (id>=0 && id<(int)m_sounds_array.Count()) {
486  return m_sounds_array[id];
487  }
488  return NULL;
489 
490 }
491 
492 moSound* moSoundManager::GetSound( moText p_name, bool create) {
493 
494  int id;
495 
496  if (p_name=="") {
497  return NULL;
498  }
499 
500  id = GetSoundId( p_name, create);
501 
502  if(id == MO_SOUNDERROR ) {
503  if (create)
504  MODebug2->Error("ERROR DE SONIDO, no se pudo cargar: " + p_name );
505  return(NULL);
506  } //error
507  else {
508  return(m_sounds_array[id]);
509  }
510 }
511 
512 MOint
513 moSoundManager::GetSoundId( moText p_name, bool create ) {
514 
515  MOint i,idnuevo;
516 
517  for( i=0; i<(int)m_sounds_array.Count(); i++ ) {
518  if(!stricmp( m_sounds_array[i]->GetName(), p_name )) {
519  return(i);
520  break;
521  }
522  }
523 
524  if (!create) return -1;
525 
526  moSoundType stype = GetTypeForFile(p_name);
527  if (!m_pResourceManager) return MO_SOUNDERROR;
528 
530  namefull += moSlash + (const moText)p_name;
531 
532  moFile fileSound(namefull);
533  namefull = fileSound.GetCompletePath();
534 
535  moSoundParam sparam = MODefSoundParams ;
536  moSound* pSound = CreateSound( p_name );
537 
538  if (pSound) {
539 
540  pSound->LoadFromFile( namefull );
541 
542  m_sounds_array.Add( pSound );
543 
544  idnuevo = m_sounds_array.Count() - 1;
545  } else {
546  idnuevo = -1;
547  }
548  //si es superior a 65535 es un error
549  stype = MO_SOUNDTYPE_UNDEFINED;
550  return(idnuevo);
551 }
552 
553 int
555 
556  if (p_Sound) {
557  m_sounds_array.Add( p_Sound );
558  return m_sounds_array.Count()-1;
559  }
560  return -1;
561 
562 }
563 
566 
567  moText extension = p_name;
568  extension.Right(3);
569 
570  if(!stricmp(extension,"mp3") || !stricmp(extension,"mpg") || !stricmp(extension,"peg")) {
571  return MO_SOUNDTYPE_MP3;
572  } else if(!stricmp(extension,"m4a")) {
573  return MO_SOUNDTYPE_M4A;
574  } else if(!stricmp(extension,"wav")) {
575  return MO_SOUNDTYPE_WAV;
576  } else if(!stricmp(extension,"ogg")) {
577  return MO_SOUNDTYPE_OGG;
578  } else return MO_SOUNDTYPE_UNDEFINED;
579 }
580 
582 
583  moSound* pSound;
584 
585  pSound = new moSound();
586 
587  if (pSound) {
588 
589  pSound->SetName(p_name);
590  pSound->Init();
591 
592  return pSound;
593  } else return NULL;
594 
595 }
596 
597 
599  for(MOuint i=0; i<m_sounds_array.Count(); i++) {
600  //moSound* psound = m_sounds_array[i];
601  //if (psound) delete psound;
602  //psound = NULL;
603  }
604  m_sounds_array.Empty();
605 
606  for(MOuint i=0; i<m_effects_array.Count(); i++) {
607  //moSoundEffect* psoundeffect = m_effects_array[i];
608  //if (psoundeffect) delete psoundeffect;
609  //psoundeffect = NULL;
610  }
611  m_effects_array.Empty();
612 
613  #ifdef MO_WIN32
614  #ifdef MO_USING_ALFW
615  if (m_bInitialized) {
616  ALFWShutdownOpenAL();
617  ALFWShutdown();
618  }
619  #endif
620  #endif
621  return true;
622 }
623 
virtual void Rewind()
void SetEchoIntensity(float intensity)
Definition: moGsGraph.cpp:3869
moText m_FileName
MOboolean Initialized()
Pregunta si está inicializado.
Definition: moAbstract.cpp:153
moDefineDynamicArray(moSoundArray) moDefineDynamicArray(moSoundEffectArray) moDefineDynamicArray(moSoundBufferArray) moSound
virtual void Seek(int position, float rate=1.0)
#define MOulong
Definition: moTypes.h:392
void GetParameterF(MOint param, MOfloat *pflValue)
virtual void SetEchoIntensity(float intensity)
Parámetros de sonido.
void Error(moText p_text)
Anuncia y registra un error.
Definition: moAbstract.cpp:79
void SetName(const moText &p_name)
void Stop()
Detener la reproducción del video.
Definition: moGsGraph.cpp:3652
void SetPitch(float pitch)
Definition: moGsGraph.cpp:3852
virtual MOulong GetSampleCount()
float m_EchoFeedback
moSoundType m_SoundType
virtual ~moSoundManager()
virtual void Play()
#define MOboolean
Definition: moTypes.h:385
virtual MOboolean Init()
Inicializa el objeto.
#define MO_SOUNDERROR
void SetResourceType(moResourceType p_restype)
moSoundEffectArray * GetEffects()
moSoundType GetTypeForFile(moText p_name)
const moText & GetName() const
void Seek(MOuint frame, float rate=1.0)
Definition: moGsGraph.cpp:3674
void SetPosition(float x, float y, float z)
virtual float GetEchoIntensity()
void SetDirection(float x, float y, float z)
moSoundEffectArray m_effects_array
virtual float GetVolume()
void SetVelocity(float x, float y, float z)
moText GetExtension()
Get absolute path and filename "/D/PP/myFileName.txt".
Definition: moFile.cpp:543
virtual MOboolean Finish()
Finaliza el objeto, libera recursos.
MOulong GetSamplesLength()
Definition: moGsGraph.cpp:3748
MOint m_ActualSample
bool BuildLiveSound(moText filename)
Definition: moGsGraph.cpp:2737
virtual bool InitGraph()
Inicialización del grafo.
Definition: moGsGraph.cpp:1532
#define MOfloat
Definition: moTypes.h:403
virtual float GetBalance()
moText m_SoundName
virtual MOboolean Init()
MOboolean LoadFromFile(moText filename)
virtual void PlaySample(MOint sampleid)
clase de para manejar textos
Definition: moText.h:75
virtual void SetFileName(moText filename)
virtual ~moSound()
moResourceManager * m_pResourceManager
Puntero al administrador de recursos.
Dispositivo de entrada/salida, típicamente, interfaces humanas de IO y datos ( teclado, mouse, tableta, tcp, udp, serial )
Definition: moTypes.h:532
float m_Balance
moGsGraph * m_pAudioGraph
int AddSound(moSound *p_Sound)
Efecto de sonido.
virtual MOulong GetPosition()
virtual void Stop()
virtual bool IsEOS()
moText0 moText
Definition: moText.h:291
virtual MOulong GetDuration()
La duración total del stream en nanosegundos.
Definition: moGsGraph.cpp:3771
moSoundType GetType()
moStreamState
Definition: moVideoGraph.h:140
float m_EchoDelay
panorama
virtual void SetEchoDelay(float delay)
GStreamer Graph Class.
Definition: moGsGraph.h:151
MOint m_OldSample
moText0 & Right(MOuint)
Definition: moText.cpp:491
#define MOint
Definition: moTypes.h:388
virtual void SetPitch(float pitch)
void Pause()
Pausa la reproducción del video.
Definition: moGsGraph.cpp:3660
void SetEchoFeedback(float feedback)
Definition: moGsGraph.cpp:3876
MOboolean Finish()
virtual moText GetFileName()
virtual float GetPitch()
void SetLabelName(const moText &p_labelname)
virtual void SetBalance(float balance)
virtual void Pause()
Clase base de sonido.
static moDebug * MODebug2
Clase de impresión de errores para depuración
Definition: moAbstract.h:225
virtual float GetEchoFeedback()
moDataManager * GetDataMan()
bool m_bIsPlaying
virtual MOulong GetDuration()
moSoundType
Tipo de sonido.
virtual MOulong GetPositionMS()
Definition: moGsGraph.cpp:3814
virtual ~moSound3D()
void SetVolume(float volume)
Definition: moGsGraph.cpp:3838
virtual void SetEchoFeedback(float feedback)
virtual moStreamState State()
moSound * CreateSound(moText p_name)
int GetSoundId(moText p_name, bool create=true)
virtual moStreamState GetState()
Estado de la reproducción.
Definition: moGsGraph.cpp:3553
virtual bool IsEOS()
Definition: moGsGraph.cpp:1767
virtual MOboolean Finish()
Finaliza el objeto, libera recursos.
virtual MOboolean Init()
Inicializa el objeto.
void Play()
Reproducir el video.
Definition: moGsGraph.cpp:3641
moText GetDataPath()
virtual bool IsPlaying()
#define MOuint
Definition: moTypes.h:387
MOint m_BufferSize
virtual moText GetName()
moSoundArray m_sounds_array
float m_Pitch
virtual float GetEchoDelay()
virtual void SetName(moText name)
void SetEchoDelay(float delay)
Definition: moGsGraph.cpp:3861
MOboolean m_bInitialized
Valor de inicialización
Definition: moAbstract.h:223
float m_EchoIntensity
moSound * GetSound(moText p_name, bool create=true)
const moSoundParam MODefSoundParams
float m_Volume
void SetType(moMoldeoObjectType p_type)
void SetParameterF(MOint param, MOfloat flValue)
void SetBalance(float balance)
Fija el balance entre canal izquierdo y derecho en caso de haberlos.
Definition: moGsGraph.cpp:3845
virtual void SetVolume(float gain)
MOboolean Init()
MOboolean SupportedFile(moText p_filename)
virtual void Update()