SoundInterval.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # DCR - what this is all about: Miles is under-reporting the
  47. # length of MP3 files, and they're getting cut off too early.
  48. # This is a temporary hack. We could:
  49. # - hack Miles to fix its MP3 length calculation
  50. # - complain louder about this to RAD
  51. # - precompute MP3 durations and store them in a table
  52. # drose - ok, I've put in a lower-level workaround in the
  53. # MilesAudioManager. This is no longer necessary up here,
  54. # where it pollutes SoundInterval for everyone.
  55. #duration += min(duration * 2.4, 1.5)
  56. # Generate unique name if necessary
  57. if (name == None):
  58. name = id
  59. # Initialize superclass
  60. Interval.Interval.__init__(self, name, duration)
  61. def privInitialize(self, t):
  62. # If its within a 10th of a second of the start,
  63. # start at the beginning
  64. t1 = t + self.startTime
  65. if (t1 < 0.1):
  66. t1 = 0.0
  67. if t1 < self.soundDuration:
  68. base.sfxPlayer.playSfx(self.sound, self.fLoop, 1, self.volume, t1, self.node)
  69. self.state = CInterval.SStarted
  70. self.currT = t
  71. def privStep(self, t):
  72. if self.state == CInterval.SPaused:
  73. # Restarting from a pause.
  74. t1 = t + self.startTime
  75. if t1 < self.soundDuration:
  76. base.sfxPlayer.playSfx(self.sound, self.fLoop, 1, self.volume, t1, self.node)
  77. self.state = CInterval.SStarted
  78. self.currT = t
  79. def privFinalize(self):
  80. if self.sound != None:
  81. self.sound.stop()
  82. self.currT = self.getDuration()
  83. self.state = CInterval.SFinal
  84. def privInterrupt(self):
  85. if self.sound != None:
  86. self.sound.stop()
  87. self.state = CInterval.SPaused