summaryrefslogtreecommitdiff
path: root/libsidplay2/sidplay-libs-2.1.0/libsidplay/src/mixer.cpp
blob: c7703dc1d03886bdc835e3386861d901c9621c82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***************************************************************************
                          mixer.cpp  -  Sids Mixer Routines
                             -------------------
    begin                : Sun Jul 9 2000
    copyright            : (C) 2000 by Simon White
    email                : s_a_white@email.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/***************************************************************************
 *  $Log: mixer.cpp,v $
 *  Revision 1.10  2002/01/29 21:50:33  s_a_white
 *  Auto switching to a better emulation mode.  m_tuneInfo reloaded after a
 *  config.  Initial code added to support more than two sids.
 *
 *  Revision 1.9  2001/12/13 08:28:08  s_a_white
 *  Added namespace support to fix problems with xsidplay.
 *
 *  Revision 1.8  2001/11/16 19:25:33  s_a_white
 *  Removed m_context as where getting mixed with parent class.
 *
 *  Revision 1.7  2001/10/02 18:29:32  s_a_white
 *  Corrected fixed point maths overflow caused by fastforwarding.
 *
 *  Revision 1.6  2001/09/17 19:02:38  s_a_white
 *  Now uses fixed point maths for sample output and rtc.
 *
 *  Revision 1.5  2001/07/25 17:02:37  s_a_white
 *  Support for new configuration interface.
 *
 *  Revision 1.4  2001/07/14 12:47:39  s_a_white
 *  Mixer routines simplified.  Added new and more efficient method of
 *  determining when an output samples is required.
 *
 *  Revision 1.3  2001/03/01 23:46:37  s_a_white
 *  Support for sample mode to be selected at runtime.
 *
 *  Revision 1.2  2000/12/12 22:50:15  s_a_white
 *  Bug Fix #122033.
 *
 ***************************************************************************/

#include "player.h"
#include "sidendian.h"

const int_least32_t VOLUME_MAX = 255;

SIDPLAY2_NAMESPACE_START

void Player::mixerReset (void)
{   // Fixed point 16.16
    m_sampleClock  = m_samplePeriod & 0x0FFFF;
    // Schedule next sample event
    (context ()).schedule (&mixerEvent,
        m_samplePeriod >> 24);
}

void Player::mixer (void)
{   // Fixed point 16.16
    event_clock_t cycles;
    char   *buf    = m_sampleBuffer + m_sampleIndex;
    m_sampleClock += m_samplePeriod;
    cycles         = m_sampleClock >> 16;
    m_sampleClock &= 0x0FFFF;
    m_sampleIndex += (this->*output) (buf);
 
    // Schedule next sample event
    (context ()).schedule (&mixerEvent, cycles);

    // Filled buffer
    if (m_sampleIndex >= m_sampleCount)
        m_running = false;
}


//-------------------------------------------------------------------------
// Generic sound output generation routines
//-------------------------------------------------------------------------
inline
int_least32_t Player::monoOutGenericLeftIn (uint_least8_t bits)
{
    return sid[0]->output (bits) * m_leftVolume / VOLUME_MAX;
}

inline
int_least32_t Player::monoOutGenericStereoIn (uint_least8_t bits)
{
    // Convert to mono
    return ((sid[0]->output (bits) * m_leftVolume) +
        (sid[1]->output (bits) * m_rightVolume)) / (VOLUME_MAX * 2);
}

inline
int_least32_t Player::monoOutGenericRightIn (uint_least8_t bits)
{
    return sid[1]->output (bits) * m_rightVolume / VOLUME_MAX;
}


//-------------------------------------------------------------------------
// 8 bit sound output generation routines
//-------------------------------------------------------------------------
uint_least32_t Player::monoOut8MonoIn (char *buffer)
{
    *buffer = (char) monoOutGenericLeftIn (8) ^ '\x80';
    return sizeof (char);
}

uint_least32_t Player::monoOut8StereoIn (char *buffer)
{
    *buffer = (char) monoOutGenericStereoIn (8) ^ '\x80';
    return sizeof (char);
}

uint_least32_t Player::monoOut8StereoRIn (char *buffer)
{
    *buffer = (char) monoOutGenericRightIn (8) ^ '\x80';
    return sizeof (char);
}

uint_least32_t Player::stereoOut8MonoIn (char *buffer)
{
    char sample = (char) monoOutGenericLeftIn (8) ^ '\x80';
    buffer[0] = sample; 
    buffer[1] = sample; 
    return (2 * sizeof (char));
}

uint_least32_t Player::stereoOut8StereoIn (char *buffer)
{
    buffer[0] = (char) monoOutGenericLeftIn  (8) ^ '\x80';
    buffer[1] = (char) monoOutGenericRightIn (8) ^ '\x80';
    return (2 * sizeof (char));
}

//-------------------------------------------------------------------------
// 16 bit sound output generation routines
//-------------------------------------------------------------------------
uint_least32_t Player::monoOut16MonoIn (char *buffer)
{
    endian_16 (buffer, (uint_least16_t) monoOutGenericLeftIn (16));
    return sizeof (uint_least16_t);
}

uint_least32_t Player::monoOut16StereoIn (char *buffer)
{
    endian_16 (buffer, (uint_least16_t) monoOutGenericStereoIn (16));
    return sizeof (uint_least16_t);
}

uint_least32_t Player::monoOut16StereoRIn (char *buffer)
{
    endian_16 (buffer, (uint_least16_t) monoOutGenericRightIn (16));
    return sizeof (uint_least16_t);
}

uint_least32_t Player::stereoOut16MonoIn (char *buffer)
{
    uint_least16_t sample = (uint_least16_t) monoOutGenericLeftIn (16);
    endian_16 (buffer, sample);
    endian_16 (buffer + sizeof(uint_least16_t), sample);
    return (2 * sizeof (uint_least16_t));
}

uint_least32_t Player::stereoOut16StereoIn (char *buffer)
{
    endian_16 (buffer, (uint_least16_t) monoOutGenericLeftIn  (16));
    endian_16 (buffer + sizeof(uint_least16_t),
               (uint_least16_t) monoOutGenericRightIn (16));
    return (2 * sizeof (uint_least16_t));
}

SIDPLAY2_NAMESPACE_STOP