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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Rosegarden
A MIDI and audio sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <[email protected]>,
Chris Cannam <[email protected]>,
Richard Bown <[email protected]>
The moral rights of Guillaume Laurent, Chris Cannam, and Richard
Bown to claim authorship of this work have been asserted.
Other copyrights also apply to some parts of this work. Please
see the AUTHORS file and individual file headers for details.
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. See the file
COPYING included with this distribution for more information.
*/
#include "AudioPreviewPainter.h"
#include "CompositionModelImpl.h"
#include "CompositionColourCache.h"
#include "base/Composition.h"
#include "base/Track.h"
#include "base/AudioLevel.h"
#include "base/Studio.h"
#include "misc/Debug.h"
#include "document/ConfigGroups.h"
#include <tqimage.h>
#include <tqapplication.h>
#include <kapp.h>
#include <kconfig.h>
namespace Rosegarden {
AudioPreviewPainter::AudioPreviewPainter(CompositionModelImpl& model,
CompositionModel::AudioPreviewData* apData,
const Composition &composition,
const Segment* segment)
: m_model(model),
m_apData(apData),
m_composition(composition),
m_segment(segment),
m_rect(model.computeSegmentRect(*(segment))),
m_image(1, 1, 8, 4),
m_defaultCol(CompositionColourCache::getInstance()->SegmentAudioPreview),
m_height(model.grid().getYSnap()/2)
{
int pixWidth = std::min(m_rect.getBaseWidth(), tileWidth());
m_image = TQImage(pixWidth, m_rect.height(), 8, 4);
m_image.setAlphaBuffer(true);
m_penWidth = (std::max(1U, (uint)m_rect.getPen().width()) * 2);
m_halfRectHeight = m_model.grid().getYSnap()/2 - m_penWidth / 2 - 2;
}
int AudioPreviewPainter::tileWidth()
{
static int tw = -1;
if (tw == -1) tw = TQApplication::desktop()->width();
return tw;
}
void AudioPreviewPainter::paintPreviewImage()
{
const std::vector<float>& values = m_apData->getValues();
if (values.size() == 0)
return;
float gain[2] = { 1.0, 1.0 };
int instrumentChannels = 2;
TrackId trackId = m_segment->getTrack();
Track *track = m_model.getComposition().getTrackById(trackId);
if (track) {
Instrument *instrument = m_model.getStudio().getInstrumentById(track->getInstrument());
if (instrument) {
float level = AudioLevel::dB_to_multiplier(instrument->getLevel());
float pan = instrument->getPan() - 100.0;
gain[0] = level * ((pan > 0.0) ? (1.0 - (pan / 100.0)) : 1.0);
gain[1] = level * ((pan < 0.0) ? ((pan + 100.0) / 100.0) : 1.0);
instrumentChannels = instrument->getAudioChannels();
}
}
bool showMinima = m_apData->showsMinima();
unsigned int channels = m_apData->getChannels();
if (channels == 0) {
RG_DEBUG << "AudioPreviewPainter::paintPreviewImage : problem with audio file for segment "
<< m_segment->getLabel().c_str() << endl;
return;
}
int samplePoints = values.size() / (channels * (showMinima ? 2 : 1));
float h1, h2, l1 = 0, l2 = 0;
double sampleScaleFactor = samplePoints / double(m_rect.getBaseWidth());
m_sliceNb = 0;
m_image.fill(0);
int centre = m_image.height() / 2;
RG_DEBUG << "AudioPreviewPainter::paintPreviewImage width = " << m_rect.getBaseWidth() << ", height = " << m_rect.height() << ", halfRectHeight = " << m_halfRectHeight << endl;
RG_DEBUG << "AudioPreviewPainter::paintPreviewImage: channels = " << channels << ", gain left = " << gain[0] << ", right = " << gain[1] << endl;
double audioDuration = double(m_segment->getAudioEndTime().sec) +
double(m_segment->getAudioEndTime().nsec) / 1000000000.0;
// We need to take each pixel value and map it onto a point within
// the preview. We have samplePoints preview points in a known
// duration of audioDuration. Thus each point spans a real time
// of audioDuration / samplePoints. We need to convert the
// accumulated real time back into musical time, and map this
// proportionately across the segment width.
RealTime startRT =
m_model.getComposition().getElapsedRealTime(m_segment->getStartTime());
double startTime = double(startRT.sec) + double(startRT.nsec) / 1000000000.0;
RealTime endRT =
m_model.getComposition().getElapsedRealTime(m_segment->getEndMarkerTime());
double endTime = double(endRT.sec) + double(endRT.nsec) / 1000000000.0;
bool haveTempoChange = false;
int finalTempoChangeNumber =
m_model.getComposition().getTempoChangeNumberAt
(m_segment->getEndMarkerTime());
if ((finalTempoChangeNumber >= 0) &&
(finalTempoChangeNumber >
m_model.getComposition().getTempoChangeNumberAt
(m_segment->getStartTime()))) {
haveTempoChange = true;
}
TDEConfig* config = kapp->config();
config->setGroup(GeneralOptionsConfigGroup);
bool meterLevels = (config->readUnsignedNumEntry("audiopreviewstyle", 1)
== 1);
for (int i = 0; i < m_rect.getBaseWidth(); ++i) {
// i is the x coordinate within the rectangle. We need to
// calculate the position within the audio preview from which
// to draw the peak for this coordinate. It's possible there
// may be more than one, in which case we need to find the
// peak of all of them.
int position = 0;
if (haveTempoChange) {
// First find the time corresponding to this i.
timeT musicalTime =
m_model.grid().getRulerScale()->getTimeForX(m_rect.x() + i);
RealTime realTime =
m_model.getComposition().getElapsedRealTime(musicalTime);
double time = double(realTime.sec) +
double(realTime.nsec) / 1000000000.0;
double offset = time - startTime;
if (endTime > startTime) {
position = offset * m_rect.getBaseWidth() / (endTime - startTime);
position = int(channels * position);
}
} else {
position = int(channels * i * sampleScaleFactor);
}
if (position < 0) continue;
if (position >= values.size() - channels) {
finalizeCurrentSlice();
break;
}
if (channels == 1) {
h1 = values[position++];
h2 = h1;
if (showMinima) {
l1 = values[position++];
l2 = l1;
}
} else {
h1 = values[position++];
if (showMinima) l1 = values[position++];
h2 = values[position++];
if (showMinima) l2 = values[position++];
}
if (instrumentChannels == 1 && channels == 2) {
h1 = h2 = (h1 + h2) / 2;
l1 = l2 = (l1 + l2) / 2;
}
h1 *= gain[0];
h2 *= gain[1];
l1 *= gain[0];
l2 *= gain[1];
int width = 1;
int pixel;
// h1 left, h2 right
if (h1 >= 1.0) { h1 = 1.0; pixel = 2; }
else { pixel = 1; }
int h;
if (meterLevels) {
h = AudioLevel::multiplier_to_preview(h1, m_height);
} else {
h = h1 * m_height;
}
if (h <= 0) h = 1;
if (h > m_halfRectHeight) h = m_halfRectHeight;
int rectX = i % tileWidth();
for (int py = 0; py < h; ++py) {
m_image.setPixel(rectX, centre - py, pixel);
}
if (h2 >= 1.0) { h2 = 1.0; pixel = 2; }
else { pixel = 1; }
if (meterLevels) {
h = AudioLevel::multiplier_to_preview(h2, m_height);
} else {
h = h2 * m_height;
}
if (h < 0) h = 0;
for (int py = 0; py < h; ++py) {
m_image.setPixel(rectX, centre + py, pixel);
}
if (((i+1) % tileWidth()) == 0 || i == (m_rect.getBaseWidth() - 1)) {
finalizeCurrentSlice();
}
}
/* Auto-fade not yet implemented.
if (m_segment->isAutoFading()) {
Composition &comp = m_model.getComposition();
int audioFadeInEnd = int(
m_model.grid().getRulerScale()->getXForTime(comp.
getElapsedTimeForRealTime(m_segment->getFadeInTime()) +
m_segment->getStartTime()) -
m_model.grid().getRulerScale()->getXForTime(m_segment->getStartTime()));
m_p.setPen(TQt::blue);
m_p.drawRect(0, m_apData->getSegmentRect().height() - 1, audioFadeInEnd, 1);
m_pb.drawRect(0, m_apData->getSegmentRect().height() - 1, audioFadeInEnd, 1);
}
m_p.end();
m_pb.end();
*/
}
void AudioPreviewPainter::finalizeCurrentSlice()
{
// RG_DEBUG << "AudioPreviewPainter::finalizeCurrentSlice : copying pixmap to image at " << m_sliceNb * tileWidth() << endl;
// transparent background
m_image.setColor(0, tqRgba(255, 255, 255, 0));
// foreground from computeSegmentPreviewColor
TQColor c = m_model.computeSegmentPreviewColor(m_segment);
TQRgb rgba = tqRgba(c.red(), c.green(), c.blue(), 255);
m_image.setColor(1, rgba);
// red for clipping
m_image.setColor(2, tqRgba(255, 0, 0, 255));
m_previewPixmaps.push_back(m_image.copy());
m_image.fill(0);
++m_sliceNb;
}
PixmapArray AudioPreviewPainter::getPreviewImage()
{
return m_previewPixmaps;
}
}
|