SoundInterval.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """SoundInterval module: contains the SoundInterval class"""
  2. from pandac.PandaModules import *
  3. from direct.directnotify.DirectNotifyGlobal import *
  4. import Interval
  5. class SoundInterval(Interval.Interval):
  6. # Name counter
  7. soundNum = 1
  8. # create SoundInterval DirectNotify category
  9. notify = directNotify.newCategory('SoundInterval')
  10. # Class methods
  11. # Create a sound interval
  12. # If loop = 0, sound will play once, duration of the interval
  13. # equals the duration of the sound
  14. # If loop = 1, the sound will loop for the specified duration
  15. # If no duration is specified, sound will loop for the duration
  16. # of the sound, i.e. it will only play once.....usually, there
  17. # seems to be some timing in the audio such that the stop doesn't
  18. # kill the looping sound until the next time around if duration
  19. # of the interval equals duration of the sound
  20. def __init__(self, sound, loop = 0, duration = 0.0, name = None,
  21. volume = 1.0, startTime = 0.0, node=None):
  22. """__init__(sound, loop, name)
  23. """
  24. # Generate unique name
  25. id = 'Sound-%d' % SoundInterval.soundNum
  26. SoundInterval.soundNum += 1
  27. # Record instance variables
  28. self.sound = sound
  29. if sound:
  30. self.soundDuration = sound.length()
  31. else:
  32. self.soundDuration = 0
  33. self.fLoop = loop
  34. self.volume = volume
  35. self.startTime = startTime
  36. self.node = node
  37. # If no duration given use sound's duration as interval's duration
  38. if float(duration) == 0.0 and self.sound != None:
  39. duration = max(self.soundDuration - self.startTime, 0)
  40. #if (duration == 0):
  41. # self.notify.warning('zero length duration!')
  42. # MPG - hack for Miles bug
  43. #duration += 1.5
  44. # DCR - hack for Miles bug - adding 1.5 seconds caused
  45. # problems for MG_neg_buzzer.wav
  46. duration += min(duration * 2.4, 1.5)
  47. # Generate unique name if necessary
  48. if (name == None):
  49. name = id
  50. # Initialize superclass
  51. Interval.Interval.__init__(self, name, duration)
  52. def privInitialize(self, t):
  53. # If its within a 10th of a second of the start,
  54. # start at the beginning
  55. t1 = t + self.startTime
  56. if (t1 < 0.1):
  57. t1 = 0.0
  58. if t1 < self.soundDuration:
  59. base.sfxPlayer.playSfx(self.sound, self.fLoop, 1, self.volume, t1, self.node)
  60. self.state = CInterval.SStarted
  61. self.currT = t
  62. def privStep(self, t):
  63. if self.state == CInterval.SPaused:
  64. # Restarting from a pause.
  65. t1 = t + self.startTime
  66. if t1 < self.soundDuration:
  67. base.sfxPlayer.playSfx(self.sound, self.fLoop, 1, self.volume, t1, self.node)
  68. self.state = CInterval.SStarted
  69. self.currT = t
  70. def privFinalize(self):
  71. if self.sound != None:
  72. self.sound.stop()
  73. self.currT = self.getDuration()
  74. self.state = CInterval.SFinal
  75. def privInterrupt(self):
  76. if self.sound != None:
  77. self.sound.stop()
  78. self.state = CInterval.SPaused