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"
25 #include <QVBoxLayout>
34 AudioSignal::AudioSignal(QWidget *parent): AbstractAudioScopeWidget(false, parent)
38 dbscale << 0 << -1 << -2 << -3 << -4 << -5 << -6 << -8 << -10 << -20 << -40 ;
39 m_menu->removeAction(m_aRealtime);
40 connect(&m_timer,SIGNAL(timeout()),this,SLOT(slotNoAudioTimeout()));
44 AudioSignal::~AudioSignal()
48 QImage AudioSignal::renderAudioScope(uint, const QVector<int16_t> audioFrame,
49 const int, const int num_channels, const int samples, const int)
51 QTime start = QTime::currentTime();
53 int num_samples = samples > 200 ? 200 : samples;
56 for (int i = 0; i < num_channels; ++i) {
58 for (int s = 0; s < num_samples; s ++) {
59 val += abs(audioFrame[i+s*num_channels] / 128);
61 channels.append(val / num_samples);
64 if (peeks.count()!=channels.count()){
65 peeks=QByteArray(channels.count(),0);
66 peekage=QByteArray(channels.count(),0);
68 for (int chan=0;chan<peeks.count();chan++)
70 peekage[chan] = peekage[chan]+1;
71 if ( peeks.at(chan)<channels.at(chan) || peekage.at(chan)>50 )
74 peeks[chan]=channels[chan];
78 QImage image(m_scopeRect.size(), QImage::Format_ARGB32);
79 image.fill(Qt::transparent);
82 p.setRenderHint(QPainter::TextAntialiasing, false);
83 p.setRenderHint(QPainter::Antialiasing, false);
85 int numchan = channels.size();
86 bool horiz=width() > height();
88 bool showdb=width()>(dbsize+40);
89 //valpixel=1.0 for 127, 1.0+(1/40) for 1 short oversample, 1.0+(2/40) for longer oversample
90 for (int i = 0; i < numchan; ++i) {
91 //int maxx= (unsigned char)channels[i] * (horiz ? width() : height() ) / 127;
92 double valpixel=valueToPixel((double)(unsigned char)channels[i]/127.0);
93 int maxx= height() * valpixel;
94 int xdelta= height() /42 ;
95 int _y2= (showdb?width()-dbsize:width () ) / numchan - 1 ;
96 int _y1= (showdb?width()-dbsize:width() ) *i/numchan;
97 int _x2= maxx > xdelta ? xdelta - 3 : maxx - 3 ;
100 showdb=height()>(dbsize);
101 maxx=width()*valpixel;
102 xdelta = width() / 42;
103 _y2=( showdb?height()-dbsize:height() ) / numchan - 1 ;
104 _y1= (showdb?height()-dbsize:height() ) * i/numchan;
105 _x2= maxx > xdelta ? xdelta - 1 : maxx - 1;
108 for (int x = 0; x <= 42; x++) {
110 QColor sig=Qt::green;
111 //value of actual painted digit
112 double ival=(double)_x1/(double)xdelta/42.0;
113 if (ival > 40.0/42.0){
115 }else if ( ival > 37.0/42.0){
117 }else if ( ival >30.0/42.0){
122 p.fillRect(_x1, _y1, _x2, _y2, QBrush(sig, Qt::SolidPattern) );
124 p.fillRect(_y1, height()-_x1, _y2,-_x2, QBrush(sig, Qt::SolidPattern) );
129 int xp=valueToPixel((double)peeks.at(i)/127.0)*(horiz?width():height())-2;
130 p.fillRect(horiz?xp:_y1,horiz?_y1:height()-xdelta-xp,horiz?3:_y2,horiz?_y2:3,QBrush(Qt::gray,Qt::SolidPattern));
134 //draw db value at related pixel
135 for (int l=0;l<dbscale.size();l++){
137 double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)height();
138 p.drawText(width()-20,height()-xf*40.0/42.0+20, QString().sprintf("%d",dbscale.at(l)));
140 double xf=pow(10.0,(double)dbscale.at(l) / 20.0 )*(double)width();
141 p.drawText(xf*40/42-10,height()-2, QString().sprintf("%d",dbscale.at(l)));
146 emit signalScopeRenderingFinished((uint) start.elapsed(), 1);
150 QRect AudioSignal::scopeRect() { return QRect(0, 0, width(), height()); }
152 QImage AudioSignal::renderHUD(uint) { return QImage(); }
153 QImage AudioSignal::renderBackground(uint) { return QImage(); }
155 void AudioSignal::slotReceiveAudio(QVector<int16_t> data, int, int num_channels, int samples)
158 int num_samples = samples > 200 ? 200 : samples;
161 int num_oversample=0;
162 for (int i = 0; i < num_channels; ++i) {
166 for (int s = 0; s < num_samples; s ++) {
167 int sample=abs(data[i+s*num_channels] / 128);
174 //if 3 samples over max => 1 peak over 0 db (0db=40.0)
175 if (num_oversample>3){
178 // 10 samples @max => show max signal
179 if (num_oversample>10){
186 //max amplitude = 40/42, 3to10 oversamples=41, more then 10 oversamples=42
188 channels.append(over2);
189 } else if (over1>0.0){
190 channels.append(over1);
192 channels.append(val / num_samples*40.0/42.0);
198 void AudioSignal::slotNoAudioTimeout(){
200 showAudio(QByteArray(2,0));
204 void AudioSignal::showAudio(const QByteArray arr)
207 if (peeks.count()!=channels.count()){
208 peeks=QByteArray(channels.count(),0);
209 peekage=QByteArray(channels.count(),0);
211 for (int chan=0;chan<peeks.count();chan++)
213 peekage[chan]=peekage[chan]+1;
214 if ( peeks.at(chan)<arr.at(chan) || peekage.at(chan)>50 )
217 peeks[chan]=arr[chan];
223 double AudioSignal::valueToPixel(double in)
225 //in=0 -> return 0 (null length from max), in=127/127 return 1 (max length )
226 return 1.0- log10( in)/log10(1.0/127.0);
229 #include "audiosignal.moc"