SDL2 mixer with sample/sounds and music

C API documentation: https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html

Introduced classes

class Mix

sdl2 :: Mix

SDL2 mixer services
extern class MixChunk

sdl2 :: MixChunk

Chunk/sound handle, loaded by mix.load_wav
extern class MixFormat

sdl2 :: MixFormat

Sample format for mix.open_audio
extern class MixInitFlags

sdl2 :: MixInitFlags

Flags for mix.initialize
extern class MixMusic

sdl2 :: MixMusic

Music handle, loaded by mix.load_mus

Redefined classes

redef class Sys

sdl2 :: mixer $ Sys

The main class of the program.

All class definitions

class Mix

sdl2 $ Mix

SDL2 mixer services
extern class MixChunk

sdl2 $ MixChunk

Chunk/sound handle, loaded by mix.load_wav
extern class MixFormat

sdl2 $ MixFormat

Sample format for mix.open_audio
extern class MixInitFlags

sdl2 $ MixInitFlags

Flags for mix.initialize
extern class MixMusic

sdl2 $ MixMusic

Music handle, loaded by mix.load_mus
redef class Sys

sdl2 :: mixer $ Sys

The main class of the program.
package_diagram sdl2::mixer mixer sdl2::sdl2_base sdl2_base sdl2::mixer->sdl2::sdl2_base core core sdl2::sdl2_base->core ...core ... ...core->core gamnit::display_linux display_linux gamnit::display_linux->sdl2::mixer linux::audio audio linux::audio->sdl2::mixer gamnit::gamnit_linux gamnit_linux gamnit::gamnit_linux->gamnit::display_linux gamnit::gamnit_linux... ... gamnit::gamnit_linux...->gamnit::gamnit_linux a_star-m a_star-m a_star-m->linux::audio a_star-m... ... a_star-m...->a_star-m

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 array

core :: array

This module introduces the standard array structure.
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 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 sdl2_base

sdl2 :: sdl2_base

Basic SDL 2 features

Children

module audio

linux :: audio

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

gamnit :: display_linux

Gamnit display implementation for GNU/Linux using egl, sdl and x11

Descendants

module a_star-m

a_star-m

module camera_control_linux

gamnit :: camera_control_linux

Mouse wheel and middle mouse button to control camera
module gamnit_linux

gamnit :: gamnit_linux

Support services for Gamnit on GNU/Linux
# SDL2 mixer with sample/sounds and music
#
# C API documentation: https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html
module mixer is pkgconfig "SDL2_mixer"

import sdl2_base

`{
	#include "SDL2/SDL_mixer.h"
`}

redef class Sys
	# SDL2 mixer services
	var mix = new Mix is lazy
end

# SDL2 mixer services
class Mix
	# Initialize supports for loading formats in `flags`
	#
	# Returns the initialized supports.
	fun initialize(flags: MixInitFlags): MixInitFlags `{
		return Mix_Init(flags);
	`}

	# FLAC format support, for `initialize`
	fun flac: MixInitFlags `{ return MIX_INIT_FLAC; `}

	# MOD format support, for `initialize`
	fun mod: MixInitFlags `{ return MIX_INIT_MOD; `}

	# MP3 format support, for `initialize`
	fun mp3: MixInitFlags `{ return MIX_INIT_MP3; `}

	# OGG format support, for `initialize`
	fun ogg: MixInitFlags `{ return MIX_INIT_OGG; `}

	# Clean up all dynamically loaded library handles
	#
	# May be called after loading all the sounds.
	fun quit `{ Mix_Quit(); `}

	# Initialize and configure the mixer API
	#
	# Playback is configured with:
	# * The output sample `frequency`, in samples per second (Hz).
	# * The output sample `format`, may be `mix_default_format`.
	# * The number of sound `channels`, 1 for mono or 2 for stereo.
	# * The number of bytes used by output sample size as `chunk_size`.
	fun open_audio(frequency: Int, format: MixFormat, channels, chunk_size: Int): Bool `{
		return Mix_OpenAudio(frequency, format, channels, chunk_size) == 0;
	`}

	# Default frequency for `open_audio` (22050)
	fun default_frequency: Int `{ return MIX_DEFAULT_FREQUENCY; `}

	# Default format for `open_audio`
	fun default_format: MixFormat `{ return MIX_DEFAULT_FORMAT; `}
	# TODO other formats: AUDIO_U8, AUDIO_S8, etc.

	# Shutdown and cleanup the mixer API
	fun close_audio `{ Mix_CloseAudio(); `}

	# Last error produced by the mixer API
	fun error: CString `{ return (char*)Mix_GetError(); `}

	# ---
	# Chunks/sounds

	# Load `file` for use as a sample
	#
	# Returns a null pointer on error.
	#
	# Can load WAVE, AIFF, RIFF, OGG and VOC files.
	fun load_wav(file: CString): MixChunk `{ return Mix_LoadWAV(file); `}

	# Play `chunk` on `channel`
	#
	# If `channel == -1` the first unreserved channel is used.
	# The sound is repeated `loops` times, `loops == 0` plays it once,
	# `loops == 1` plays it twice and `loops == -1` loops infinitely.
	#
	# Returns the channel used, or `-1` on error.
	fun play_channel(channel: Int, chunk: MixChunk, loops: Int): Int `{
		return Mix_PlayChannel(channel, chunk, loops);
	`}

	# Play `chunk` on `channel`
	#
	# If `channel == -1` the first unreserved channel is used.
	# The sound is repeated `loops` times, `loops == 0` plays it once,
	# `loops == 1` plays it twice and `loops == -1` loops infinitely.
	# If `ticks != -1`, the sample plays for at most `ticks` milliseconds.
	fun play_channel_timed(channel: Int, chunk: MixChunk, loops, ticks: Int): Int `{
		return Mix_PlayChannelTimed(channel, chunk, loops, ticks);
	`}

	# Halt/stop `channel` playback
	#
	# If `channel == -1`, halt all channels.
	fun halt_channel(channel: Int) `{
		Mix_HaltChannel(channel);
	`}

	# Halt `channel` in `ticks` milliseconds and return the number of channels set to expire
	#
	# If `channel == -1`, halt all channels.
	fun expire_channel(channel, ticks: Int): Int `{
		return Mix_ExpireChannel(channel, ticks);
	`}

	# Reserve `num` channels from being used by `play_channel(-1...)`
	#
	# Returns the number of of channels reserved.
	fun reserve_channels(num: Int): Int `{
		return Mix_ReserveChannels(num);
	`}

	# Set the `volume` of `channel`, out of `mix.max_volume`
	#
	# If `channel == -1`, set the volume of all channels.
	#
	# Returns the current volume of the channel, or if `channel == -1` the average volume.
	fun volume(channel, volume: Int): Int `{
		return Mix_Volume(channel, volume);
	`}

	# Set the `volume` for `chunk`, out of `mix.max_volume`
	#
	# If `volume == -1`, only read the previous value.
	#
	# Returns the previous volume value.
	fun volume_chunk(chunk: MixChunk, volume: Int) `{
		Mix_VolumeChunk(chunk, volume);
	`}

	# Pause `channel`, or all playing channels if -1
	fun pause(channel: Int) `{
		Mix_Pause(channel);
	`}

	# Unpause `channel`, or all paused channels if -1
	fun resume(channel: Int) `{
		Mix_Resume(channel);
	`}

	# ---
	# Music

	# Load music from `file`
	#
	# Returns a null pointer on error.
	#
	# WAVE, MOD, MIDI, OGG, MP3, FLAC VOC files
	fun load_mus(file: CString): MixMusic `{ return Mix_LoadMUS(file); `}

	# Play `music` `loops` times
	#
	# The music is plays once if `loops == 1` and repeats infinitely if `loops == -1`.
	fun play_music(music: MixMusic, loops: Int): Bool `{
		return Mix_PlayMusic(music, loops) == 0;
	`}

	# Pause music playback
	fun pause_music `{ Mix_PauseMusic(); `}

	# Resume music playback
	fun resume_music `{ Mix_ResumeMusic(); `}

	# Set the music volume out of `mix.max_volume` and return the previous value
	#
	# Use `volume = -1` to only read the previous value.
	fun volume_music(volume: Int): Int `{
		return Mix_VolumeMusic(volume);
	`}

	# Maximum volume value for `volume_music` and `volume_chunk`
	fun max_volume: Int `{ return MIX_MAX_VOLUME; `}
end

# Chunk/sound handle, loaded by `mix.load_wav`
extern class MixChunk
	redef fun free `{ Mix_FreeChunk(self); `}
end

# Music handle, loaded by `mix.load_mus`
extern class MixMusic
	redef fun free `{ Mix_FreeMusic(self); `}
end

# ---
# Flags & enums

# Flags for `mix.initialize`
extern class MixInitFlags `{ int `}

	# Binary OR
	fun | (other: MixInitFlags): MixInitFlags `{ return self | other; `}

	private fun to_i: Int `{ return self; `}

	redef fun ==(o) do return o isa MixInitFlags and o.to_i == to_i
end

# Sample format for `mix.open_audio`
extern class MixFormat `{ int `}
end
lib/sdl2/mixer.nit:15,1--221,3