scope: refuse `&x` where x is a local variable
[nit.git] / lib / linux / audio.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2015 Romain Chanoir <romain.chanoir@viacesi.fr>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # `app::audio` implementation for GNU/Linux using SDL2 mixer
18 module audio
19
20 import app::audio
21 import sdl2::mixer
22 import linux
23
24 redef class PlayableAudio
25 redef var error = null
26
27 # Real file system path to this asset
28 private fun fs_path: String do return app.assets_dir / path
29
30 # Does `fs_path` exist?
31 private fun fs_path_exists: Bool
32 do
33 if not fs_path.file_exists then
34 error = new Error("Failed to load audio '{path}': file not found")
35 return false
36 end
37 return true
38 end
39 end
40
41 redef class Sound
42
43 private var native: nullable MixChunk = null
44
45 redef fun load
46 do
47 if not fs_path_exists then return
48
49 # SDL2 mixer load
50 var native = mix.load_wav(fs_path.to_cstring)
51 if native.address_is_null then
52 error = new Error("Failed to load sound '{path}': {mix.error}")
53 return
54 end
55
56 self.native = native
57 end
58
59 redef fun play do play_channel(-1, 0)
60
61 # Play this sound on `channel` (or any channel if -1) and return the channel
62 #
63 # Repeat the sound `loops` times, `loops == 0` plays it once,
64 # `loops == 1` plays it twice and `loops == -1` loops infinitely.
65 fun play_channel(channel, loops: Int): Int
66 do
67 var native = native
68
69 if native == null and error == null then
70 # Lazy load
71 load
72
73 # Auto print errors on lazy loading only
74 var error = error
75 if error != null then print_error error
76 end
77
78 # If there's an error, silently skip
79 if error != null then return -1
80 native = self.native
81 assert native != null
82
83 # Play on any available channel
84 return mix.play_channel(channel, native, loops)
85 end
86 end
87
88 redef class Music
89
90 private var native: nullable MixMusic = null
91
92 redef fun load
93 do
94 if not fs_path_exists then return
95
96 # SDL2 mixer load
97 var native = mix.load_mus(fs_path.to_cstring)
98 if native.address_is_null then
99 error = new Error("Failed to load music '{path}': {mix.error}")
100 return
101 end
102
103 self.native = native
104 end
105
106 redef fun play
107 do
108 var native = native
109
110 if native == null and error == null then
111 # Lazy load
112 load
113
114 # Auto print errors on lazy loading only
115 var error = error
116 if error != null then print_error error
117 end
118
119 # If there's an error, silently skip
120 if error != null then return
121 native = self.native
122 assert native != null
123
124 # Play looping
125 mix.play_music(native, -1)
126 end
127
128 redef fun pause do mix.pause_music
129
130 redef fun resume do mix.resume_music
131 end