tomwalters@116: // Copyright 2006, Willem van Engen tomwalters@116: // tomwalters@116: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@116: // http://www.acousticscale.org/AIMC tomwalters@116: // tomwalters@116: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@116: // you may not use this file except in compliance with the License. tomwalters@116: // You may obtain a copy of the License at tomwalters@116: // tomwalters@116: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@116: // tomwalters@116: // Unless required by applicable law or agreed to in writing, software tomwalters@116: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@116: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@116: // See the License for the specific language governing permissions and tomwalters@116: // limitations under the License. tomwalters@116: tom@229: #include tom@229: tomwalters@116: #include "Support/Common.h" tomwalters@116: tom@229: #include "Modules/Output/Graphics/GraphicsView.h" tom@229: #include "Modules/Output/Graphics/Devices/GraphicsOutputDevice.h" tom@229: tom@229: namespace aimc { tomwalters@116: tomwalters@116: GraphicsView::GraphicsView(Parameters *parameters) : Module(parameters) { tomwalters@232: module_description_ = "Graphics output."; tomwalters@116: module_identifier_ = "graphics"; tomwalters@116: module_type_ = "output"; tomwalters@116: module_version_ = "$Id: $"; tomwalters@227: tomwalters@236: m_pDev = new GraphicsOutputDeviceCairo(); tomwalters@228: m_bPlotLabels = false; tomwalters@228: m_pAxisX = new GraphAxisSpec(); tomwalters@228: AIM_ASSERT(m_pAxisX); tomwalters@228: m_pAxisY = new GraphAxisSpec(); tomwalters@228: AIM_ASSERT(m_pAxisY); tomwalters@228: m_pAxisFreq = new GraphAxisSpec(); tomwalters@228: AIM_ASSERT(m_pAxisFreq); tom@229: initialized_ = true; tomwalters@145: tomwalters@236: if (!m_pAxisY->Initialize(parameters_, tom@229: _S("graph.y"), tom@229: -1, tom@229: 1, tom@229: Scale::SCALE_LINEAR)) { tom@229: LOG_ERROR("Axis initialization failed"); tom@229: initialized_ = false; tom@229: } tomwalters@236: m_fMarginLeft = parameters_->GetFloat(_S("graph.margin.left")); tomwalters@236: m_fMarginRight = parameters_->GetFloat(_S("graph.margin.right")); tomwalters@236: m_fMarginTop = parameters_->GetFloat(_S("graph.margin.top")); tomwalters@236: m_fMarginBottom = parameters_->GetFloat(_S("graph.margin.bottom")); tomwalters@236: m_bPlotLabels = parameters_->GetBool(_S("graph.plotlabels")); tomwalters@116: tomwalters@236: const char *sGraphType = parameters_->GetString(_S("graph.type")); tomwalters@228: if (strcmp(sGraphType, _S("line"))==0) tomwalters@228: m_iGraphType = GraphTypeLine; tomwalters@228: else if (strcmp(sGraphType, _S("colormap"))==0) tomwalters@228: m_iGraphType = GraphTypeColormap; tomwalters@228: else if (strcmp(sGraphType, _S("none"))==0) tomwalters@228: m_iGraphType = GraphTypeNone; tomwalters@228: else { tom@229: LOG_ERROR(_T("Unrecognized graph type: '%s'"), sGraphType); tom@229: initialized_ = false; tomwalters@228: } tomwalters@116: tomwalters@236: if (strcmp(parameters_->GetString(_S("graph.mindistance")),"auto") == 0) tomwalters@228: // -1 means detect later, based on type and Fire() argument tomwalters@228: m_fMinPlotDistance = -1; tomwalters@228: else tomwalters@236: m_fMinPlotDistance = parameters_->GetFloat(_S("graph.mindistance")); tomwalters@116: } tomwalters@116: tomwalters@116: GraphicsView::~GraphicsView() { tomwalters@228: DELETE_IF_NONNULL(m_pAxisX); tomwalters@228: DELETE_IF_NONNULL(m_pAxisY); tomwalters@228: DELETE_IF_NONNULL(m_pAxisFreq); tomwalters@116: } tomwalters@116: tom@229: void GraphicsView::ResetInternal() { tom@229: } tomwalters@116: tomwalters@116: bool GraphicsView::InitializeInternal(const SignalBank &bank) { tomwalters@228: if (!m_pDev) { tomwalters@228: LOG_ERROR("Output device not connected"); tomwalters@228: return false; tomwalters@228: } tomwalters@116: tomwalters@116: float y_min = bank.centre_frequency(0); tomwalters@116: float y_max = bank.centre_frequency(bank.channel_count() - 1); tomwalters@236: if (!m_pAxisFreq->Initialize(parameters_, tomwalters@116: "graph.freq", tomwalters@228: y_min, tomwalters@228: y_max, tomwalters@228: Scale::SCALE_ERB)) { tomwalters@227: LOG_ERROR(""); tomwalters@116: return false; tomwalters@116: } tomwalters@116: tomwalters@116: float x_min = 0.0; tomwalters@116: float x_max = 1000.0 * bank.buffer_length() / bank.sample_rate(); tomwalters@236: if (!m_pAxisX->Initialize(parameters_, tomwalters@116: "graph.x", tomwalters@228: x_min, tomwalters@116: x_max, tomwalters@228: Scale::SCALE_LINEAR)) { tomwalters@227: LOG_ERROR(""); tomwalters@116: return false; tomwalters@116: } tomwalters@116: tomwalters@228: /* Inform graphics output of maximum number of vertices between tomwalters@228: * gBegin*() and gEnd(), for any type of plot. Colormap needs most. tomwalters@228: */ tom@229: if (!m_pDev->Initialize(std::max(10, bank.buffer_length() * 2 + 2))) { tomwalters@227: LOG_ERROR(""); tomwalters@116: return false; tomwalters@116: } tom@229: return true; tomwalters@116: } tomwalters@116: tomwalters@116: void GraphicsView::Process(const SignalBank &bank) { tomwalters@228: float height = 1.0 / bank.channel_count(); tomwalters@228: float heightMinMargin = height * (1.0f - m_fMarginBottom - m_fMarginTop); tomwalters@228: float xScaling = 1.0f; tomwalters@116: tomwalters@228: m_pDev->gGrab(); tomwalters@228: PlotAxes(bank); tomwalters@228: m_pDev->gColor3f(1.0f, 1.0f, 0.8f); tomwalters@228: for (int i = 0; i < bank.channel_count(); i++) { tomwalters@228: float yOffs = bank.centre_frequency(i); tomwalters@228: yOffs = m_pAxisFreq->m_pScale->FromLinearScaled(yOffs) + 0.5f; tomwalters@228: /* Don't plot below zero and stop when above 1, since yOffs is tomwalters@116: * monotonically increasing. Because of rounding errors, we need tomwalters@116: * to check for yOffs < -1e-6 instead of yOffs < 0. */ tomwalters@228: if (yOffs < -1e-6) tomwalters@116: continue; tomwalters@228: if (yOffs > 1) tomwalters@116: break; tomwalters@116: tomwalters@116: // Scale to single channel graphing. tomwalters@228: yOffs = yOffs * (1.0f - height) + height / 2.0; tomwalters@228: yOffs = yOffs * (1.0f - m_fMarginTop - m_fMarginBottom) + m_fMarginBottom; tom@229: PlotData(bank[i], bank.sample_rate(), yOffs, heightMinMargin, xScaling); tomwalters@228: } tomwalters@228: m_pDev->gRelease(); tomwalters@116: } tomwalters@116: tomwalters@116: void GraphicsView::SetAxisScale(Scale::ScaleType iHori, tomwalters@116: Scale::ScaleType iVert, tomwalters@116: Scale::ScaleType iFreq) { tomwalters@228: AIM_ASSERT(m_pAxisX); tomwalters@228: AIM_ASSERT(m_pAxisY); tomwalters@228: AIM_ASSERT(m_pAxisFreq); tomwalters@228: m_pAxisX->SetDisplayScale(iHori); tomwalters@228: m_pAxisY->SetDisplayScale(iVert); tomwalters@228: m_pAxisFreq->SetDisplayScale(iFreq); tomwalters@116: } tomwalters@116: tomwalters@116: void GraphicsView::BeginDataStrip() { tomwalters@228: m_iPrevValEqual=0; tomwalters@228: m_bFirstPoint=true; tomwalters@228: switch(m_iGraphType) { tomwalters@228: case GraphTypeLine: tomwalters@228: m_pDev->gBeginLineStrip(); tomwalters@228: break; tomwalters@228: case GraphTypeColormap: tomwalters@228: m_pDev->gBeginQuadStrip(); tomwalters@228: break; tomwalters@228: case GraphTypeNone: tomwalters@228: // Nothing: just for testing computation overhead of graphing tomwalters@228: break; tomwalters@228: } tomwalters@116: } tomwalters@116: tomwalters@116: void GraphicsView::PlotDataPoint(float x, tomwalters@116: float y, tomwalters@116: float val, tomwalters@116: float height, tomwalters@116: bool isLast) { tomwalters@228: AIM_ASSERT(m_pDev); tomwalters@116: tomwalters@228: /* Reduce the number of points plotted by eliminating duplicate values: tomwalters@228: * tomwalters@228: * oooo o--o tomwalters@228: * o / o tomwalters@228: * oooo ooo => o--o o--o tomwalters@228: * oooo ooo o--o o-o tomwalters@228: * tomwalters@228: * with 'o' points that are plotted, and '-' by the graphics output tomwalters@228: * device interpolated points. We could be smarter and include tomwalters@228: * first-order interpolation, but we leave that as an exercise for tomwalters@228: * the reader. Please send your patches :) tomwalters@228: * tomwalters@228: * So, we don't draw points that are too close to the previous value. tomwalters@228: * But if the value changes (or it's the last point), we must draw the tomwalters@228: * previous point too. tomwalters@228: */ tomwalters@228: if (!m_bFirstPoint tomwalters@116: && !isLast tom@229: && abs(m_fPrevVal-val) < m_fMinPlotDistance) { tomwalters@228: m_iPrevValEqual++; tomwalters@228: // Don't set m_fPrevVal to avoid not catching slow changes tomwalters@228: m_fPrevX = x; tomwalters@228: m_fPrevY = y; tomwalters@228: m_fPrevHeight = height; tomwalters@228: return; tomwalters@228: } else { tomwalters@228: if (m_iPrevValEqual > 0) { tomwalters@228: // Draw previous point tomwalters@228: PlotDataPointDirect(m_fPrevX, m_fPrevY, m_fPrevVal, m_fPrevHeight); tomwalters@228: } tomwalters@228: m_iPrevValEqual = 0; tomwalters@228: m_fPrevVal = val; tomwalters@228: m_bFirstPoint = false; tomwalters@228: } tomwalters@228: PlotDataPointDirect(x, y, val, height); tomwalters@116: } tomwalters@116: tomwalters@116: void GraphicsView::PlotDataPointDirect(float x, tomwalters@116: float y, tomwalters@116: float val, tomwalters@116: float height) { tomwalters@228: // Draw it in the right way tomwalters@228: switch(m_iGraphType) { tomwalters@228: case GraphTypeLine: tomwalters@228: m_pDev->gVertex2f(x, y + val * height); tomwalters@228: break; tomwalters@228: case GraphTypeColormap: tomwalters@228: //! \todo make it a real colormap instead of grayscale tomwalters@228: m_pDev->gColor3f(val + 0.5, val + 0.5, val + 0.5); tomwalters@228: m_pDev->gVertex2f(x, y - height / 2); tomwalters@228: m_pDev->gVertex2f(x, y + height / 2); tomwalters@228: break; tomwalters@228: case GraphTypeNone: tomwalters@228: // Nothing: just for testing computation overhead of graphing tomwalters@228: break; tomwalters@228: default: tomwalters@228: // Shouldn't happen tomwalters@228: AIM_ASSERT(0); tomwalters@228: } tomwalters@116: } tom@229: } // namespace aimc