$search
00001 /**************************************************************** 00002 * 00003 * Copyright (c) 2010 00004 * 00005 * Fraunhofer Institute for Manufacturing Engineering 00006 * and Automation (IPA) 00007 * 00008 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00009 * 00010 * Project name: care-o-bot 00011 * ROS stack name: cob_driver 00012 * ROS package name: cob_light 00013 * Description: Switch robots led color by sending data to 00014 * the led-µC over serial connection. 00015 * 00016 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00017 * 00018 * Author: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de 00019 * Supervised by: Benjamin Maidel, email:benjamin.maidel@ipa.fraunhofer.de 00020 * 00021 * Date of creation: August 2012 00022 * ToDo: 00023 * 00024 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00025 * 00026 * Redistribution and use in source and binary forms, with or without 00027 * modification, are permitted provided that the following conditions are met: 00028 * 00029 * * Redistributions of source code must retain the above copyright 00030 * notice, this list of conditions and the following disclaimer. 00031 * * Redistributions in binary form must reproduce the above copyright 00032 * notice, this list of conditions and the following disclaimer in the 00033 * documentation and/or other materials provided with the distribution. 00034 * * Neither the name of the Fraunhofer Institute for Manufacturing 00035 * Engineering and Automation (IPA) nor the names of its 00036 * contributors may be used to endorse or promote products derived from 00037 * this software without specific prior written permission. 00038 * 00039 * This program is free software: you can redistribute it and/or modify 00040 * it under the terms of the GNU Lesser General Public License LGPL as 00041 * published by the Free Software Foundation, either version 3 of the 00042 * License, or (at your option) any later version. 00043 * 00044 * This program is distributed in the hope that it will be useful, 00045 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00046 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00047 * GNU Lesser General Public License LGPL for more details. 00048 * 00049 * You should have received a copy of the GNU Lesser General Public 00050 * License LGPL along with this program. 00051 * If not, see <http://www.gnu.org/licenses/>. 00052 * 00053 ****************************************************************/ 00054 00055 #include <spektrumviz.h> 00056 #include <iomanip> 00057 00058 SpektrumViz::SpektrumViz(mybeat::BeatController* beatController) 00059 { 00060 _beatController = beatController; 00061 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 4) 00062 signal_expose_event().connect(sigc::mem_fun(*this, &SpektrumViz::on_expose_event), false); 00063 #elif (GTKMM_MAJOR_VERSION == 3) 00064 signal_draw().connect(sigc::mem_fun(*this, &SpektrumViz::on_draw), false); 00065 #endif 00066 _beatController->signalProcessingDone()->connect(boost::bind(&SpektrumViz::soundProcessingDone,this)); 00067 m_fps.start(); 00068 } 00069 SpektrumViz::~SpektrumViz(){;} 00070 00071 00072 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION >= 4) 00073 bool SpektrumViz::on_expose_event(GdkEventExpose* event) 00074 { 00075 Glib::RefPtr<Gdk::Window> window = get_window(); 00076 Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context(); 00077 #elif (GTKMM_MAJOR_VERSION == 3) 00078 bool SpektrumViz::on_draw(const Cairo::RefPtr<Cairo::Context>& cr) 00079 { 00080 #endif 00081 static int fps_counter = 0; 00082 static double time_elapsed = 0; 00083 static double fps_time = 0; 00084 Gtk::Allocation allocation = get_allocation(); 00085 const int width = allocation.get_width(); 00086 const int height = allocation.get_height(); 00087 00088 // scale to unit square and translate (0, 0) to be (0.5, 0.5), i.e. 00089 // the center of the window 00090 00091 //left side 00092 for(uint16_t idx=0; idx < _beatController->getAnalysers().size(); idx++) 00093 { 00094 cr->set_line_width(2); 00095 cr->set_source_rgba(0.423, 0.482, 0.545, 0.8); // gray 00096 00097 // for(uint16_t i=0;i<DEFAULT_RECORD_SIZE/2;i++) 00098 // { 00099 // //Draw the function itself 00100 // cr->move_to( ((double)i/(DEFAULT_RECORD_SIZE/DEFAULT_CHANNELS-1.0)*(double)width/2.0) + (((double)width/2.0)*idx), (double)height ); 00101 // cr->line_to( ((double)i/(DEFAULT_RECORD_SIZE/DEFAULT_CHANNELS-1.0)*(double)width/2.0) + (((double)width/2.0)*idx), (double)height- (_beatController->getFFTs().at(idx)->get_magnitude(i)/_beatController->getFFTs().at(idx)->get_magnitude_max())*(double)height); 00102 // cr->stroke(); 00103 // } 00104 00105 uint16_t bands=_beatController->getAnalysers().at(idx)->getBands(); 00106 for(uint16_t i=0;i<bands;i++) 00107 { 00108 //Draw the function itself 00109 cr->move_to( ((double)i/(bands-1))*((double)width/2.0) + (((double)width/2.0)*idx), (double)height ); 00110 cr->line_to( ((double)i/(bands-1))*((double)width/2.0) + (((double)width/2.0)*idx), (double)height- (_beatController->getAnalysers().at(idx)->getBand(i)->getNewest()/_beatController->getAnalysers().at(idx)->getMaxBandValue())*(double)height); 00111 cr->stroke(); 00112 } 00113 00114 cr->set_source_rgba(1, 0, 0, 0.8); 00115 cr->arc(0+(((double)width/2.0)*idx), ((double)height-(_beatController->getAnalysers().at(idx)->getBand(0)->getAllTimeMaximumRaw()/_beatController->getAnalysers().at(idx)->getMaxBandValue())*(double)height), 00116 1.5, 0, 2*M_PI); 00117 cr->fill(); 00118 for(uint16_t i=1;i<bands;i++) 00119 { 00120 cr->arc( ((double)i/(bands-1)*(double)width/2.0) + (((double)width/2.0)*idx), (double)height-(_beatController->getAnalysers().at(idx)->getBand(i)->getAllTimeMaximumRaw()/_beatController->getAnalysers().at(idx)->getMaxBandValue())*(double)height, 00121 1.5, 0, 2*M_PI); 00122 cr->fill(); 00123 } 00124 00125 00126 // cr->set_source_rgba(0,1,0,0.8); 00127 // cr->set_line_width(5); 00128 // cr->move_to((25.0 + ((double)width/2.0)*idx), (double)height/3.0); 00129 // cr->line_to((25.0 + ((double)width/2.0)*idx) + _beatController->getAnalysers().at(idx)->getMagSpectrum(50, 2000)*75.0, (double)height/3.0); 00130 // cr->stroke(); 00131 00132 cr->set_source_rgba(0,1,0,0.8); 00133 cr->set_line_width(5); 00134 cr->move_to((160.0 + ((double)width/2.0)*idx), (double)height/3.0); 00135 cr->line_to((160.0 + ((double)width/2.0)*idx) + ((_beatController->getBuffers().at(idx)->pwr() / _beatController->getBuffers().at(idx)->max_pwr())*75.0) , (double)height/3.0); 00136 cr->stroke(); 00137 00138 cr->set_source_rgba(1,0,0, (_beatController->getBuffers().at(idx)->pwr() / _beatController->getBuffers().at(idx)->max_pwr())); 00139 cr->arc((double)width/4.0 + ((double)width/2.0)*idx, (double)height/2.0, 20, 0, 2*M_PI); 00140 cr->fill(); 00141 00142 m_fps.stop(); 00143 time_elapsed += m_fps.elapsed(); 00144 m_fps.start(); 00145 fps_counter ++; 00146 if(fps_counter >= 10) 00147 { 00148 fps_time = 10.0/time_elapsed; 00149 time_elapsed = 0; 00150 fps_counter = 0; 00151 } 00152 cr->save(); 00153 cr->set_source_rgba(0.25, 0.25, 0.25, 0.85); 00154 cr->select_font_face("Cairo", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); 00155 cr->set_font_size(10); 00156 cr->move_to(width-66, 20); 00157 std::stringstream ss; 00158 ss << std::fixed << std::setprecision(2) << fps_time; 00159 std::string str("fps: " + ss.str()); 00160 cr->show_text(str.c_str()); 00161 cr->restore(); 00162 } 00163 00164 return true; 00165 } 00166 00167 void SpektrumViz::soundProcessingDone() 00168 { 00169 // force our program to redraw the entire clock. 00170 Glib::RefPtr<Gdk::Window> win = get_window(); 00171 if (win) 00172 { 00173 Gdk::Rectangle r(0, 0, get_allocation().get_width(), 00174 get_allocation().get_height()); 00175 win->invalidate_rect(r, false); 00176 } 00177 }