~ubuntu-branches/ubuntu/wily/zaz/wily

« back to all changes in this revision

Viewing changes to src/audiobuffer.cpp

  • Committer: Package Import Robot
  • Author(s): Miriam Ruiz
  • Date: 2011-11-16 02:28:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111116022810-d3tehh974q280vit
Tags: 1.0.0~dfsg1-1
* New Upstream Release
* Upgraded Standards-Version from 3.8.3 to 3.9.2
* New homepage: http://phuzzboxmedia.com/index.php/games/open-sourced-zaz
* Refreshed patches
* Moved package to DebSrc 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
namespace Scenes
22
22
{
23
 
    AudioBuffer::AudioBuffer()
24
 
    {
25
 
        Reset();
26
 
    }
27
 
 
28
 
    void AudioBuffer::Reset()
29
 
    {
30
 
        bufflen = 0;
31
 
        buff = 0;
32
 
        playing = false;
33
 
        sample = 0;
34
 
    }
35
 
 
36
 
    void AudioBuffer::Play(Sample *smpl, int vol, int pan, bool loop)
37
 
    {
38
 
        if (vol > 100)
39
 
            vol = 100;
40
 
 
41
 
        if (vol < 0)
42
 
            vol = 0;
43
 
 
44
 
        if (pan > 100)
45
 
            pan = 100;
46
 
 
47
 
        if (pan < -100)
48
 
            pan = -100;
49
 
 
50
 
        sample = smpl;
51
 
        this->vol = vol;
52
 
        this->pan = pan;
53
 
        this->loop = loop;
54
 
        streaming = sample->getStreaming();
55
 
 
56
 
        if (streaming)
57
 
            sample->Restart();
58
 
 
59
 
        // if not streaming sample - preload & premix the whole thing
60
 
        if (!streaming)
61
 
        {
62
 
            offset = 0;
63
 
            length = sample->getLength();
64
 
 
65
 
            if (sample->getNumChannels() == 1)
66
 
                length *= 2;
67
 
 
68
 
            if (buff)
69
 
                free(buff);
70
 
 
71
 
            buff = (Sint16*)malloc(length * 4);
72
 
            bufflen = length;
73
 
 
74
 
            PanAndMix();
75
 
        }
76
 
 
77
 
        playing = true;
78
 
    };
79
 
 
80
 
    uint AudioBuffer::PanAndMix(uint req_lenth)
81
 
    {
82
 
        uint rl;
83
 
        Sint16 *tb = sample->getSampleData(req_lenth, rl);
84
 
 
85
 
        double v1 = (double)vol / 100;
86
 
        double v2 = (double)vol / 100;
87
 
 
88
 
        if (pan != 0)
89
 
        {
90
 
            if (pan > 0)
91
 
                v1 = ((double)(vol - pan)) / 100;
92
 
 
93
 
            if (v1 < 0)
94
 
                v1 = 0;
95
 
 
96
 
            if (pan < 0)
97
 
                v2 = ((double)(vol + pan)) / 100;
98
 
 
99
 
            if (v2 < 0)
100
 
                v2 = 0;
101
 
 
102
 
        }
103
 
 
104
 
        bool mixasis = false;
105
 
 
106
 
        if ((vol == 100) && (pan == 0))
107
 
            mixasis = true;
108
 
 
109
 
        Sint16 *lc = tb;
110
 
        Sint16 *rc = tb + 1;
111
 
        uint inc = 2;
 
23
AudioBuffer::AudioBuffer()
 
24
{
 
25
    bufflen = 0;
 
26
    buff = 0;
 
27
    playing = false;
 
28
    sample = 0;
 
29
}
 
30
 
 
31
 
 
32
void AudioBuffer::Play(Sample *smpl, int vol, int pan, bool loop)
 
33
{
 
34
    if (vol > 100)
 
35
        vol = 100;
 
36
 
 
37
    if (vol < 0)
 
38
        vol = 0;
 
39
 
 
40
    if (pan > 100)
 
41
        pan = 100;
 
42
 
 
43
    if (pan < -100)
 
44
        pan = -100;
 
45
 
 
46
    sample = smpl;
 
47
    this->vol = vol;
 
48
    this->pan = pan;
 
49
    this->loop = loop;
 
50
    streaming = sample->getStreaming();
 
51
 
 
52
    if (streaming)
 
53
        sample->Restart();
 
54
 
 
55
    // if not streaming sample - preload & premix the whole thing
 
56
    if (!streaming)
 
57
    {
 
58
        offset = 0;
 
59
        length = sample->getLength();
112
60
 
113
61
        if (sample->getNumChannels() == 1)
114
 
        {
115
 
            rc = lc;
116
 
            inc = 1;
117
 
            rl*=2;
118
 
        }
119
 
 
120
 
        for (uint f = 0; f < rl; f+=2)
121
 
        {
122
 
            if (!mixasis)
123
 
            {
124
 
                buff[f] = (Sint16)(((double)*lc) * v1);
125
 
                buff[f + 1] = (Sint16)(((double)*rc) * v2);
126
 
            }
127
 
            else
128
 
            {
129
 
                buff[f] = *lc;
130
 
                buff[f + 1] = *rc;
131
 
            }
132
 
 
133
 
            lc += inc;
134
 
            rc += inc;
135
 
        }
136
 
 
137
 
        return rl;
138
 
    }
139
 
 
140
 
    AudioBuffer::~AudioBuffer()
141
 
    {
 
62
            length *= 2;
 
63
 
142
64
        if (buff)
143
65
            free(buff);
144
 
    }
145
 
 
146
 
    Sint16 *AudioBuffer::getMix(uint requested_length, uint &returned_length)
147
 
    {
148
 
        if (!streaming)
149
 
        {
150
 
            uint oldoffs = offset;
151
 
 
152
 
            if ((offset + requested_length) < length)
153
 
            {
154
 
                returned_length = requested_length;
155
 
            }
156
 
            else
157
 
            {
158
 
                returned_length = length - offset;
159
 
                if (loop)
160
 
                {
161
 
                    offset = 0;
162
 
                }
163
 
                else
164
 
                {
165
 
                    playing = false;
166
 
                }
167
 
 
168
 
            }
169
 
 
170
 
            offset += returned_length;
171
 
            return buff + oldoffs;
172
 
        }
173
 
 
174
 
        // streaming
175
 
        if (requested_length != bufflen)
176
 
        {
177
 
            if (buff)
178
 
                free(buff);
179
 
 
180
 
            buff = (Sint16*)malloc(requested_length * 4);
181
 
            bufflen = requested_length;
182
 
        }
183
 
 
184
 
        returned_length = PanAndMix(requested_length);
185
 
        if (returned_length < requested_length)
186
 
        {
 
66
 
 
67
        buff = (Sint16*)malloc(length * 4);
 
68
        bufflen = length;
 
69
 
 
70
        PanAndMix();
 
71
    }
 
72
 
 
73
    playing = true;
 
74
};
 
75
 
 
76
uint AudioBuffer::PanAndMix(uint req_lenth)
 
77
{
 
78
    uint rl;
 
79
    Sint16 *tb = sample->getSampleData(req_lenth, rl);
 
80
 
 
81
    double v1 = (double)vol / 100;
 
82
    double v2 = (double)vol / 100;
 
83
 
 
84
    if (pan != 0)
 
85
    {
 
86
        if (pan > 0)
 
87
            v1 = ((double)(vol - pan)) / 100;
 
88
 
 
89
        if (v1 < 0)
 
90
            v1 = 0;
 
91
 
 
92
        if (pan < 0)
 
93
            v2 = ((double)(vol + pan)) / 100;
 
94
 
 
95
        if (v2 < 0)
 
96
            v2 = 0;
 
97
 
 
98
    }
 
99
 
 
100
    bool mixasis = false;
 
101
 
 
102
    if ((vol == 100) && (pan == 0))
 
103
        mixasis = true;
 
104
 
 
105
    Sint16 *lc = tb;
 
106
    Sint16 *rc = tb + 1;
 
107
    uint inc = 2;
 
108
 
 
109
    if (sample->getNumChannels() == 1)
 
110
    {
 
111
        rc = lc;
 
112
        inc = 1;
 
113
        rl*=2;
 
114
    }
 
115
 
 
116
    for (uint f = 0; f < rl; f+=2)
 
117
    {
 
118
        if (!mixasis)
 
119
        {
 
120
            buff[f] = (Sint16)(((double)*lc) * v1);
 
121
            buff[f + 1] = (Sint16)(((double)*rc) * v2);
 
122
        }
 
123
        else
 
124
        {
 
125
            buff[f] = *lc;
 
126
            buff[f + 1] = *rc;
 
127
        }
 
128
 
 
129
        lc += inc;
 
130
        rc += inc;
 
131
    }
 
132
 
 
133
    return rl;
 
134
}
 
135
 
 
136
AudioBuffer::~AudioBuffer()
 
137
{
 
138
    if (buff)
 
139
        free(buff);
 
140
}
 
141
 
 
142
Sint16 *AudioBuffer::getMix(uint requested_length, uint &returned_length)
 
143
{
 
144
    if (!streaming)
 
145
    {
 
146
        uint oldoffs = offset;
 
147
 
 
148
        if ((offset + requested_length) < length)
 
149
        {
 
150
            returned_length = requested_length;
 
151
        }
 
152
        else
 
153
        {
 
154
            returned_length = length - offset;
187
155
            if (loop)
188
156
            {
189
 
                sample->Restart();
 
157
                offset = 0;
190
158
            }
191
159
            else
192
160
            {
193
161
                playing = false;
194
162
            }
 
163
 
195
164
        }
196
165
 
197
 
        return buff;
 
166
        offset += returned_length;
 
167
        return buff + oldoffs;
198
168
    }
199
169
 
200
 
    void AudioBuffer::Stop()
 
170
    // streaming
 
171
    if (requested_length != bufflen)
201
172
    {
202
 
        playing = false;
203
173
        if (buff)
204
 
        {
205
174
            free(buff);
206
 
            buff = 0;
207
 
        }
208
 
    }
 
175
 
 
176
        buff = (Sint16*)malloc(requested_length * 4);
 
177
        bufflen = requested_length;
 
178
    }
 
179
 
 
180
    returned_length = PanAndMix(requested_length);
 
181
    if (returned_length < requested_length)
 
182
    {
 
183
        if (loop)
 
184
        {
 
185
            sample->Restart();
 
186
        }
 
187
        else
 
188
        {
 
189
            playing = false;
 
190
        }
 
191
    }
 
192
 
 
193
    return buff;
 
194
}
 
195
 
 
196
void AudioBuffer::Stop()
 
197
{
 
198
    playing = false;
 
199
}
209
200
 
210
201
}