PageRenderTime 389ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/example/ipy_dx/dsound/playsound.py

http://cwcpylib.googlecode.com/
Python | 178 lines | 155 code | 14 blank | 9 comment | 12 complexity | 59b0a02bbb8ca28fd095fe2fb2992d17 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. Description: DirectSound ¼½©ñ wave
  4. Author: Chui-Wen Chiu <sisimi.pchome@gmail.com>
  5. License: PYTHON SOFTWARE FOUNDATION LICENSE
  6. """
  7. __author__ = "Chui-Wen Chiu"
  8. __author_email__ = "cwchiu@hotmail.com"
  9. import clr
  10. # WinForm ¥²­n°Ñ¦Ò
  11. clr.AddReference("System")
  12. clr.AddReference("System.Drawing")
  13. clr.AddReference("System.Windows.Forms")
  14. # DirectX °Ñ¦Ò
  15. clr.AddReferenceByName("Microsoft.DirectX, Version=1.0.2902.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" )
  16. clr.AddReferenceByName("Microsoft.DirectX.DirectSound, Version=1.0.2902.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" )
  17. from System import *
  18. from System.IO import *
  19. from System.Windows.Forms import *
  20. from System.Drawing import *
  21. from Microsoft.DirectX import *
  22. from Microsoft.DirectX.DirectSound import *
  23. class MyForm(Form):
  24. def __init__(self):
  25. self.PathSoundFile = ''
  26. self.ApplicationBuffer = None
  27. self.ApplicationDevice = None
  28. self.InitializeComponent()
  29. def _Form1_Load(self, sender, e):
  30. try:
  31. self.ApplicationDevice = Device()
  32. self.ApplicationDevice.SetCooperativeLevel(self, CooperativeLevel.Priority)
  33. except:
  34. MessageBox.Show("Unable to create sound device. Sample will now exit.")
  35. self.Close()
  36. def Dispose(self, disposing):
  37. super(type(self), self).Dispose(disposing)
  38. def InitializeComponent(self):
  39. self.btnSoundfile = Button();
  40. self.lblFilename = Label();
  41. self.cbLoopCheck = CheckBox();
  42. self.btnPlay = Button();
  43. self.btnStop = Button();
  44. self.btnCancel = Button();
  45. self.SuspendLayout()
  46. #
  47. # btnSoundfile
  48. #
  49. self.btnSoundfile.Location = Point(10, 11);
  50. self.btnSoundfile.Name = "btnSoundfile";
  51. self.btnSoundfile.Size = Size(69, 21);
  52. self.btnSoundfile.TabIndex = 0;
  53. self.btnSoundfile.Text = "Sound &file...";
  54. self.btnSoundfile.Click += self.btnSoundfile_Click
  55. #
  56. # lblFilename
  57. #
  58. self.lblFilename.BorderStyle = BorderStyle.Fixed3D;
  59. self.lblFilename.Location = Point(94, 11);
  60. self.lblFilename.Name = "lblFilename";
  61. self.lblFilename.Size = Size(345, 21);
  62. self.lblFilename.TabIndex = 1;
  63. self.lblFilename.Text = "No file loaded.";
  64. self.lblFilename.TextAlign = ContentAlignment.MiddleLeft;
  65. #
  66. # cbLoopCheck
  67. #
  68. self.cbLoopCheck.Enabled = False;
  69. self.cbLoopCheck.Location = Point(9, 44);
  70. self.cbLoopCheck.Name = "cbLoopCheck";
  71. self.cbLoopCheck.Size = Size(87, 16);
  72. self.cbLoopCheck.TabIndex = 2;
  73. self.cbLoopCheck.Text = "&Loop sound";
  74. #
  75. # btnPlay
  76. #
  77. self.btnPlay.Enabled = False;
  78. self.btnPlay.Location = Point(104, 48);
  79. self.btnPlay.Name = "btnPlay";
  80. self.btnPlay.TabIndex = 3;
  81. self.btnPlay.Text = "&Play";
  82. self.btnPlay.Click += self.btnPlay_Click
  83. #
  84. # btnStop
  85. #
  86. self.btnStop.Enabled = False;
  87. self.btnStop.Location = Point(176, 48);
  88. self.btnStop.Name = "btnStop";
  89. self.btnStop.TabIndex = 4;
  90. self.btnStop.Text = "&Stop";
  91. self.btnStop.Click += self.btnStop_Click
  92. #
  93. # btnCancel
  94. #
  95. self.btnCancel.Location = Point(364, 48);
  96. self.btnCancel.Name = "btnCancel";
  97. self.btnCancel.TabIndex = 5;
  98. self.btnCancel.Text = "E&xit";
  99. self.btnCancel.Click += self.btnCancel_Click
  100. # Form
  101. self.AcceptButton = self.btnSoundfile;
  102. self.ClientSize = Size(470, 77)
  103. self.Controls.AddRange( Array[Control] ([ self.btnSoundfile, self.lblFilename, self.cbLoopCheck, self.btnPlay, self.btnStop, self.btnCancel ]) );
  104. self.Name = 'MainForm'
  105. self.Text = 'PlaySound'
  106. self.Load += self._Form1_Load
  107. self.ResumeLayout(False)
  108. def btnSoundfile_Click(self, sender, e):
  109. ofd = OpenFileDialog()
  110. ofd.InitialDirectory = self.PathSoundFile
  111. ofd.Filter= "Wave files(*.wav)|*.wav"
  112. # Stop the sound if it's already playing before you open the the dialog
  113. if (not self.ApplicationBuffer is None):
  114. self.ApplicationBuffer.Stop()
  115. if( DialogResult.Cancel == ofd.ShowDialog() ):
  116. return
  117. if(self.LoadSoundFile(ofd.FileName)):
  118. self.PathSoundFile = Path.GetDirectoryName(ofd.FileName)
  119. self.lblFilename.Text = Path.GetFileName(ofd.FileName)
  120. self.EnablePlayUI(True)
  121. else:
  122. self.lblFilename.Text = "Could not create sound buffer."
  123. self.EnablePlayUI(False);
  124. def LoadSoundFile(self, name):
  125. try:
  126. print name
  127. self.ApplicationBuffer = SecondaryBuffer(name, self.ApplicationDevice)
  128. except:
  129. return False;
  130. return True;
  131. def EnablePlayUI(self, enable):
  132. if (enable):
  133. self.cbLoopCheck.Enabled = True
  134. self.btnPlay.Enabled = True
  135. self.btnStop.Enabled = True
  136. else:
  137. self.cbLoopCheck.Enabled = False
  138. self.btnPlay.Enabled = False
  139. self.btnStop.Enabled = False
  140. def btnStop_Click(self, sender, e):
  141. if not None is self.ApplicationBuffer:
  142. self.ApplicationBuffer.Stop()
  143. def btnPlay_Click(self, sender, e):
  144. if not None is self.ApplicationBuffer:
  145. self.ApplicationBuffer.Play(0, BufferPlayFlags.Looping if self.cbLoopCheck.Checked else BufferPlayFlags.Default)
  146. def btnCancel_Click(self, sender, e):
  147. self.Close()
  148. if __name__ == '__main__':
  149. Application.EnableVisualStyles()
  150. Application.SetCompatibleTextRenderingDefault(False)
  151. frm = MyForm()
  152. Application.Run(frm)