Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
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
00089
00090
00091
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);
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 uint16_t bands=_beatController->getAnalysers().at(idx)->getBands();
00106 for(uint16_t i=0;i<bands;i++)
00107 {
00108
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
00127
00128
00129
00130
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
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 }