OpenTTD Source 20241224-master-gee860a5c8e
mixer.cpp File Reference

Mixing of sound samples. More...

#include "stdafx.h"
#include <mutex>
#include <atomic>
#include "core/math_func.hpp"
#include "framerate_type.h"
#include "mixer.h"
#include "settings_type.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  MixerChannel
 

Typedefs

using MixerChannelMask = uint8_t
 Type representing a bitmask of mixer channels.
 

Functions

template<typename T >
static int RateConversion (T *b, int frac_pos)
 Perform the rate conversion between the input and output.
 
template<typename T >
static void mix_int16 (MixerChannel *sc, int16_t *buffer, uint samples, uint8_t effect_vol)
 
static void MxCloseChannel (uint8_t channel_index)
 
void MxCloseAllChannels ()
 Close all mixer channels.
 
void MxMixSamples (void *buffer, uint samples)
 
MixerChannelMxAllocateChannel ()
 
void MxSetChannelRawSrc (MixerChannel *mc, const std::shared_ptr< std::vector< uint8_t > > &mem, uint rate, bool is16bit)
 
void MxSetChannelVolume (MixerChannel *mc, uint volume, float pan)
 Set volume and pan parameters for a sound.
 
void MxActivateChannel (MixerChannel *mc)
 
uint32_t MxSetMusicSource (MxStreamCallback music_callback)
 Set source of PCM music.
 
bool MxInitialize (uint rate)
 
void SetEffectVolume (uint8_t volume)
 

Variables

static std::atomic< MixerChannelMask_active_channels
 
static std::atomic< MixerChannelMask_stop_channels
 
static MixerChannel _channels [8]
 
static uint32_t _play_rate = 11025
 
static uint32_t _max_size = UINT_MAX
 
static MxStreamCallback _music_stream = nullptr
 
static std::mutex _music_stream_mutex
 
static std::atomic< uint8_t > _effect_vol
 
static const int MAX_VOLUME = 32767
 The theoretical maximum volume for a single sound sample.
 

Detailed Description

Mixing of sound samples.

Definition in file mixer.cpp.

Typedef Documentation

◆ MixerChannelMask

using MixerChannelMask = uint8_t

Type representing a bitmask of mixer channels.

Definition at line 37 of file mixer.cpp.

Function Documentation

◆ mix_int16()

template<typename T >
static void mix_int16 ( MixerChannel sc,
int16_t *  buffer,
uint  samples,
uint8_t  effect_vol 
)
static

Definition at line 70 of file mixer.cpp.

◆ MxActivateChannel()

void MxActivateChannel ( MixerChannel mc)

Definition at line 222 of file mixer.cpp.

◆ MxAllocateChannel()

MixerChannel * MxAllocateChannel ( )

Definition at line 170 of file mixer.cpp.

◆ MxCloseAllChannels()

void MxCloseAllChannels ( )

Close all mixer channels.

This signals to the mixer that each channel should be closed even if it has not played all remaining samples. This is safe (and designed) to be called from the main thread.

Definition at line 119 of file mixer.cpp.

Referenced by ChangeSoundSet().

◆ MxCloseChannel()

static void MxCloseChannel ( uint8_t  channel_index)
static

Definition at line 109 of file mixer.cpp.

◆ MxInitialize()

bool MxInitialize ( uint  rate)

Definition at line 242 of file mixer.cpp.

◆ MxMixSamples()

void MxMixSamples ( void *  buffer,
uint  samples 
)

Definition at line 124 of file mixer.cpp.

◆ MxSetChannelRawSrc()

void MxSetChannelRawSrc ( MixerChannel mc,
const std::shared_ptr< std::vector< uint8_t > > &  mem,
uint  rate,
bool  is16bit 
)

Definition at line 183 of file mixer.cpp.

◆ MxSetChannelVolume()

void MxSetChannelVolume ( MixerChannel mc,
uint  volume,
float  pan 
)

Set volume and pan parameters for a sound.

Parameters
mcMixerChannel to set
volumeVolume level for sound, range is 0..16384
panPan position for sound, range is 0..1

Definition at line 213 of file mixer.cpp.

◆ MxSetMusicSource()

uint32_t MxSetMusicSource ( MxStreamCallback  music_callback)

Set source of PCM music.

Parameters
music_callbackFunction that will be called to fill sample buffers with music data.
Returns
Sample rate of mixer, which the buffers supplied to the callback must be rendered at.

Definition at line 234 of file mixer.cpp.

References lock.

Referenced by MusicDriver_FluidSynth::Start(), and MusicDriver_FluidSynth::Stop().

◆ RateConversion()

template<typename T >
static int RateConversion ( T *  b,
int  frac_pos 
)
static

Perform the rate conversion between the input and output.

Parameters
bthe buffer to read the data from
frac_posthe position from the begin of the buffer till the next element
Template Parameters
Tthe size of the buffer (8 or 16 bits)
Returns
the converted value.

Definition at line 64 of file mixer.cpp.

◆ SetEffectVolume()

void SetEffectVolume ( uint8_t  volume)

Definition at line 251 of file mixer.cpp.

Variable Documentation

◆ _active_channels

std::atomic<MixerChannelMask> _active_channels
static

Definition at line 39 of file mixer.cpp.

◆ _channels

MixerChannel _channels[8]
static

Definition at line 41 of file mixer.cpp.

◆ _effect_vol

std::atomic<uint8_t> _effect_vol
static

Definition at line 46 of file mixer.cpp.

◆ _max_size

uint32_t _max_size = UINT_MAX
static

Definition at line 43 of file mixer.cpp.

◆ _music_stream

MxStreamCallback _music_stream = nullptr
static

Definition at line 44 of file mixer.cpp.

◆ _music_stream_mutex

std::mutex _music_stream_mutex
static

Definition at line 45 of file mixer.cpp.

◆ _play_rate

uint32_t _play_rate = 11025
static

Definition at line 42 of file mixer.cpp.

◆ _stop_channels

std::atomic<MixerChannelMask> _stop_channels
static

Definition at line 40 of file mixer.cpp.

◆ MAX_VOLUME

const int MAX_VOLUME = 32767
static

The theoretical maximum volume for a single sound sample.

Multiple sound samples should not exceed this limit as it will sound too loud. It also stops overflowing when too many sounds are played at the same time, which causes an even worse sound quality.

Definition at line 54 of file mixer.cpp.