Debian Codegraph @

examples/media/soundspace/reader.py

raw symbols SCIP
1 # ----------------------------------------------------------------------------
2 # pyglet
3 # Copyright (c) 2006-2008 Alex Holkner
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 # * Neither the name of pyglet nor the names of its
17 # contributors may be used to endorse or promote products
18 # derived from this software without specific prior written
19 # permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 # ----------------------------------------------------------------------------
34 # $Id$
35
36 import os
37
38 from pyglet import media
39
40
41 class ReaderException(Exception):
42 pass
43
44
45 class PlayerReader:
46 def __init__(self, player):
47 self.player = player
48
49 def line(self, line, lineno):
50 parts = line.split()
51 if parts[0] == 'position':
52 if len(parts) < 4:
53 raise ReaderException('Invalid position line %d' % lineno)
54 self.player.position = tuple([float(x) for x in parts[1:]])
55 if parts[0] == 'cone_orientation':
56 if len(parts) < 4:
57 raise ReaderException('Invalid orientation line %d' % lineno)
58 self.player.cone_orientation = tuple([float(x) for x in parts[1:]])
59 elif parts[0] == 'outer_cone_angle':
60 if len(parts) < 2:
61 raise ReaderException('Invalid angle line %d' % lineno)
62 self.player.cone_outer_angle = float(parts[1])
63 elif parts[0] == 'inner_cone_angle':
64 if len(parts) < 2:
65 raise ReaderException('Invalid angle line %d' % lineno)
66 self.player.cone_inner_angle = float(parts[1])
67 elif parts[0] == 'label':
68 if len(parts) < 2:
69 raise ReaderException('Invalid label line %d' % lineno)
70 self.player.label = parts[1]
71
72
73 class SpaceReader:
74 def __init__(self, space):
75 self.basedir = ''
76 self.space = space
77
78 def read(self, file):
79 if not hasattr(file, 'read'):
80 self.basedir = os.path.dirname(file)
81 with open(file) as f:
82 file = f.readlines()
83 elif hasattr(file, 'name'):
84 self.basedir = os.path.dirname(file.name)
85 reader = None
86 player = None
87 lineno = 0
88 for line in file:
89 lineno += 1
90
91 if not line.strip() or line.startswith('#'):
92 continue
93
94 if line.startswith(' '):
95 if not reader:
96 raise ReaderException('Unexpected indented block line %d' % lineno)
97 reader.line(line, lineno)
98 else:
99 reader = None
100 if player is not None:
101 self.space.add_player(player)
102 player = None
103
104 parts = line.split()
105 if parts[0] == 'loop':
106 if len(parts) < 2:
107 raise ReaderException('No loop filename line %d' % lineno)
108 player = media.Player()
109 player.loop = True
110 player.queue(self.source(parts[1], streaming=False))
111 reader = PlayerReader(player)
112
113 if player is not None:
114 self.space.add_player(player)
115
116 def source(self, filename, **kwargs):
117 filename = os.path.join(self.basedir, filename)
118 return media.load(filename, **kwargs)