Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / sound-openal-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-openal-1: real 3D sound with OpenAL
20
21
# You need to install OpenAL (http://openal.org) and PyOpenAL
22
# (http://home.gna.org/oomadness/en/pyopenal/index.html)
23
#
24
# You may need to stop artsd daemon too (typically for KDE user)
25
#
26
# The tuto draws a moving cube that play a 3D sound.
27
# Notice that the two sound modules soya.openal4soya and soya.sdl_mixer4soya have the
28
# same interface.
29
30
import os, os.path, sys
31
32
import soya, soya.cube as cube
33
34
soya.init()
35
36
# Define the data directories ; sounds are searched in sounds/ subdirectory.
37
38
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
39
40
scene = soya.World()
41
42
camera = soya.Camera(scene)
43
camera.y = 0.3
44
camera.z = 2.0
45
46
soya.set_root_widget(camera)
47
48
49
import soya.openal4soya as sound
50
51
# Inits the sound module, and pass to it the location of the listener
52
# (usually the camera).
53
54
sound.init(camera)
55
56
# You can set the volume level with set_volume
57
58
sound.set_volume(1000000000000000000000000.0)
59
60
# Create a cube.
61
62
import math
63
class MovingVolume(soya.Volume):
64
def __init__(self, parent, shape):
65
soya.Volume.__init__(self, parent, shape)
66
self.angle = 3.0
67
68
def advance_time(self, proportion):
69
self.angle += 0.04 * proportion
70
self.set_xyz(100.0 * math.cos(self.angle), 0.0, 100.0 * math.sin(self.angle))
71
72
cube = MovingVolume(scene, cube.Cube().shapify())
73
74
# The cube continuously play a sound
75
#
76
# The parameters for play are:
77
# - the sound file name (supported format: Wave, and Ogg Vorbis if PyVorbis is installed)
78
# - the sound position
79
# - the sound speed (a Vector, optional)
80
# the speed is automatically computed if omitted
81
# the speed is not used by SDL_mixer, since it doesn't provide Doppler effect.
82
# - 1 for looping (0 else)
83
# - 1 to load the sound asynchronously (if not already cached)
84
85
sound.play("test.wav", cube, None, 1)
86
87
88
light = soya.Light(scene)
89
light.set_xyz(1.5, 2.0, 2.2)
90
91
soya.Idler(scene).idle()
92