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