1 /***************************************************************************
2 * Copyright (C) 2010 by Marco Gittler (g.marco@freenet.de) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "audiosignal.h"
24 #include <QVBoxLayout>
33 AudioSignal::AudioSignal(QWidget *parent): QWidget(parent)
35 //QVBoxLayout *vbox=new QVBoxLayout(this);
37 //vbox->addWidget(label);
40 col << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green << Qt::green ;
41 col << Qt::yellow << Qt::yellow << Qt::yellow << Qt::yellow << Qt::yellow ;
42 col << Qt::darkYellow << Qt::darkYellow << Qt::darkYellow;
43 col << Qt::red << Qt::red;
44 setContextMenuPolicy(Qt::ActionsContextMenu);
45 m_aMonitoringEnabled = new QAction(i18n("Monitor audio signal"), this);
46 m_aMonitoringEnabled->setCheckable(true);
47 connect(m_aMonitoringEnabled, SIGNAL(toggled(bool)), this, SLOT(slotSwitchAudioMonitoring(bool)));
48 addAction(m_aMonitoringEnabled);
51 AudioSignal::~AudioSignal()
53 delete m_aMonitoringEnabled;
56 bool AudioSignal::monitoringEnabled() const
58 return m_aMonitoringEnabled->isChecked();
61 void AudioSignal::slotReceiveAudio(const QVector<int16_t>& data, int, int num_channels, int samples){
63 int num_samples = samples > 200 ? 200 : samples;
66 for (int i = 0; i < num_channels; i++) {
68 for (int s = 0; s < num_samples; s ++) {
69 val += abs(data[i+s*num_channels] / 128);
71 channels.append(val / num_samples);
75 void AudioSignal::showAudio(const QByteArray arr)
78 if (peeks.count()!=channels.count()){
79 peeks=QByteArray(channels.count(),0);
80 peekage=QByteArray(channels.count(),0);
82 for (int chan=0;chan<peeks.count();chan++)
84 peekage[chan]=peekage[chan]+1;
85 if ( peeks.at(chan)<arr.at(chan) || peekage.at(chan)>50 )
88 peeks[chan]=arr[chan];
94 double AudioSignal::valueToPixel(double in,bool db)
98 // ratio db(in)/db(0.01) (means: min db value = -40.0 )
99 return 1.0- log10( in)/log10(0.01);
107 void AudioSignal::paintEvent(QPaintEvent* /*e*/)
109 if (!m_aMonitoringEnabled->isChecked()) {
114 //p.fillRect(0,0,(unsigned char)channels[0]*width()/255,height()/2,QBrush(Qt::SolidPattern));
115 //p.fillRect(0,height()/2,(unsigned char)channels[1]*width()/255,height()/2,QBrush(Qt::SolidPattern));
116 int numchan = channels.size();
117 bool db=true; // show values in db(i)
118 bool horiz=width() > height();
119 for (int i = 0; i < numchan; i++) {
120 //int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
121 int maxx= (horiz ? width() : height() ) * valueToPixel((double)(unsigned char)channels[i]/127.0,db);
122 int xdelta=(horiz ? width():height() ) /20 ;
123 int _y2= (horiz ? height() :width () ) / numchan - 1 ;
124 int _y1=(horiz ? height():width() ) *i/numchan;
125 int _x2=maxx > xdelta ? xdelta - (horiz?1:3) : maxx - (horiz ?1 :3 );
127 for (int x = 0; x < 20; x++) {
130 //p.fillRect(x * xdelta, y1, maxx > xdelta ? xdelta - 1 : maxx - 1, _h, QBrush(col.at(x), Qt::SolidPattern));
131 p.fillRect(horiz?_x1:_y1, horiz?_y1:height()-_x1, horiz?_x2:_y2,horiz? _y2:-_x2, QBrush(col.at(x), Qt::SolidPattern));
135 int xp=valueToPixel((double)peeks.at(i)/127.0,db)*(horiz?width():height())-2;
136 p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::black,Qt::SolidPattern));
141 void AudioSignal::slotSwitchAudioMonitoring(bool)
143 emit updateAudioMonitoring();
146 #include "audiosignal.moc"