tomwalters@116: // Copyright 2006-2010, Willem van Engen, Thomas Walters 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: tomwalters@116: /*! tomwalters@116: * \file tomwalters@116: * \brief Time-representation graphics view tomwalters@116: * tomwalters@116: * \author Willem van Engen tomwalters@116: * \date created 2006/09/26 tomwalters@116: * \version \$Id: GraphicsViewTime.cpp 607 2008-06-25 00:14:14Z tom $ tomwalters@116: */ tomwalters@116: tomwalters@116: #include "Support/Common.h" tomwalters@116: tomwalters@116: #include tomwalters@116: tomwalters@116: #include "Support/SignalBank.h" tomwalters@116: #include "Modules/Output/Graphics/GraphicsView.h" tomwalters@116: #include "Modules/Output/Graphics/GraphicsViewTime.h" tomwalters@116: tom@229: namespace aimc { tom@229: tomwalters@116: GraphicsViewTime::GraphicsViewTime(Parameters *pParam) tomwalters@228: : GraphicsView(pParam) { tomwalters@232: module_description_ = "Graphics output."; tomwalters@232: module_identifier_ = "graphics_time"; tomwalters@232: module_type_ = "output"; tomwalters@232: module_version_ = "$Id: $"; tomwalters@116: } tomwalters@116: tomwalters@116: GraphicsViewTime *GraphicsViewTime::Clone(GraphicsOutputDevice *pDev) { tomwalters@237: GraphicsViewTime *pView = new GraphicsViewTime(parameters_); tomwalters@228: // Copy everything tomwalters@228: pView->m_pAxisX->SetDisplayRange(m_pAxisX->m_fMax, m_pAxisX->m_fMin); tomwalters@228: pView->m_pAxisX->SetDisplayScale(m_pAxisX->m_pScale->getType()); tomwalters@228: pView->m_pAxisY->SetDisplayRange(m_pAxisY->m_fMax, m_pAxisY->m_fMin); tomwalters@228: pView->m_pAxisY->SetDisplayScale(m_pAxisY->m_pScale->getType()); tomwalters@228: pView->m_pAxisFreq->SetDisplayRange(m_pAxisFreq->m_fMax, m_pAxisFreq->m_fMin); tomwalters@228: pView->m_pAxisFreq->SetDisplayScale(m_pAxisFreq->m_pScale->getType()); tomwalters@228: return pView; tomwalters@116: } tomwalters@116: tomwalters@116: void GraphicsViewTime::PlotAxes(const SignalBank &bank) { tomwalters@228: m_pDev->gColor3f(0.0f, 0.7f, 0.7f); tomwalters@228: // Vertical axis tomwalters@228: m_pDev->gBeginLineStrip(); tomwalters@228: m_pDev->gVertex2f(m_fMarginLeft, m_fMarginBottom); tomwalters@228: m_pDev->gVertex2f(m_fMarginLeft, 1.0f - m_fMarginTop); tomwalters@228: m_pDev->gEnd(); tomwalters@228: // Horizontal axis tomwalters@228: m_pDev->gBeginLineStrip(); tomwalters@228: m_pDev->gVertex2f(m_fMarginLeft, m_fMarginBottom); tomwalters@228: m_pDev->gVertex2f(1.0f-m_fMarginRight, m_fMarginBottom); tomwalters@228: m_pDev->gEnd(); tomwalters@116: tomwalters@237: //if (!m_bPlotLabels) tomwalters@237: // return; tomwalters@116: tomwalters@228: // Labels tomwalters@228: char sTxt[80]; tomwalters@228: snprintf(sTxt, sizeof(sTxt) / sizeof(sTxt[0]), tomwalters@116: _S("%s [%.0f..%.0f Hz, %s scale]"), tomwalters@228: m_pAxisFreq->m_sLabel ? m_pAxisFreq->m_sLabel : "", tomwalters@228: m_pAxisFreq->m_fMin, m_pAxisFreq->m_fMax, tomwalters@228: m_pAxisFreq->m_pScale->getName()); tomwalters@228: m_pDev->gText2f(0.0025f, 0.35f, sTxt, true); tom@229: snprintf(sTxt, sizeof(sTxt) / sizeof(sTxt[0]), tomwalters@232: _S("%s [%.2f..%.2f ms, %s scale]"), tomwalters@232: m_pAxisX->m_sLabel ? m_pAxisX->m_sLabel : "", tomwalters@232: m_pAxisX->m_fMin, tomwalters@232: m_pAxisX->m_fMax, tomwalters@232: m_pAxisX->m_pScale->getName()); tomwalters@256: tomwalters@228: m_pDev->gText2f(m_fMarginLeft, 0.0025f, sTxt, false); tomwalters@145: tomwalters@228: // Frame time tomwalters@237: snprintf(sTxt, sizeof(sTxt)/sizeof(sTxt[0]), _S("t=%.0f ms"), tomwalters@237: 1000.0 * bank.start_time() / bank.sample_rate()); tomwalters@237: m_pDev->gText2f(0.8f, 0.0025f, sTxt, false); tomwalters@116: } tomwalters@116: tomwalters@508: void GraphicsViewTime::PlotStrobes(const vector& signal, tomwalters@508: const vector& strobes, tomwalters@508: float sample_rate, tomwalters@508: float y_offset, tomwalters@508: float height, tomwalters@508: float x_scale, tomwalters@508: float diameter) { tomwalters@508: x_scale *= 1000.0 / sample_rate; tomwalters@508: m_pDev->gColor3f(1.0f, 0.0f, 0.0f); tomwalters@508: for (vector::const_iterator i = strobes.begin(); tomwalters@508: i != strobes.end(); ++i) { tomwalters@508: float x = *i * x_scale; tomwalters@508: float y = signal[*i]; tomwalters@508: x = m_pAxisX->m_pScale->FromLinearScaled(x) + 0.5f; tomwalters@508: y = m_pAxisY->m_pScale->FromLinearScaled(y); tomwalters@508: tomwalters@508: if (x < 0.0) tomwalters@508: continue; tomwalters@508: tomwalters@508: // Now fit it into the drawing area. tomwalters@508: x = x * (1.0f - m_fMarginLeft - m_fMarginRight) + m_fMarginLeft; // fit inside x-axis area tomwalters@508: PlotStrobe(x, y_offset, y, height, diameter); tomwalters@508: } tomwalters@508: } tomwalters@508: tomwalters@116: void GraphicsViewTime::PlotData(const vector &signal, tomwalters@116: float sample_rate, tomwalters@116: float yOffset, tomwalters@116: float height, tomwalters@116: float xScale) { tomwalters@228: AIM_ASSERT(xScale >= 0 && xScale <= 1); tomwalters@228: AIM_ASSERT(height > 0 && height <= 1); tomwalters@228: AIM_ASSERT(yOffset >= 0 && yOffset <= 1); tomwalters@228: AIM_ASSERT(m_pAxisX && m_pAxisX->m_pScale); tomwalters@228: AIM_ASSERT(m_pAxisY && m_pAxisY->m_pScale); tomwalters@116: tomwalters@228: // Make sure we get time is ms as x value tomwalters@228: xScale *= 1000.0 / sample_rate; tomwalters@228: m_pDev->gColor3f(1.0f, 1.0f, 0.8f); tomwalters@228: BeginDataStrip(); tomwalters@116: tomwalters@228: // Draw the signal. tomwalters@228: float x = 0; tomwalters@228: float y = 0; tom@229: for (unsigned int i = 0; i < signal.size(); i++) { tomwalters@228: // Find out where to draw and do so tomwalters@228: x = xScale * i; tomwalters@228: y = signal[i]; tomwalters@228: x = m_pAxisX->m_pScale->FromLinearScaled(x) + 0.5f; tomwalters@228: y = m_pAxisY->m_pScale->FromLinearScaled(y); tomwalters@116: tomwalters@228: if (x < 0.0) tomwalters@116: continue; tomwalters@116: tomwalters@228: // Now fit it into the drawing area tomwalters@228: x = x * (1.0f - m_fMarginLeft - m_fMarginRight) + m_fMarginLeft; // fit inside x-axis area tomwalters@228: PlotDataPoint(x, yOffset, y, height, false); tomwalters@228: /* There's no point in drawing anything when x>1.0, outside of view. tomwalters@228: * x is continuously increasing, so we can just stop completely. We need to tomwalters@228: * plot this point however, to finish the final line. */ tomwalters@228: if (x > 1.0) tomwalters@228: break; tomwalters@228: } tomwalters@228: // Redraw the last point in case it's needed tomwalters@228: PlotDataPoint(x, yOffset, y, height, true); tomwalters@116: tomwalters@228: m_pDev->gEnd(); tomwalters@116: } tom@229: } // namespace aimc