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 dbscale << 0 << -1 << -2 << -3 << -4 << -5 << -6 << -8 << -10 << -20 << -40 ;
41 setContextMenuPolicy(Qt::ActionsContextMenu);
42 m_aMonitoringEnabled = new QAction(i18n("Monitor audio signal"), this);
43 m_aMonitoringEnabled->setCheckable(true);
44 m_aMonitoringEnabled->setChecked(true);
45 connect(m_aMonitoringEnabled, SIGNAL(toggled(bool)), this, SLOT(slotSwitchAudioMonitoring(bool)));
46 connect(&m_timer,SIGNAL(timeout()),this,SLOT(slotNoAudioTimeout()));
47 addAction(m_aMonitoringEnabled);
50 AudioSignal::~AudioSignal()
52 delete m_aMonitoringEnabled;
55 bool AudioSignal::monitoringEnabled() const
57 return m_aMonitoringEnabled->isChecked();
60 void AudioSignal::slotReceiveAudio(const QVector<int16_t>& data, int, int num_channels, int samples){
62 int num_samples = samples > 200 ? 200 : samples;
66 for (int i = 0; i < num_channels; i++) {
70 for (int s = 0; s < num_samples; s ++) {
71 int sample=abs(data[i+s*num_channels] / 128);
78 //if 3 samples over max => 1 peak over 0 db (0db=40.0)
79 if (num_oversample>3){
82 // 10 samples @max => show max signal
83 if (num_oversample>10){
90 //max amplitude = 40/42, 3to10 oversamples=41, more then 10 oversamples=42
92 channels.append(over2);
93 } else if (over1>0.0){
94 channels.append(over1);
96 channels.append(val / num_samples*40.0/42.0);
102 void AudioSignal::slotNoAudioTimeout(){
104 showAudio(QByteArray(2,0));
108 void AudioSignal::showAudio(const QByteArray arr)
111 if (peeks.count()!=channels.count()){
112 peeks=QByteArray(channels.count(),0);
113 peekage=QByteArray(channels.count(),0);
115 for (int chan=0;chan<peeks.count();chan++)
117 peekage[chan]=peekage[chan]+1;
118 if ( peeks.at(chan)<arr.at(chan) || peekage.at(chan)>50 )
121 peeks[chan]=arr[chan];
127 double AudioSignal::valueToPixel(double in)
129 //in=0 -> return 0 (null length from max), in=127/127 return 1 (max length )
130 return 1.0- log10( in)/log10(1.0/127.0);
133 void AudioSignal::paintEvent(QPaintEvent* /*e*/)
135 if (!m_aMonitoringEnabled->isChecked()) {
139 int numchan = channels.size();
140 bool horiz=width() > height();
142 bool showdb=width()>(dbsize+40);
143 //valpixel=1.0 for 127, 1.0+(1/40) for 1 short oversample, 1.0+(2/40) for longer oversample
144 for (int i = 0; i < numchan; i++) {
145 //int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
146 double valpixel=valueToPixel((double)(unsigned char)channels[i]/127.0);
147 int maxx= height() * valpixel;
148 int xdelta= height() /42 ;
149 int _y2= (showdb?width()-dbsize:width () ) / numchan - 1 ;
150 int _y1= (showdb?width()-dbsize:width() ) *i/numchan;
151 int _x2= maxx > xdelta ? xdelta - 3 : maxx - 3 ;
154 showdb=height()>(dbsize);
155 maxx=width()*valpixel;
156 xdelta = width() / 42;
157 _y2=( showdb?height()-dbsize:height() ) / numchan - 1 ;
158 _y1= (showdb?height()-dbsize:height() ) * i/numchan;
159 _x2= maxx > xdelta ? xdelta - 1 : maxx - 1;
162 for (int x = 0; x <= 42; x++) {
164 QColor sig=Qt::green;
165 //value of actual painted digit
166 double ival=(double)_x1/(double)xdelta/42.0;
167 if (ival > 40.0/42.0){
169 }else if ( ival > 37.0/42.0){
171 }else if ( ival >30.0/42.0){
176 p.fillRect(_x1, _y1, _x2, _y2, QBrush(sig, Qt::SolidPattern) );
178 p.fillRect(_y1, height()-_x1, _y2,-_x2, QBrush(sig, Qt::SolidPattern) );
183 int xp=valueToPixel((double)peeks.at(i)/127.0)*(horiz?width():height())-2;
184 p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::black,Qt::SolidPattern));
188 //draw db value at related pixel
189 for (int l=0;l<dbscale.size();l++){
191 double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)height();
192 p.drawText(width()-20,height()-xf*40.0/42.0+20, QString().sprintf("%d",dbscale.at(l)));
194 double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)width();
195 p.drawText(xf*40/42-10,height()-2, QString().sprintf("%d",dbscale.at(l)));
202 void AudioSignal::slotSwitchAudioMonitoring(bool)
204 emit updateAudioMonitoring();
207 #include "audiosignal.moc"