File: sound.py

package info (click to toggle)
python-sfml 1.5.1.is.1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,456 kB
  • ctags: 1,585
  • sloc: python: 5,747; cpp: 285; makefile: 147
file content (54 lines) | stat: -rw-r--r-- 1,507 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# pySFML - Python bindings for SFML
# Copyright 2012-2013, Jonathan De Wachter <dewachter.jonathan@gmail.com>
#
# This software is released under the LGPLv3 license.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sfml as sf

def play_sound():
	# load a sound buffer from a wav file
	buffer = sf.SoundBuffer.from_file("data/canary.wav")

	# display sound informations
	print("canary.wav:")
	print("{0} seconds".format(buffer.duration))
	print("{0} samples / sec".format(buffer.sample_rate))
	print("{0} channels".format(buffer.channel_count))

	# create a sound instance and play it
	sound = sf.Sound(buffer)
	sound.play();

	# loop while the sound is playing
	while sound.status == sf.Sound.PLAYING:
		# leave some CPU time for other processes
		sf.sleep(sf.milliseconds(100))

def play_music():
	# load an ogg music file
	music = sf.Music.from_file("data/orchestral.ogg")

	# display music informations
	print("orchestral.ogg:")
	print("{0} seconds".format(music.duration))
	print("{0} samples / sec".format(music.sample_rate))
	print("{0} channels".format(music.channel_count))

	# play it
	music.play();

	# loop while the music is playing
	while music.status == sf.Music.PLAYING:
		# leave some CPU time for other processes
		sf.sleep(sf.milliseconds(100))

if __name__ == "__main__":
	play_sound()
	play_music()

	input("Press enter to exit...")