app::audio implementation for GNU/Linux using SDL2 mixer

Redefined classes

redef class Music

linux :: audio $ Music

Long sound that can bee looped
redef abstract class PlayableAudio

linux :: audio $ PlayableAudio

Abstraction of a playable Audio
redef class Sound

linux :: audio $ Sound

Short sound

All class definitions

redef class Music

linux :: audio $ Music

Long sound that can bee looped
redef abstract class PlayableAudio

linux :: audio $ PlayableAudio

Abstraction of a playable Audio
redef class Sound

linux :: audio $ Sound

Short sound
package_diagram linux::audio audio app::audio audio linux::audio->app::audio sdl2::mixer mixer linux::audio->sdl2::mixer linux linux linux::audio->linux app::app_base app_base app::audio->app::app_base sdl2::sdl2_base sdl2_base sdl2::mixer->sdl2::sdl2_base app app linux->app sdl2 sdl2 linux->sdl2 xdg_basedir xdg_basedir linux->xdg_basedir sqlite3 sqlite3 linux->sqlite3 json json linux->json curl curl linux->curl gtk gtk linux->gtk ...app::app_base ... ...app::app_base->app::app_base ...sdl2::sdl2_base ... ...sdl2::sdl2_base->sdl2::sdl2_base ...app ... ...app->app ...sdl2 ... ...sdl2->sdl2 ...xdg_basedir ... ...xdg_basedir->xdg_basedir ...sqlite3 ... ...sqlite3->sqlite3 ...json ... ...json->json ...curl ... ...curl->curl ...gtk ... ...gtk->gtk a_star-m a_star-m a_star-m->linux::audio

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module app

app :: app

app.nit is a framework to create cross-platform applications
module app_base

app :: app_base

Base of the app.nit framework, defines App
module array

core :: array

This module introduces the standard array structure.
module assets

app :: assets

Portable services to load resources from the assets folder
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sdl2_base

sdl2 :: sdl2_base

Basic SDL 2 features
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module audio

app :: audio

Services to load and play Sound and Music from the assets folder
module linux

linux :: linux

Implementation of app.nit for the Linux platform
module mixer

sdl2 :: mixer

SDL2 mixer with sample/sounds and music

Children

module a_star-m

a_star-m

# `app::audio` implementation for GNU/Linux using SDL2 mixer
module audio

import app::audio
import sdl2::mixer
import linux

redef class PlayableAudio
	redef var error = null

	# Real file system path to this asset
	private fun fs_path: String do return app.assets_dir / path

	# Does `fs_path` exist?
	private fun fs_path_exists: Bool
	do
		if not fs_path.file_exists then
			error = new Error("Failed to load audio '{path}': file not found")
			return false
		end
		return true
	end
end

redef class Sound

	private var native: nullable MixChunk = null

	redef fun load
	do
		if not fs_path_exists then return

		# SDL2 mixer load
		var native = mix.load_wav(fs_path.to_cstring)
		if native.address_is_null then
			error = new Error("Failed to load sound '{path}': {mix.error}")
			return
		end

		self.native = native
	end

	redef fun play do play_channel(-1, 0)

	# Play this sound on `channel` (or any channel if -1) and return the channel
	#
	# Repeat the sound `loops` times, `loops == 0` plays it once,
	# `loops == 1` plays it twice and `loops == -1` loops infinitely.
	fun play_channel(channel, loops: Int): Int
	do
		var native = native

		if native == null and error == null then
			# Lazy load
			load

			# Auto print errors on lazy loading only
			var error = error
			if error != null then print_error error
		end

		# If there's an error, silently skip
		if error != null then return -1
		native = self.native
		assert native != null

		# Play on any available channel
		return mix.play_channel(channel, native, loops)
	end
end

redef class Music

	private var native: nullable MixMusic = null

	redef fun load
	do
		if not fs_path_exists then return

		# SDL2 mixer load
		var native = mix.load_mus(fs_path.to_cstring)
		if native.address_is_null then
			error = new Error("Failed to load music '{path}': {mix.error}")
			return
		end

		self.native = native
	end

	redef fun play
	do
		var native = native

		if native == null and error == null then
			# Lazy load
			load

			# Auto print errors on lazy loading only
			var error = error
			if error != null then print_error error
		end

		# If there's an error, silently skip
		if error != null then return
		native = self.native
		assert native != null

		# Play looping
		mix.play_music(native, -1)
	end

	redef fun pause do mix.pause_music

	redef fun resume do mix.resume_music
end
lib/linux/audio.nit:17,1--131,3