Merge: Make the `do catch` great again
[nit.git] / lib / app / audio.nit
index de68bb9..87b510f 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# App audio abstraction
+# Services to load and play `Sound` and `Music` from the assets folder
 #
-# Once the application has started (after `App.setup`)
-# use `App.load_sound` to get a sound
-# then `Sound.play` to play it
+# Get a handle to a sound using `new Sound` or `new Music` at any time.
+# Call `load` at or after `App::on_create` or leave it to be loaded
+# on demand by the first call to `play`.
 module audio
 
 import app_base
+import core::error
 
-# Abstraction of a sound
-abstract class Sound
+# Platform variations
+import linux::audio is conditional(linux)
+import android::audio is conditional(android)
+import ios::audio is conditional(ios)
 
-       # Plays the sound
-       fun play is abstract
+# Abstraction of a playable Audio
+abstract class PlayableAudio
 
-       # Pauses the sound
-       fun pause is abstract
+       init do sounds.add self
 
-       # Resumes the sound
-       fun resume is abstract
+       # Path to the audio file in the assets folder
+       var path: String
+
+       # Last error on this sound, if any
+       fun error: nullable Error do return null
+
+       # Load this playable audio
+       fun load do end
+
+       # Play the sound
+       fun play do end
+
+       # Pause the sound
+       fun pause do end
+
+       # Resume the sound
+       fun resume do end
+end
+
+# Short sound
+class Sound
+       super PlayableAudio
+end
+
+# Long sound that can bee looped
+class Music
+       super PlayableAudio
 end
 
-redef class App
+redef class Sys
 
-       # Loads a sound from the assets of the app, returns `null` if the loading failed
-       fun load_sound(name: String): nullable Sound do return null
+       # All instantiated sounds
+       var sounds = new Array[PlayableAudio]
 end