Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / sound-sdl_mixer-1.pyOpen in CoCalc
Author: William A. Stein
1
# Soya 3D tutorial
2
# Copyright (C) 2005 Jean-Baptiste LAMY
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
# sound-sdl_mixer-1: pseudo-3D sound with SDL_mixer
20
21
# You need to install PySDL_Mixer (http://gna.org/projects/pysdlmixer/)
22
#
23
# You may need to stop artsd daemon too (typically for KDE user)
24
#
25
# The tuto draws a moving cube that play a 3D sound.
26
# Notice that the two sound modules soya.openal4soya and soya.sdl_mixer4soya have the
27
# same interface.
28
29
import os, os.path, sys
30
31
import soya, soya.cube as cube
32
33
soya.init()
34
35
# Define the data directories ; sounds are searched in sounds/ subdirectory.
36
37
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
38
39
scene = soya.World()
40
41
camera = soya.Camera(scene)
42
camera.y = 0.3
43
camera.z = 2.0
44
45
soya.set_root_widget(camera)
46
47
48
import soya.sdl_mixer4soya as sound
49
50
# Inits the sound module, and pass to it the location of the listener
51
# (usually the camera).
52
53
sound.init(camera)
54
55
# Create a cube.
56
57
import math
58
class MovingVolume(soya.Volume):
59
def __init__(self, parent, shape):
60
soya.Volume.__init__(self, parent, shape)
61
self.angle = 3.0
62
63
def advance_time(self, proportion):
64
self.angle += 0.04 * proportion
65
self.set_xyz(100.0 * math.cos(self.angle), 0.0, 100.0 * math.sin(self.angle))
66
67
cube = MovingVolume(scene, cube.Cube().shapify())
68
69
# The cube continuously play a sound
70
#
71
# The parameters for play are:
72
# - the sound file name (supported format: Wave, Ogg Vorbis, MP3,...)
73
# - the sound position
74
# - the sound speed (a Vector, IGNORED (it is present for compatibility with soya.openal4soya))
75
# the speed is automatically computed if omitted
76
# the speed is not used by SDL_mixer, since it doesn't provide Doppler effect.
77
# - 1 for looping (0 else)
78
# - 1 to load the sound asynchronously (if not already cached)
79
80
sound.play("test.wav", cube, None, 1)
81
82
83
light = soya.Light(scene)
84
light.set_xyz(1.5, 2.0, 2.2)
85
86
soya.Idler(scene).idle()
87