src/mixer.cpp
branchcpp_gui
changeset 6285 187e3ef04cc9
parent 5838 9c3129cb019b
child 6298 c30fe89622df
equal deleted inserted replaced
6284:45d0233e7d79 6285:187e3ef04cc9
     1 /* $Id$ */
     1 /* $Id$ */
       
     2 
       
     3 /** @file mixer.cpp*/
     2 
     4 
     3 #include "stdafx.h"
     5 #include "stdafx.h"
     4 #include "openttd.h"
     6 #include "openttd.h"
     5 #include "mixer.h"
     7 #include "mixer.h"
     6 
     8 
     7 struct MixerChannel {
     9 struct MixerChannel {
     8 	bool active;
    10 	bool active;
     9 
    11 
    10 	// pointer to allocated buffer memory
    12 	/* pointer to allocated buffer memory */
    11 	int8 *memory;
    13 	int8 *memory;
    12 
    14 
    13 	// current position in memory
    15 	/* current position in memory */
    14 	uint32 pos;
    16 	uint32 pos;
    15 	uint32 frac_pos;
    17 	uint32 frac_pos;
    16 	uint32 frac_speed;
    18 	uint32 frac_speed;
    17 	uint32 samples_left;
    19 	uint32 samples_left;
    18 
    20 
    19 	// Mixing volume
    21 	/* Mixing volume */
    20 	uint volume_left;
    22 	uint volume_left;
    21 	uint volume_right;
    23 	uint volume_right;
    22 
    24 
    23 	uint flags;
    25 	uint flags;
    24 };
    26 };
    44 	frac_speed = sc->frac_speed;
    46 	frac_speed = sc->frac_speed;
    45 	volume_left = sc->volume_left;
    47 	volume_left = sc->volume_left;
    46 	volume_right = sc->volume_right;
    48 	volume_right = sc->volume_right;
    47 
    49 
    48 	if (frac_speed == 0x10000) {
    50 	if (frac_speed == 0x10000) {
    49 		// Special case when frac_speed is 0x10000
    51 		/* Special case when frac_speed is 0x10000 */
    50 		do {
    52 		do {
    51 			buffer[0] += *b * volume_left >> 8;
    53 			buffer[0] += *b * volume_left >> 8;
    52 			buffer[1] += *b * volume_right >> 8;
    54 			buffer[1] += *b * volume_right >> 8;
    53 			b++;
    55 			b++;
    54 			buffer += 2;
    56 			buffer += 2;
    77 
    79 
    78 void MxMixSamples(void *buffer, uint samples)
    80 void MxMixSamples(void *buffer, uint samples)
    79 {
    81 {
    80 	MixerChannel *mc;
    82 	MixerChannel *mc;
    81 
    83 
    82 	// Clear the buffer
    84 	/* Clear the buffer */
    83 	memset(buffer, 0, sizeof(int16) * 2 * samples);
    85 	memset(buffer, 0, sizeof(int16) * 2 * samples);
    84 
    86 
    85 	// Mix each channel
    87 	/* Mix each channel */
    86 	for (mc = _channels; mc != endof(_channels); mc++) {
    88 	for (mc = _channels; mc != endof(_channels); mc++) {
    87 		if (mc->active) {
    89 		if (mc->active) {
    88 			mix_int8_to_int16(mc, (int16*)buffer, samples);
    90 			mix_int8_to_int16(mc, (int16*)buffer, samples);
    89 			if (mc->samples_left == 0) MxCloseChannel(mc);
    91 			if (mc->samples_left == 0) MxCloseChannel(mc);
    90 		}
    92 		}
   109 	mc->frac_pos = 0;
   111 	mc->frac_pos = 0;
   110 	mc->pos = 0;
   112 	mc->pos = 0;
   111 
   113 
   112 	mc->frac_speed = (rate << 16) / _play_rate;
   114 	mc->frac_speed = (rate << 16) / _play_rate;
   113 
   115 
   114 	// adjust the magnitude to prevent overflow
   116 	/* adjust the magnitude to prevent overflow */
   115 	while (size & 0xFFFF0000) {
   117 	while (size & 0xFFFF0000) {
   116 		size >>= 1;
   118 		size >>= 1;
   117 		rate = (rate >> 1) + 1;
   119 		rate = (rate >> 1) + 1;
   118 	}
   120 	}
   119 
   121