2 * Copyright (c) 2004 Matthijs Hollemans
3 * Copyright (c) 2008-2014 Haiku, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "ScopeView.h"
33 #undef B_TRANSLATION_CONTEXT
34 #define B_TRANSLATION_CONTEXT "Scope View"
37 // #pragma mark - ScopeView
40 ScopeView::ScopeView()
42 BView(BRect(0, 0, 180, 63), NULL
, B_FOLLOW_LEFT
| B_FOLLOW_TOP
,
49 fSampleCount(Bounds().IntegerWidth()),
50 fLeftSamples(new int16
[fSampleCount
]),
51 fRightSamples(new int16
[fSampleCount
]),
57 ScopeView::~ScopeView()
59 delete[] fLeftSamples
;
60 delete[] fRightSamples
;
64 void ScopeView::AttachedToWindow()
66 SetViewColor(0, 0, 0);
69 fScopeThreadID
= spawn_thread(_Thread
, "ScopeThread",
70 B_NORMAL_PRIORITY
, this);
71 if (fScopeThreadID
> 0)
72 resume_thread(fScopeThreadID
);
77 ScopeView::DetachedFromWindow()
79 if (fScopeThreadID
> 0) {
82 wait_for_thread(fScopeThreadID
, &exitValue
);
88 ScopeView::Draw(BRect updateRect
)
90 super::Draw(updateRect
);
94 else if (!fHaveFile
&& !fIsLiveInput
)
98 else if (fIsPlaying
|| fIsLiveInput
)
106 ScopeView::_Thread(void* data
)
108 return ((ScopeView
*) data
)->Thread();
115 // Because Pulse() was too slow, I created a thread that tells the
116 // ScopeView to repaint itself. Note that we need to call LockLooper
117 // with a timeout, otherwise we'll deadlock in DetachedFromWindow().
119 while (!fIsFinished
) {
120 if (fIsEnabled
&& (fIsPlaying
|| fIsLiveInput
)) {
121 if (LockLooperWithTimeout(50000) == B_OK
) {
134 ScopeView::DrawLoading()
136 DrawText(B_TRANSLATE("Loading instruments" B_UTF8_ELLIPSIS
));
141 ScopeView::DrawNoFile()
143 DrawText(B_TRANSLATE("Drop MIDI file here"));
148 ScopeView::DrawDisabled()
150 SetHighColor(64, 64, 64);
152 StrokeLine(BPoint(0, Bounds().Height() / 2),
153 BPoint(Bounds().Width(), Bounds().Height() / 2));
158 ScopeView::DrawStopped()
160 SetHighColor(0, 130, 0);
162 StrokeLine(BPoint(0, Bounds().Height() / 2),
163 BPoint(Bounds().Width(), Bounds().Height() / 2));
168 ScopeView::DrawPlaying()
170 int32 width
= (int32
) Bounds().Width();
171 int32 height
= (int32
) Bounds().Height();
173 // Scope drawing magic based on code by Michael Pfeiffer.
175 int32 size
= be_synth
->GetAudio(fLeftSamples
, fRightSamples
, fSampleCount
);
177 SetHighColor(255, 0, 130);
178 SetLowColor(0, 130, 0);
183 int32 f
= (height
<< 16) / 65535;
184 int32 dy
= height
/ 2 + 1;
185 for (int32 i
= 0; i
< width
; i
++) {
187 y
= ((fLeftSamples
[x
] * f
) >> 16) + dy
;
188 FillRect(BRect(i
, y
, i
, y
));
190 y
= ((fRightSamples
[x
] * f
) >> 16) + dy
;
191 FillRect(BRect(i
, y
, i
, y
), B_SOLID_LOW
);
200 ScopeView::DrawText(const char* text
)
203 GetFontHeight(&height
);
205 float stringWidth
= StringWidth(text
);
206 float stringHeight
= height
.ascent
+ height
.descent
;
208 float x
= (Bounds().Width() - stringWidth
) / 2;
209 float y
= height
.ascent
+ (Bounds().Height() - stringHeight
) / 2;
211 SetHighColor(255, 255, 255);
212 SetLowColor(ViewColor());
213 SetDrawingMode(B_OP_OVER
);
215 DrawString(text
, BPoint(x
, y
));