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