InvSounds.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include "InvSounds.h"
  5. #include <QDir>
  6. #include <QDebug>
  7. using namespace GE;
  8. CInvSounds::CInvSounds(QObject *parent) : QObject( parent )
  9. {
  10. m_musicInstance = 0;
  11. m_sounds = 0;
  12. m_internalSounds = 0;
  13. m_soundCount = 0;
  14. m_internalSoundCount = 0;
  15. enableSounds(true);
  16. m_audioOut = new AudioOut( this, &m_mixer );
  17. enableInternalSounds();
  18. }
  19. void CInvSounds::enableSounds(bool enable)
  20. {
  21. if (enable)
  22. m_mixer.setGeneralVolume(0.2f);
  23. else
  24. m_mixer.setGeneralVolume(0);
  25. }
  26. void CInvSounds::enableInternalSounds()
  27. {
  28. disableInternalSounds();
  29. // Application internal sounda
  30. m_internalSoundPaths.append(":/sound/menu1.wav");
  31. m_internalSoundPaths.append(":/sound/menu2.wav");
  32. m_internalSoundPaths.append(":/sound/gamestart.wav");
  33. m_internalSoundPaths.append(":/sound/gameover.wav");
  34. m_internalSoundPaths.append(":/sound/youwin.wav");
  35. m_internalSoundCount = m_internalSoundPaths.count();
  36. m_internalSounds = new CAudioBuffer*[m_internalSoundCount];
  37. // Load general application sounds
  38. for (int ff=0; ff<m_internalSoundPaths.count(); ff++) {
  39. m_internalSounds[ff] = CAudioBuffer::loadWav(m_internalSoundPaths[ff]);
  40. }
  41. }
  42. void CInvSounds::enableSounds(QStringList sounds)
  43. {
  44. disableSounds();
  45. // Sounds from the level
  46. m_soundPaths = sounds;
  47. m_soundCount = sounds.count();
  48. m_sounds = new CAudioBuffer*[m_soundCount];
  49. // Load level sounds
  50. for (int f=0; f<m_soundPaths.count(); f++) {
  51. m_sounds[f] = CAudioBuffer::loadWav(m_soundPaths[f]);
  52. }
  53. }
  54. void CInvSounds::disableInternalSounds()
  55. {
  56. for (int f=0; f<m_internalSoundCount; f++) {
  57. if (m_internalSounds[f]) {
  58. delete m_internalSounds[f];
  59. }
  60. }
  61. delete [] m_internalSounds;
  62. m_internalSoundCount = 0;
  63. m_internalSoundPaths.clear();
  64. m_musicInstance = 0;
  65. }
  66. void CInvSounds::disableSounds()
  67. {
  68. for (int f=0; f<m_soundCount; f++) {
  69. if (m_sounds[f]) {
  70. delete m_sounds[f];
  71. }
  72. }
  73. delete [] m_sounds;
  74. m_sounds = 0;
  75. m_soundCount = 0;
  76. m_soundPaths.clear();
  77. m_musicInstance = 0;
  78. }
  79. CInvSounds::~CInvSounds() {
  80. if (m_audioOut) {
  81. delete m_audioOut;
  82. m_audioOut = 0;
  83. }
  84. disableSounds();
  85. disableInternalSounds();
  86. }
  87. void CInvSounds::beginMusicOn() {
  88. m_musicInstance = (GE::CAudioBufferPlayInstance*)m_mixer.addAudioSource( new GE::CAudioBufferPlayInstance( m_sounds[0] ) );
  89. m_musicInstance->setLoopTimes(-1);
  90. }
  91. void CInvSounds::beginMusicOff() {
  92. if (!m_musicInstance) return;
  93. m_musicInstance->setLoopTimes(0); // stop when finished
  94. m_musicInstance = 0;
  95. };
  96. void CInvSounds::playSound(int index) {
  97. if (index < m_soundCount && index > -1) {
  98. m_sounds[index]->playWithMixer( m_mixer );
  99. }
  100. }
  101. void CInvSounds::playSounds(int index, int count)
  102. {
  103. if (index < m_soundCount && index > -1) {
  104. CAudioBufferPlayInstance* i = m_sounds[index]->playWithMixer( m_mixer );
  105. i->setLoopTimes(count);
  106. }
  107. }
  108. void CInvSounds::playInternalSound(int index) {
  109. if (index < m_internalSoundCount && index > -1) {
  110. m_internalSounds[index]->playWithMixer( m_mixer );
  111. }
  112. }
  113. void CInvSounds::playInternalSounds(int index, int count)
  114. {
  115. if (index < m_internalSoundCount && index > -1) {
  116. CAudioBufferPlayInstance* i = m_internalSounds[index]->playWithMixer( m_mixer );
  117. i->setLoopTimes(count);
  118. }
  119. }
  120. void CInvSounds::gameStartSound() {
  121. CAudioBufferPlayInstance* i = m_internalSounds[2]->playWithMixer( m_mixer );
  122. i->setLoopTimes(2);
  123. };