tomwalters@397
|
1 // Copyright 2006-2010, Willem van Engen, Thomas Walters
|
tomwalters@397
|
2 //
|
tomwalters@397
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@397
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@397
|
5 //
|
tomwalters@397
|
6 // Licensed under the Apache License, Version 2.0 (the "License");
|
tomwalters@397
|
7 // you may not use this file except in compliance with the License.
|
tomwalters@397
|
8 // You may obtain a copy of the License at
|
tomwalters@397
|
9 //
|
tomwalters@397
|
10 // http://www.apache.org/licenses/LICENSE-2.0
|
tomwalters@397
|
11 //
|
tomwalters@397
|
12 // Unless required by applicable law or agreed to in writing, software
|
tomwalters@397
|
13 // distributed under the License is distributed on an "AS IS" BASIS,
|
tomwalters@397
|
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tomwalters@397
|
15 // See the License for the specific language governing permissions and
|
tomwalters@397
|
16 // limitations under the License.
|
tomwalters@397
|
17
|
tomwalters@397
|
18 /*!
|
tomwalters@397
|
19 * \file
|
tomwalters@397
|
20 * \brief Time-representation graphics view
|
tomwalters@397
|
21 *
|
tomwalters@397
|
22 * \author Willem van Engen <cnbh@willem.engen.nl>
|
tomwalters@397
|
23 * \date created 2006/09/26
|
tomwalters@397
|
24 * \version \$Id: GraphicsViewTime.cpp 607 2008-06-25 00:14:14Z tom $
|
tomwalters@397
|
25 */
|
tomwalters@397
|
26
|
tomwalters@397
|
27 #include "Support/Common.h"
|
tomwalters@397
|
28
|
tomwalters@397
|
29 #include <stdio.h>
|
tomwalters@397
|
30
|
tomwalters@397
|
31 #include "Support/SignalBank.h"
|
tomwalters@397
|
32 #include "Modules/Output/Graphics/GraphicsView.h"
|
tomwalters@397
|
33 #include "Modules/Output/Graphics/GraphicsViewTime.h"
|
tomwalters@397
|
34
|
tom@399
|
35 namespace aimc {
|
tom@399
|
36
|
tomwalters@397
|
37 GraphicsViewTime::GraphicsViewTime(Parameters *pParam)
|
tomwalters@398
|
38 : GraphicsView(pParam) {
|
tomwalters@397
|
39 }
|
tomwalters@397
|
40
|
tomwalters@397
|
41 GraphicsViewTime *GraphicsViewTime::Clone(GraphicsOutputDevice *pDev) {
|
tomwalters@398
|
42 GraphicsViewTime *pView = new GraphicsViewTime(m_pParam);
|
tomwalters@398
|
43 // Copy everything
|
tomwalters@398
|
44 pView->m_pAxisX->SetDisplayRange(m_pAxisX->m_fMax, m_pAxisX->m_fMin);
|
tomwalters@398
|
45 pView->m_pAxisX->SetDisplayScale(m_pAxisX->m_pScale->getType());
|
tomwalters@398
|
46 pView->m_pAxisY->SetDisplayRange(m_pAxisY->m_fMax, m_pAxisY->m_fMin);
|
tomwalters@398
|
47 pView->m_pAxisY->SetDisplayScale(m_pAxisY->m_pScale->getType());
|
tomwalters@398
|
48 pView->m_pAxisFreq->SetDisplayRange(m_pAxisFreq->m_fMax, m_pAxisFreq->m_fMin);
|
tomwalters@398
|
49 pView->m_pAxisFreq->SetDisplayScale(m_pAxisFreq->m_pScale->getType());
|
tomwalters@398
|
50 return pView;
|
tomwalters@397
|
51 }
|
tomwalters@397
|
52
|
tomwalters@397
|
53 void GraphicsViewTime::PlotAxes(const SignalBank &bank) {
|
tomwalters@398
|
54 m_pDev->gColor3f(0.0f, 0.7f, 0.7f);
|
tomwalters@398
|
55 // Vertical axis
|
tomwalters@398
|
56 m_pDev->gBeginLineStrip();
|
tomwalters@398
|
57 m_pDev->gVertex2f(m_fMarginLeft, m_fMarginBottom);
|
tomwalters@398
|
58 m_pDev->gVertex2f(m_fMarginLeft, 1.0f - m_fMarginTop);
|
tomwalters@398
|
59 m_pDev->gEnd();
|
tomwalters@398
|
60 // Horizontal axis
|
tomwalters@398
|
61 m_pDev->gBeginLineStrip();
|
tomwalters@398
|
62 m_pDev->gVertex2f(m_fMarginLeft, m_fMarginBottom);
|
tomwalters@398
|
63 m_pDev->gVertex2f(1.0f-m_fMarginRight, m_fMarginBottom);
|
tomwalters@398
|
64 m_pDev->gEnd();
|
tomwalters@397
|
65
|
tomwalters@398
|
66 if (!m_bPlotLabels)
|
tomwalters@398
|
67 return;
|
tomwalters@397
|
68
|
tomwalters@398
|
69 // Labels
|
tomwalters@398
|
70 char sTxt[80];
|
tomwalters@398
|
71 snprintf(sTxt, sizeof(sTxt) / sizeof(sTxt[0]),
|
tomwalters@397
|
72 _S("%s [%.0f..%.0f Hz, %s scale]"),
|
tomwalters@398
|
73 m_pAxisFreq->m_sLabel ? m_pAxisFreq->m_sLabel : "",
|
tomwalters@398
|
74 m_pAxisFreq->m_fMin, m_pAxisFreq->m_fMax,
|
tomwalters@398
|
75 m_pAxisFreq->m_pScale->getName());
|
tomwalters@398
|
76 m_pDev->gText2f(0.0025f, 0.35f, sTxt, true);
|
tom@399
|
77 snprintf(sTxt, sizeof(sTxt) / sizeof(sTxt[0]),
|
tom@399
|
78 _S("%s [%.2f..%.2f ms, %s scale]"),
|
tom@399
|
79 m_pAxisX->m_sLabel ? m_pAxisX->m_sLabel : "",
|
tom@399
|
80 m_pAxisX->m_fMin,
|
tom@399
|
81 m_pAxisX->m_fMax,
|
tom@399
|
82 m_pAxisX->m_pScale->getName());
|
tom@399
|
83
|
tomwalters@398
|
84 m_pDev->gText2f(m_fMarginLeft, 0.0025f, sTxt, false);
|
tomwalters@397
|
85
|
tomwalters@398
|
86 // Frame time
|
tom@399
|
87 //snprintf(sTxt, sizeof(sTxt)/sizeof(sTxt[0]), _S("t=%.0f ms"),
|
tom@399
|
88 // pBank->getSampleTime(0));
|
tom@399
|
89 //m_pDev->gText2f(0.8f, 0.0025f, sTxt, false);
|
tomwalters@397
|
90 }
|
tomwalters@397
|
91
|
tomwalters@397
|
92 void GraphicsViewTime::PlotData(const vector<float> &signal,
|
tomwalters@397
|
93 float sample_rate,
|
tomwalters@397
|
94 float yOffset,
|
tomwalters@397
|
95 float height,
|
tomwalters@397
|
96 float xScale) {
|
tomwalters@398
|
97 AIM_ASSERT(xScale >= 0 && xScale <= 1);
|
tomwalters@398
|
98 AIM_ASSERT(height > 0 && height <= 1);
|
tomwalters@398
|
99 AIM_ASSERT(yOffset >= 0 && yOffset <= 1);
|
tomwalters@398
|
100 AIM_ASSERT(m_pAxisX && m_pAxisX->m_pScale);
|
tomwalters@398
|
101 AIM_ASSERT(m_pAxisY && m_pAxisY->m_pScale);
|
tomwalters@397
|
102
|
tomwalters@398
|
103 // Make sure we get time is ms as x value
|
tomwalters@398
|
104 xScale *= 1000.0 / sample_rate;
|
tomwalters@398
|
105 m_pDev->gColor3f(1.0f, 1.0f, 0.8f);
|
tomwalters@398
|
106 BeginDataStrip();
|
tomwalters@397
|
107
|
tomwalters@398
|
108 // Draw the signal.
|
tomwalters@398
|
109 float x = 0;
|
tomwalters@398
|
110 float y = 0;
|
tom@399
|
111 for (unsigned int i = 0; i < signal.size(); i++) {
|
tomwalters@398
|
112 // Find out where to draw and do so
|
tomwalters@398
|
113 x = xScale * i;
|
tomwalters@398
|
114 y = signal[i];
|
tomwalters@398
|
115 x = m_pAxisX->m_pScale->FromLinearScaled(x) + 0.5f;
|
tomwalters@398
|
116 y = m_pAxisY->m_pScale->FromLinearScaled(y);
|
tomwalters@397
|
117
|
tomwalters@398
|
118 if (x < 0.0)
|
tomwalters@397
|
119 continue;
|
tomwalters@397
|
120
|
tomwalters@398
|
121 // Now fit it into the drawing area
|
tomwalters@398
|
122 x = x * (1.0f - m_fMarginLeft - m_fMarginRight) + m_fMarginLeft; // fit inside x-axis area
|
tomwalters@398
|
123 PlotDataPoint(x, yOffset, y, height, false);
|
tomwalters@398
|
124 /* There's no point in drawing anything when x>1.0, outside of view.
|
tomwalters@398
|
125 * x is continuously increasing, so we can just stop completely. We need to
|
tomwalters@398
|
126 * plot this point however, to finish the final line. */
|
tomwalters@398
|
127 if (x > 1.0)
|
tomwalters@398
|
128 break;
|
tomwalters@398
|
129 }
|
tomwalters@398
|
130 // Redraw the last point in case it's needed
|
tomwalters@398
|
131 PlotDataPoint(x, yOffset, y, height, true);
|
tomwalters@397
|
132
|
tomwalters@398
|
133 m_pDev->gEnd();
|
tomwalters@397
|
134 }
|
tom@399
|
135 } // namespace aimc
|