annotate trunk/src/Modules/Output/Graphics/GraphicsViewTime.cc @ 706:f8e90b5d85fd tip

Delete CARFAC code from this repository. It has been moved to https://github.com/google/carfac Please email me with your github username to get access. I've also created a new mailing list to discuss CARFAC development: https://groups.google.com/forum/#!forum/carfac-dev
author ronw@google.com
date Thu, 18 Jul 2013 20:56:51 +0000
parents 16a590fbc0ba
children
rev   line source
tomwalters@397 1 // Copyright 2006-2010, Willem van Engen, Thomas Walters
tomwalters@397 2 //
tomwalters@397 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@397 4 // http://www.acousticscale.org/AIMC
tomwalters@397 5 //
tomwalters@397 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@397 7 // you may not use this file except in compliance with the License.
tomwalters@397 8 // You may obtain a copy of the License at
tomwalters@397 9 //
tomwalters@397 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@397 11 //
tomwalters@397 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@397 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@397 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@397 15 // See the License for the specific language governing permissions and
tomwalters@397 16 // limitations under the License.
tomwalters@397 17
tomwalters@397 18 /*!
tomwalters@397 19 * \file
tomwalters@397 20 * \brief Time-representation graphics view
tomwalters@397 21 *
tomwalters@397 22 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@397 23 * \date created 2006/09/26
tomwalters@397 24 * \version \$Id: GraphicsViewTime.cpp 607 2008-06-25 00:14:14Z tom $
tomwalters@397 25 */
tomwalters@397 26
tomwalters@397 27 #include "Support/Common.h"
tomwalters@397 28
tomwalters@397 29 #include <stdio.h>
tomwalters@397 30
tomwalters@397 31 #include "Support/SignalBank.h"
tomwalters@397 32 #include "Modules/Output/Graphics/GraphicsView.h"
tomwalters@397 33 #include "Modules/Output/Graphics/GraphicsViewTime.h"
tomwalters@397 34
tom@399 35 namespace aimc {
tom@399 36
tomwalters@397 37 GraphicsViewTime::GraphicsViewTime(Parameters *pParam)
tomwalters@398 38 : GraphicsView(pParam) {
tomwalters@402 39 module_description_ = "Graphics output.";
tomwalters@402 40 module_identifier_ = "graphics_time";
tomwalters@402 41 module_type_ = "output";
tomwalters@402 42 module_version_ = "$Id: $";
tomwalters@397 43 }
tomwalters@397 44
tomwalters@397 45 GraphicsViewTime *GraphicsViewTime::Clone(GraphicsOutputDevice *pDev) {
tomwalters@411 46 GraphicsViewTime *pView = new GraphicsViewTime(parameters_);
tomwalters@398 47 // Copy everything
tomwalters@398 48 pView->m_pAxisX->SetDisplayRange(m_pAxisX->m_fMax, m_pAxisX->m_fMin);
tomwalters@398 49 pView->m_pAxisX->SetDisplayScale(m_pAxisX->m_pScale->getType());
tomwalters@398 50 pView->m_pAxisY->SetDisplayRange(m_pAxisY->m_fMax, m_pAxisY->m_fMin);
tomwalters@398 51 pView->m_pAxisY->SetDisplayScale(m_pAxisY->m_pScale->getType());
tomwalters@398 52 pView->m_pAxisFreq->SetDisplayRange(m_pAxisFreq->m_fMax, m_pAxisFreq->m_fMin);
tomwalters@398 53 pView->m_pAxisFreq->SetDisplayScale(m_pAxisFreq->m_pScale->getType());
tomwalters@398 54 return pView;
tomwalters@397 55 }
tomwalters@397 56
tomwalters@397 57 void GraphicsViewTime::PlotAxes(const SignalBank &bank) {
tomwalters@398 58 m_pDev->gColor3f(0.0f, 0.7f, 0.7f);
tomwalters@398 59 // Vertical axis
tomwalters@398 60 m_pDev->gBeginLineStrip();
tomwalters@398 61 m_pDev->gVertex2f(m_fMarginLeft, m_fMarginBottom);
tomwalters@398 62 m_pDev->gVertex2f(m_fMarginLeft, 1.0f - m_fMarginTop);
tomwalters@398 63 m_pDev->gEnd();
tomwalters@398 64 // Horizontal axis
tomwalters@398 65 m_pDev->gBeginLineStrip();
tomwalters@398 66 m_pDev->gVertex2f(m_fMarginLeft, m_fMarginBottom);
tomwalters@398 67 m_pDev->gVertex2f(1.0f-m_fMarginRight, m_fMarginBottom);
tomwalters@398 68 m_pDev->gEnd();
tomwalters@397 69
tomwalters@411 70 //if (!m_bPlotLabels)
tomwalters@411 71 // return;
tomwalters@397 72
tomwalters@398 73 // Labels
tomwalters@398 74 char sTxt[80];
tomwalters@398 75 snprintf(sTxt, sizeof(sTxt) / sizeof(sTxt[0]),
tomwalters@397 76 _S("%s [%.0f..%.0f Hz, %s scale]"),
tomwalters@398 77 m_pAxisFreq->m_sLabel ? m_pAxisFreq->m_sLabel : "",
tomwalters@398 78 m_pAxisFreq->m_fMin, m_pAxisFreq->m_fMax,
tomwalters@398 79 m_pAxisFreq->m_pScale->getName());
tomwalters@398 80 m_pDev->gText2f(0.0025f, 0.35f, sTxt, true);
tom@399 81 snprintf(sTxt, sizeof(sTxt) / sizeof(sTxt[0]),
tomwalters@402 82 _S("%s [%.2f..%.2f ms, %s scale]"),
tomwalters@402 83 m_pAxisX->m_sLabel ? m_pAxisX->m_sLabel : "",
tomwalters@402 84 m_pAxisX->m_fMin,
tomwalters@402 85 m_pAxisX->m_fMax,
tomwalters@402 86 m_pAxisX->m_pScale->getName());
tomwalters@443 87
tomwalters@398 88 m_pDev->gText2f(m_fMarginLeft, 0.0025f, sTxt, false);
tomwalters@397 89
tomwalters@398 90 // Frame time
tomwalters@411 91 snprintf(sTxt, sizeof(sTxt)/sizeof(sTxt[0]), _S("t=%.0f ms"),
tomwalters@411 92 1000.0 * bank.start_time() / bank.sample_rate());
tomwalters@411 93 m_pDev->gText2f(0.8f, 0.0025f, sTxt, false);
tomwalters@397 94 }
tomwalters@397 95
tomwalters@569 96 void GraphicsViewTime::PlotStrobes(const vector<float>& signal,
tomwalters@569 97 const vector<int>& strobes,
tomwalters@569 98 float sample_rate,
tomwalters@569 99 float y_offset,
tomwalters@569 100 float height,
tomwalters@569 101 float x_scale,
tomwalters@569 102 float diameter) {
tomwalters@569 103 x_scale *= 1000.0 / sample_rate;
tomwalters@569 104 m_pDev->gColor3f(1.0f, 0.0f, 0.0f);
tomwalters@569 105 for (vector<int>::const_iterator i = strobes.begin();
tomwalters@569 106 i != strobes.end(); ++i) {
tomwalters@569 107 float x = *i * x_scale;
tomwalters@569 108 float y = signal[*i];
tomwalters@569 109 x = m_pAxisX->m_pScale->FromLinearScaled(x) + 0.5f;
tomwalters@569 110 y = m_pAxisY->m_pScale->FromLinearScaled(y);
tomwalters@569 111
tomwalters@569 112 if (x < 0.0)
tomwalters@569 113 continue;
tomwalters@569 114
tomwalters@569 115 // Now fit it into the drawing area.
tomwalters@569 116 x = x * (1.0f - m_fMarginLeft - m_fMarginRight) + m_fMarginLeft; // fit inside x-axis area
tomwalters@569 117 PlotStrobe(x, y_offset, y, height, diameter);
tomwalters@569 118 }
tomwalters@569 119 }
tomwalters@569 120
tomwalters@397 121 void GraphicsViewTime::PlotData(const vector<float> &signal,
tomwalters@397 122 float sample_rate,
tomwalters@397 123 float yOffset,
tomwalters@397 124 float height,
tomwalters@397 125 float xScale) {
tomwalters@398 126 AIM_ASSERT(xScale >= 0 && xScale <= 1);
tomwalters@398 127 AIM_ASSERT(height > 0 && height <= 1);
tomwalters@398 128 AIM_ASSERT(yOffset >= 0 && yOffset <= 1);
tomwalters@398 129 AIM_ASSERT(m_pAxisX && m_pAxisX->m_pScale);
tomwalters@398 130 AIM_ASSERT(m_pAxisY && m_pAxisY->m_pScale);
tomwalters@397 131
tomwalters@398 132 // Make sure we get time is ms as x value
tomwalters@398 133 xScale *= 1000.0 / sample_rate;
tomwalters@398 134 m_pDev->gColor3f(1.0f, 1.0f, 0.8f);
tomwalters@398 135 BeginDataStrip();
tomwalters@397 136
tomwalters@398 137 // Draw the signal.
tomwalters@398 138 float x = 0;
tomwalters@398 139 float y = 0;
tom@399 140 for (unsigned int i = 0; i < signal.size(); i++) {
tomwalters@398 141 // Find out where to draw and do so
tomwalters@398 142 x = xScale * i;
tomwalters@398 143 y = signal[i];
tomwalters@398 144 x = m_pAxisX->m_pScale->FromLinearScaled(x) + 0.5f;
tomwalters@398 145 y = m_pAxisY->m_pScale->FromLinearScaled(y);
tomwalters@397 146
tomwalters@398 147 if (x < 0.0)
tomwalters@397 148 continue;
tomwalters@397 149
tomwalters@398 150 // Now fit it into the drawing area
tomwalters@398 151 x = x * (1.0f - m_fMarginLeft - m_fMarginRight) + m_fMarginLeft; // fit inside x-axis area
tomwalters@398 152 PlotDataPoint(x, yOffset, y, height, false);
tomwalters@398 153 /* There's no point in drawing anything when x>1.0, outside of view.
tomwalters@398 154 * x is continuously increasing, so we can just stop completely. We need to
tomwalters@398 155 * plot this point however, to finish the final line. */
tomwalters@398 156 if (x > 1.0)
tomwalters@398 157 break;
tomwalters@398 158 }
tomwalters@398 159 // Redraw the last point in case it's needed
tomwalters@398 160 PlotDataPoint(x, yOffset, y, height, true);
tomwalters@397 161
tomwalters@398 162 m_pDev->gEnd();
tomwalters@397 163 }
tom@399 164 } // namespace aimc