gamnit: implement app::audio for desktop using SDL2 mixer
[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
60 do
61 var native = native
62
63 if native == null and error == null then
64 # Lazy load
65 load
66
67 # Auto print errors on lazy loading only
68 var error = error
69 if error != null then print_error error
70 end
71
72 # If there's an error, silently skip
73 if error != null then return
74 native = self.native
75 assert native != null
76
77 # Play on any available channel
78 mix.play_channel(-1, native, 0)
79 end
80 end
81
82 redef class Music
83
84 private var native: nullable MixMusic = null
85
86 redef fun load
87 do
88 if not fs_path_exists then return
89
90 # SDL2 mixer load
91 var native = mix.load_mus(fs_path.to_cstring)
92 if native.address_is_null then
93 error = new Error("Failed to load music '{path}': {mix.error}")
94 return
95 end
96
97 self.native = native
98 end
99
100 redef fun play
101 do
102 var native = native
103
104 if native == null and error == null then
105 # Lazy load
106 load
107
108 # Auto print errors on lazy loading only
109 var error = error
110 if error != null then print_error error
111 end
112
113 # If there's an error, silently skip
114 if error != null then return
115 native = self.native
116 assert native != null
117
118 # Play looping
119 mix.play_music(native, -1)
120 end
121
122 redef fun pause do mix.pause_music
123
124 redef fun resume do mix.resume_music
125 end