1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2015 Romain Chanoir <romain.chanoir@viacesi.fr>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # `app::audio` implementation for GNU/Linux using SDL2 mixer
24 redef class PlayableAudio
25 redef var error
= null
27 # Real file system path to this asset
28 private fun fs_path
: String do return app
.assets_dir
/ path
30 # Does `fs_path` exist?
31 private fun fs_path_exists
: Bool
33 if not fs_path
.file_exists
then
34 error
= new Error("Failed to load audio '{path}': file not found")
43 private var native
: nullable MixChunk = null
47 if not fs_path_exists
then return
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}")
59 redef fun play
do play_channel
(-1, 0)
61 # Play this sound on `channel` (or any channel if -1) and return the channel
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
69 if native
== null and error
== null then
73 # Auto print errors on lazy loading only
75 if error
!= null then print_error error
78 # If there's an error, silently skip
79 if error
!= null then return -1
83 # Play on any available channel
84 return mix
.play_channel
(channel
, native
, loops
)
90 private var native
: nullable MixMusic = null
94 if not fs_path_exists
then return
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}")
110 if native
== null and error
== null then
114 # Auto print errors on lazy loading only
116 if error
!= null then print_error error
119 # If there's an error, silently skip
120 if error
!= null then return
122 assert native
!= null
125 mix
.play_music
(native
, -1)
128 redef fun pause
do mix
.pause_music
130 redef fun resume
do mix
.resume_music