annotate src/Modules/Output/Graphics/GraphicsView.cc @ 244:d3968c3149b0

- Support (hopefully) both movie writing and HTK file output.
author tomwalters
date Tue, 26 Oct 2010 00:03:29 +0000
parents af02b6addf7a
children e35740ed81f3
rev   line source
tomwalters@116 1 // Copyright 2006, Willem van Engen
tomwalters@116 2 //
tomwalters@116 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@116 4 // http://www.acousticscale.org/AIMC
tomwalters@116 5 //
tomwalters@116 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@116 7 // you may not use this file except in compliance with the License.
tomwalters@116 8 // You may obtain a copy of the License at
tomwalters@116 9 //
tomwalters@116 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@116 11 //
tomwalters@116 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@116 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@116 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@116 15 // See the License for the specific language governing permissions and
tomwalters@116 16 // limitations under the License.
tomwalters@116 17
tom@229 18 #include <algorithm>
tom@229 19
tomwalters@116 20 #include "Support/Common.h"
tomwalters@116 21
tom@229 22 #include "Modules/Output/Graphics/GraphicsView.h"
tom@229 23 #include "Modules/Output/Graphics/Devices/GraphicsOutputDevice.h"
tomwalters@237 24 #include "Modules/Output/Graphics/Devices/GraphicsOutputDeviceMovie.h"
tom@229 25
tom@229 26 namespace aimc {
tomwalters@116 27
tomwalters@116 28 GraphicsView::GraphicsView(Parameters *parameters) : Module(parameters) {
tomwalters@232 29 module_description_ = "Graphics output.";
tomwalters@116 30 module_identifier_ = "graphics";
tomwalters@116 31 module_type_ = "output";
tomwalters@116 32 module_version_ = "$Id: $";
tomwalters@227 33
tomwalters@237 34 m_pDev = new GraphicsOutputDeviceMovie(parameters);
tomwalters@228 35 m_bPlotLabels = false;
tomwalters@228 36 m_pAxisX = new GraphAxisSpec();
tomwalters@228 37 AIM_ASSERT(m_pAxisX);
tomwalters@228 38 m_pAxisY = new GraphAxisSpec();
tomwalters@228 39 AIM_ASSERT(m_pAxisY);
tomwalters@228 40 m_pAxisFreq = new GraphAxisSpec();
tomwalters@228 41 AIM_ASSERT(m_pAxisFreq);
tom@229 42 initialized_ = true;
tomwalters@145 43
tomwalters@236 44 if (!m_pAxisY->Initialize(parameters_,
tom@229 45 _S("graph.y"),
tom@229 46 -1,
tom@229 47 1,
tom@229 48 Scale::SCALE_LINEAR)) {
tom@229 49 LOG_ERROR("Axis initialization failed");
tom@229 50 initialized_ = false;
tom@229 51 }
tomwalters@237 52 m_fMarginLeft = parameters_->DefaultFloat(_S("graph.margin.left"), 0.05);
tomwalters@237 53 m_fMarginRight = parameters_->DefaultFloat(_S("graph.margin.right"), 0.005);
tomwalters@237 54 m_fMarginTop = parameters_->DefaultFloat(_S("graph.margin.top"), 0.005);
tomwalters@237 55 m_fMarginBottom = parameters_->DefaultFloat(_S("graph.margin.bottom"), 0.05);
tomwalters@237 56 m_bPlotLabels = parameters_->DefaultBool(_S("graph.plotlabels"), true);
tomwalters@116 57
tomwalters@237 58 const char *sGraphType = parameters_->DefaultString(_S("graph.type"), "line");
tomwalters@228 59 if (strcmp(sGraphType, _S("line"))==0)
tomwalters@228 60 m_iGraphType = GraphTypeLine;
tomwalters@228 61 else if (strcmp(sGraphType, _S("colormap"))==0)
tomwalters@228 62 m_iGraphType = GraphTypeColormap;
tomwalters@228 63 else if (strcmp(sGraphType, _S("none"))==0)
tomwalters@228 64 m_iGraphType = GraphTypeNone;
tomwalters@228 65 else {
tom@229 66 LOG_ERROR(_T("Unrecognized graph type: '%s'"), sGraphType);
tom@229 67 initialized_ = false;
tomwalters@228 68 }
tomwalters@116 69
tomwalters@237 70 if (strcmp(parameters_->DefaultString(_S("graph.mindistance"), "auto"),"auto") == 0)
tomwalters@228 71 // -1 means detect later, based on type and Fire() argument
tomwalters@228 72 m_fMinPlotDistance = -1;
tomwalters@228 73 else
tomwalters@236 74 m_fMinPlotDistance = parameters_->GetFloat(_S("graph.mindistance"));
tomwalters@116 75 }
tomwalters@116 76
tomwalters@116 77 GraphicsView::~GraphicsView() {
tomwalters@228 78 DELETE_IF_NONNULL(m_pAxisX);
tomwalters@228 79 DELETE_IF_NONNULL(m_pAxisY);
tomwalters@228 80 DELETE_IF_NONNULL(m_pAxisFreq);
tomwalters@116 81 }
tomwalters@116 82
tom@229 83 void GraphicsView::ResetInternal() {
tomwalters@237 84 if (m_pDev != NULL) {
tomwalters@244 85 m_pDev->Reset(global_parameters_);
tomwalters@237 86 }
tom@229 87 }
tomwalters@116 88
tomwalters@116 89 bool GraphicsView::InitializeInternal(const SignalBank &bank) {
tomwalters@228 90 if (!m_pDev) {
tomwalters@228 91 LOG_ERROR("Output device not connected");
tomwalters@228 92 return false;
tomwalters@228 93 }
tomwalters@116 94
tomwalters@116 95 float y_min = bank.centre_frequency(0);
tomwalters@116 96 float y_max = bank.centre_frequency(bank.channel_count() - 1);
tomwalters@236 97 if (!m_pAxisFreq->Initialize(parameters_,
tomwalters@116 98 "graph.freq",
tomwalters@228 99 y_min,
tomwalters@228 100 y_max,
tomwalters@228 101 Scale::SCALE_ERB)) {
tomwalters@237 102 LOG_ERROR("Frequency axis init failed.");
tomwalters@116 103 return false;
tomwalters@116 104 }
tomwalters@116 105
tomwalters@116 106 float x_min = 0.0;
tomwalters@116 107 float x_max = 1000.0 * bank.buffer_length() / bank.sample_rate();
tomwalters@236 108 if (!m_pAxisX->Initialize(parameters_,
tomwalters@116 109 "graph.x",
tomwalters@228 110 x_min,
tomwalters@116 111 x_max,
tomwalters@228 112 Scale::SCALE_LINEAR)) {
tomwalters@237 113 LOG_ERROR("Time axis init failed.");
tomwalters@116 114 return false;
tomwalters@116 115 }
tomwalters@116 116
tomwalters@228 117 /* Inform graphics output of maximum number of vertices between
tomwalters@228 118 * gBegin*() and gEnd(), for any type of plot. Colormap needs most.
tomwalters@228 119 */
tomwalters@237 120 LOG_INFO("Initializing graphics output device.");
tomwalters@237 121
tomwalters@237 122 if (!m_pDev->Initialize(global_parameters_)) {
tomwalters@237 123 LOG_ERROR("Graphics output device init failed.");
tomwalters@116 124 return false;
tomwalters@116 125 }
tomwalters@237 126 m_pDev->Start();
tomwalters@237 127 previous_start_time_ = 0;
tom@229 128 return true;
tomwalters@116 129 }
tomwalters@116 130
tomwalters@116 131 void GraphicsView::Process(const SignalBank &bank) {
tomwalters@228 132 float height = 1.0 / bank.channel_count();
tomwalters@228 133 float heightMinMargin = height * (1.0f - m_fMarginBottom - m_fMarginTop);
tomwalters@228 134 float xScaling = 1.0f;
tomwalters@116 135
tomwalters@228 136 m_pDev->gGrab();
tomwalters@228 137 PlotAxes(bank);
tomwalters@228 138 m_pDev->gColor3f(1.0f, 1.0f, 0.8f);
tomwalters@228 139 for (int i = 0; i < bank.channel_count(); i++) {
tomwalters@228 140 float yOffs = bank.centre_frequency(i);
tomwalters@228 141 yOffs = m_pAxisFreq->m_pScale->FromLinearScaled(yOffs) + 0.5f;
tomwalters@228 142 /* Don't plot below zero and stop when above 1, since yOffs is
tomwalters@116 143 * monotonically increasing. Because of rounding errors, we need
tomwalters@116 144 * to check for yOffs < -1e-6 instead of yOffs < 0. */
tomwalters@228 145 if (yOffs < -1e-6)
tomwalters@116 146 continue;
tomwalters@228 147 if (yOffs > 1)
tomwalters@116 148 break;
tomwalters@116 149
tomwalters@116 150 // Scale to single channel graphing.
tomwalters@228 151 yOffs = yOffs * (1.0f - height) + height / 2.0;
tomwalters@228 152 yOffs = yOffs * (1.0f - m_fMarginTop - m_fMarginBottom) + m_fMarginBottom;
tom@229 153 PlotData(bank[i], bank.sample_rate(), yOffs, heightMinMargin, xScaling);
tomwalters@228 154 }
tomwalters@228 155 m_pDev->gRelease();
tomwalters@237 156
tomwalters@237 157 frame_rate_ = bank.sample_rate()
tomwalters@237 158 / (bank.start_time() - previous_start_time_);
tomwalters@237 159 previous_start_time_ = bank.start_time();
tomwalters@237 160 global_parameters_->SetFloat("frame_rate", frame_rate_);
tomwalters@116 161 }
tomwalters@116 162
tomwalters@116 163 void GraphicsView::SetAxisScale(Scale::ScaleType iHori,
tomwalters@116 164 Scale::ScaleType iVert,
tomwalters@116 165 Scale::ScaleType iFreq) {
tomwalters@228 166 AIM_ASSERT(m_pAxisX);
tomwalters@228 167 AIM_ASSERT(m_pAxisY);
tomwalters@228 168 AIM_ASSERT(m_pAxisFreq);
tomwalters@228 169 m_pAxisX->SetDisplayScale(iHori);
tomwalters@228 170 m_pAxisY->SetDisplayScale(iVert);
tomwalters@228 171 m_pAxisFreq->SetDisplayScale(iFreq);
tomwalters@116 172 }
tomwalters@116 173
tomwalters@116 174 void GraphicsView::BeginDataStrip() {
tomwalters@228 175 m_iPrevValEqual=0;
tomwalters@228 176 m_bFirstPoint=true;
tomwalters@228 177 switch(m_iGraphType) {
tomwalters@228 178 case GraphTypeLine:
tomwalters@228 179 m_pDev->gBeginLineStrip();
tomwalters@228 180 break;
tomwalters@228 181 case GraphTypeColormap:
tomwalters@228 182 m_pDev->gBeginQuadStrip();
tomwalters@228 183 break;
tomwalters@228 184 case GraphTypeNone:
tomwalters@228 185 // Nothing: just for testing computation overhead of graphing
tomwalters@228 186 break;
tomwalters@228 187 }
tomwalters@116 188 }
tomwalters@116 189
tomwalters@116 190 void GraphicsView::PlotDataPoint(float x,
tomwalters@116 191 float y,
tomwalters@116 192 float val,
tomwalters@116 193 float height,
tomwalters@116 194 bool isLast) {
tomwalters@228 195 AIM_ASSERT(m_pDev);
tomwalters@116 196
tomwalters@228 197 /* Reduce the number of points plotted by eliminating duplicate values:
tomwalters@228 198 *
tomwalters@228 199 * oooo o--o
tomwalters@228 200 * o / o
tomwalters@228 201 * oooo ooo => o--o o--o
tomwalters@228 202 * oooo ooo o--o o-o
tomwalters@228 203 *
tomwalters@228 204 * with 'o' points that are plotted, and '-' by the graphics output
tomwalters@228 205 * device interpolated points. We could be smarter and include
tomwalters@228 206 * first-order interpolation, but we leave that as an exercise for
tomwalters@228 207 * the reader. Please send your patches :)
tomwalters@228 208 *
tomwalters@228 209 * So, we don't draw points that are too close to the previous value.
tomwalters@228 210 * But if the value changes (or it's the last point), we must draw the
tomwalters@228 211 * previous point too.
tomwalters@228 212 */
tomwalters@228 213 if (!m_bFirstPoint
tomwalters@116 214 && !isLast
tom@229 215 && abs(m_fPrevVal-val) < m_fMinPlotDistance) {
tomwalters@228 216 m_iPrevValEqual++;
tomwalters@228 217 // Don't set m_fPrevVal to avoid not catching slow changes
tomwalters@228 218 m_fPrevX = x;
tomwalters@228 219 m_fPrevY = y;
tomwalters@228 220 m_fPrevHeight = height;
tomwalters@228 221 return;
tomwalters@228 222 } else {
tomwalters@228 223 if (m_iPrevValEqual > 0) {
tomwalters@228 224 // Draw previous point
tomwalters@228 225 PlotDataPointDirect(m_fPrevX, m_fPrevY, m_fPrevVal, m_fPrevHeight);
tomwalters@228 226 }
tomwalters@228 227 m_iPrevValEqual = 0;
tomwalters@228 228 m_fPrevVal = val;
tomwalters@228 229 m_bFirstPoint = false;
tomwalters@228 230 }
tomwalters@228 231 PlotDataPointDirect(x, y, val, height);
tomwalters@116 232 }
tomwalters@116 233
tomwalters@116 234 void GraphicsView::PlotDataPointDirect(float x,
tomwalters@116 235 float y,
tomwalters@116 236 float val,
tomwalters@116 237 float height) {
tomwalters@228 238 // Draw it in the right way
tomwalters@228 239 switch(m_iGraphType) {
tomwalters@228 240 case GraphTypeLine:
tomwalters@228 241 m_pDev->gVertex2f(x, y + val * height);
tomwalters@228 242 break;
tomwalters@228 243 case GraphTypeColormap:
tomwalters@228 244 //! \todo make it a real colormap instead of grayscale
tomwalters@228 245 m_pDev->gColor3f(val + 0.5, val + 0.5, val + 0.5);
tomwalters@228 246 m_pDev->gVertex2f(x, y - height / 2);
tomwalters@228 247 m_pDev->gVertex2f(x, y + height / 2);
tomwalters@228 248 break;
tomwalters@228 249 case GraphTypeNone:
tomwalters@228 250 // Nothing: just for testing computation overhead of graphing
tomwalters@228 251 break;
tomwalters@228 252 default:
tomwalters@228 253 // Shouldn't happen
tomwalters@228 254 AIM_ASSERT(0);
tomwalters@228 255 }
tomwalters@116 256 }
tom@229 257 } // namespace aimc