forked from jamulussoftware/jamulus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
executable file
·1398 lines (1219 loc) · 56.8 KB
/
Copy pathutil.cpp
File metadata and controls
executable file
·1398 lines (1219 loc) · 56.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************\
* Copyright (c) 2004-2020
*
* Author(s):
* Volker Fischer
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\******************************************************************************/
#include "util.h"
#include "client.h"
/* Implementation *************************************************************/
// Input level meter implementation --------------------------------------------
void CStereoSignalLevelMeter::Update ( const CVector<short>& vecsAudio )
{
// get the stereo vector size
const int iStereoVecSize = vecsAudio.Size();
// Get maximum of current block
//
// Speed optimization:
// - we only make use of the positive values and ignore the negative ones
// -> we do not need to call the fabs() function
// - we only evaluate every third sample
//
// With these speed optimizations we might loose some information in
// special cases but for the average music signals the following code
// should give good results.
//
short sMaxL = 0;
short sMaxR = 0;
for ( int i = 0; i < iStereoVecSize; i += 6 ) // 2 * 3 = 6 -> stereo
{
// left channel
sMaxL = std::max ( sMaxL, vecsAudio[i] );
// right channel
sMaxR = std::max ( sMaxR, vecsAudio[i + 1] );
}
dCurLevelL = UpdateCurLevel ( dCurLevelL, sMaxL );
dCurLevelR = UpdateCurLevel ( dCurLevelR, sMaxR );
}
double CStereoSignalLevelMeter::UpdateCurLevel ( double dCurLevel,
const short& sMax )
{
// decrease max with time
if ( dCurLevel >= METER_FLY_BACK )
{
// TODO Calculate factor from sample rate and frame size (64 or 128 samples frame size).
// But tests with 128 and 64 samples frame size have shown that the meter fly back
// is ok for both numbers of samples frame size.
dCurLevel *= 0.97;
}
else
{
dCurLevel = 0;
}
// update current level -> only use maximum
if ( static_cast<double> ( sMax ) > dCurLevel )
{
return static_cast<double> ( sMax );
}
else
{
return dCurLevel;
}
}
double CStereoSignalLevelMeter::CalcLogResult ( const double& dLinearLevel )
{
const double dNormMicLevel = dLinearLevel / _MAXSHORT;
// logarithmic measure
if ( dNormMicLevel > 0 )
{
return 20.0 * log10 ( dNormMicLevel );
}
else
{
return -100000.0; // large negative value
}
}
// CRC -------------------------------------------------------------------------
void CCRC::Reset()
{
// init state shift-register with ones. Set all registers to "1" with
// bit-wise not operation
iStateShiftReg = ~uint32_t ( 0 );
}
void CCRC::AddByte ( const uint8_t byNewInput )
{
for ( int i = 0; i < 8; i++ )
{
// shift bits in shift-register for transistion
iStateShiftReg <<= 1;
// take bit, which was shifted out of the register-size and place it
// at the beginning (LSB)
// (If condition is not satisfied, implicitely a "0" is added)
if ( ( iStateShiftReg & iBitOutMask) > 0 )
{
iStateShiftReg |= 1;
}
// add new data bit to the LSB
if ( ( byNewInput & ( 1 << ( 8 - i - 1 ) ) ) > 0 )
{
iStateShiftReg ^= 1;
}
// add mask to shift-register if first bit is true
if ( iStateShiftReg & 1 )
{
iStateShiftReg ^= iPoly;
}
}
}
uint32_t CCRC::GetCRC()
{
// return inverted shift-register (1's complement)
iStateShiftReg = ~iStateShiftReg;
// remove bit which where shifted out of the shift-register frame
return iStateShiftReg & ( iBitOutMask - 1 );
}
/******************************************************************************\
* Audio Reverberation *
\******************************************************************************/
/*
The following code is based on "JCRev: John Chowning's reverberator class"
by Perry R. Cook and Gary P. Scavone, 1995 - 2004
which is in "The Synthesis ToolKit in C++ (STK)"
http://ccrma.stanford.edu/software/stk
Original description:
This class is derived from the CLM JCRev function, which is based on the use
of networks of simple allpass and comb delay filters. This class implements
three series allpass units, followed by four parallel comb filters, and two
decorrelation delay lines in parallel at the output.
*/
void CAudioReverb::Init ( const int iSampleRate,
const double rT60 )
{
int delay, i;
// delay lengths for 44100 Hz sample rate
int lengths[9] = { 1116, 1356, 1422, 1617, 225, 341, 441, 211, 179 };
const double scaler = static_cast<double> ( iSampleRate ) / 44100.0;
if ( scaler != 1.0 )
{
for ( i = 0; i < 9; i++ )
{
delay = static_cast<int> ( floor ( scaler * lengths[i] ) );
if ( ( delay & 1 ) == 0 )
{
delay++;
}
while ( !isPrime ( delay ) )
{
delay += 2;
}
lengths[i] = delay;
}
}
for ( i = 0; i < 3; i++ )
{
allpassDelays[i].Init ( lengths[i + 4] );
}
for ( i = 0; i < 4; i++ )
{
combDelays[i].Init ( lengths[i] );
combFilters[i].setPole ( 0.2 );
}
setT60 ( rT60, iSampleRate );
outLeftDelay.Init ( lengths[7] );
outRightDelay.Init ( lengths[8] );
allpassCoefficient = 0.7;
Clear();
}
bool CAudioReverb::isPrime ( const int number )
{
/*
Returns true if argument value is prime. Taken from "class Effect" in
"STK abstract effects parent class".
*/
if ( number == 2 )
{
return true;
}
if ( number & 1 )
{
for ( int i = 3; i < static_cast<int> ( sqrt ( static_cast<double> ( number ) ) ) + 1; i += 2 )
{
if ( ( number % i ) == 0 )
{
return false;
}
}
return true; // prime
}
else
{
return false; // even
}
}
void CAudioReverb::Clear()
{
// reset and clear all internal state
allpassDelays[0].Reset ( 0 );
allpassDelays[1].Reset ( 0 );
allpassDelays[2].Reset ( 0 );
combDelays[0].Reset ( 0 );
combDelays[1].Reset ( 0 );
combDelays[2].Reset ( 0 );
combDelays[3].Reset ( 0 );
combFilters[0].Reset();
combFilters[1].Reset();
combFilters[2].Reset();
combFilters[3].Reset();
outRightDelay.Reset ( 0 );
outLeftDelay.Reset ( 0 );
}
void CAudioReverb::setT60 ( const double rT60,
const int iSampleRate )
{
// set the reverberation T60 decay time
for ( int i = 0; i < 4; i++ )
{
combCoefficient[i] = pow ( 10.0, static_cast<double> ( -3.0 *
combDelays[i].Size() / ( rT60 * iSampleRate ) ) );
}
}
void CAudioReverb::COnePole::setPole ( const double dPole )
{
// calculate IIR filter coefficients based on the pole value
dA = -dPole;
dB = 1.0 - dPole;
}
double CAudioReverb::COnePole::Calc ( const double dIn )
{
// calculate IIR filter
dLastSample = dB * dIn - dA * dLastSample;
return dLastSample;
}
void CAudioReverb::ProcessSample ( int16_t& iInputOutputLeft,
int16_t& iInputOutputRight,
const double dAttenuation )
{
// compute one output sample
double temp, temp0, temp1, temp2;
// we sum up the stereo input channels (in case mono input is used, a zero
// shall be input for the right channel)
const double dMixedInput = 0.5 * ( iInputOutputLeft + iInputOutputRight );
temp = allpassDelays[0].Get();
temp0 = allpassCoefficient * temp;
temp0 += dMixedInput;
allpassDelays[0].Add ( temp0 );
temp0 = - ( allpassCoefficient * temp0 ) + temp;
temp = allpassDelays[1].Get();
temp1 = allpassCoefficient * temp;
temp1 += temp0;
allpassDelays[1].Add ( temp1 );
temp1 = - ( allpassCoefficient * temp1 ) + temp;
temp = allpassDelays[2].Get();
temp2 = allpassCoefficient * temp;
temp2 += temp1;
allpassDelays[2].Add ( temp2 );
temp2 = - ( allpassCoefficient * temp2 ) + temp;
const double temp3 = temp2 + combFilters[0].Calc ( combCoefficient[0] * combDelays[0].Get() );
const double temp4 = temp2 + combFilters[1].Calc ( combCoefficient[1] * combDelays[1].Get() );
const double temp5 = temp2 + combFilters[2].Calc ( combCoefficient[2] * combDelays[2].Get() );
const double temp6 = temp2 + combFilters[3].Calc ( combCoefficient[3] * combDelays[3].Get() );
combDelays[0].Add ( temp3 );
combDelays[1].Add ( temp4 );
combDelays[2].Add ( temp5 );
combDelays[3].Add ( temp6 );
const double filtout = temp3 + temp4 + temp5 + temp6;
outLeftDelay.Add ( filtout );
outRightDelay.Add ( filtout );
// inplace apply the attenuated reverb signal
iInputOutputLeft = Double2Short (
( 1.0 - dAttenuation ) * iInputOutputLeft +
0.5 * dAttenuation * outLeftDelay.Get() );
iInputOutputRight = Double2Short (
( 1.0 - dAttenuation ) * iInputOutputRight +
0.5 * dAttenuation * outRightDelay.Get() );
}
/******************************************************************************\
* GUI Utilities *
\******************************************************************************/
// About dialog ----------------------------------------------------------------
CAboutDlg::CAboutDlg ( QWidget* parent ) : QDialog ( parent )
{
setupUi ( this );
// general description of software
txvAbout->setText (
"<p>" + tr ( "The " ) + APP_NAME +
tr ( " software enables musicians to perform real-time jam sessions "
"over the internet." ) + "<br>" + tr ( "There is a " ) + APP_NAME + tr ( " "
"server which collects the audio data from each " ) +
APP_NAME + tr ( " client, mixes the audio data and sends the mix back "
"to each client." ) + "</p>"
"<p><font face=\"courier\">" // GPL header text
"This program is free software; you can redistribute it and/or modify "
"it under the terms of the GNU General Public License as published by "
"the Free Software Foundation; either version 2 of the License, or "
"(at your option) any later version.<br>This program is distributed in "
"the hope that it will be useful, but WITHOUT ANY WARRANTY; without "
"even the implied warranty of MERCHANTABILITY or FITNESS FOR A "
"PARTICULAR PURPOSE. See the GNU General Public License for more "
"details.<br>You should have received a copy of the GNU General Public "
"License along with his program; if not, write to the Free Software "
"Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 "
"USA"
"</font></p>" );
// libraries used by this compilation
txvLibraries->setText ( APP_NAME +
tr ( " uses the following libraries, resources or code snippets:" ) +
"<br><p>" + tr ( "Qt cross-platform application framework" ) +
", <i><a href=""http://www.qt.io"">http://www.qt.io</a></i></p>"
"<p>Opus Interactive Audio Codec"
", <i><a href=""http://www.opus-codec.org"">http://www.opus-codec.org</a></i></p>"
"<p>" + tr ( "Audio reverberation code by Perry R. Cook and Gary P. Scavone" ) +
", 1995 - 2004, <i><a href=""http://ccrma.stanford.edu/software/stk"">"
"The Synthesis ToolKit in C++ (STK)</a></i></p>"
"<p>" + tr ( "Some pixmaps are from the" ) + " Open Clip Art Library (OCAL), "
"<i><a href=""http://openclipart.org"">http://openclipart.org</a></i></p>"
"<p>" + tr ( "Country flag icons from Mark James" ) +
", <i><a href=""http://www.famfamfam.com"">http://www.famfamfam.com</a></i></p>" );
// contributors list
txvContributors->setText (
"<p>Peter L. Jones (<a href=""https://github.com/pljones"">pljones</a>)</p>"
"<p>Jonathan Baker-Bates (<a href=""https://github.com/gilgongo"">gilgongo</a>)</p>"
"<p>Daniele Masato (<a href=""https://github.com/doloopuntil"">doloopuntil</a>)</p>"
"<p>Simon Tomlinson (<a href=""https://github.com/sthenos"">sthenos</a>)</p>"
"<p>Marc jr. Landolt (<a href=""https://github.com/braindef"">braindef</a>)</p>"
"<p>Olivier Humbert (<a href=""https://github.com/trebmuh"">trebmuh</a>)</p>"
"<p>Tarmo Johannes (<a href=""https://github.com/tarmoj"">tarmoj</a>)</p>"
"<p>mirabilos (<a href=""https://github.com/mirabilos"">mirabilos</a>)</p>"
"<p>newlaurent62 (<a href=""https://github.com/newlaurent62"">newlaurent62</a>)</p>"
"<p>Emlyn Bolton (<a href=""https://github.com/emlynmac"">emlynmac</a>)</p>"
"<p>Jos van den Oever (<a href=""https://github.com/vandenoever"">vandenoever</a>)</p>"
"<p>Tormod Volden (<a href=""https://github.com/tormodvolden"">tormodvolden</a>)</p>"
"<p>Stanislas Michalak (<a href=""https://github.com/stanislas-m"">stanislas-m</a>)</p>"
"<p>JP Cimalando (<a href=""https://github.com/jpcima"">jpcima</a>)</p>"
"<br>" + tr ( "For details on the contributions check out the " ) +
"<a href=""https://github.com/corrados/jamulus/graphs/contributors"">" + tr ( "Github Contributors list" ) + "</a>." );
// translators
txvTranslation->setText (
"<p><b>" + tr ( "Spanish" ) + "</b></p>"
"<p>Daryl Hanlon (<a href=""https://github.com/ignotus666"">ignotus666</a>)</p>"
"<p><b>" + tr ( "French" ) + "</b></p>"
"<p>Olivier Humbert (<a href=""https://github.com/trebmuh"">trebmuh</a>)</p>"
"<p><b>" + tr ( "Portuguese" ) + "</b></p>"
"<p>Miguel de Matos (<a href=""https://github.com/Snayler"">Snayler</a>)</p>"
"<p><b>" + tr ( "Dutch" ) + "</b></p>"
"<p>Jeroen Geertzen (<a href=""https://github.com/jerogee"">jerogee</a>)</p>"
"<p><b>" + tr ( "Italian" ) + "</b></p>"
"<p>Giuseppe Sapienza (<a href=""https://github.com/dzpex"">dzpex</a>)</p>"
"<p><b>" + tr ( "German" ) + "</b></p>"
"<p>Volker Fischer (<a href=""https://github.com/corrados"">corrados</a>)</p>" );
// set version number in about dialog
lblVersion->setText ( GetVersionAndNameStr() );
// set window title
setWindowTitle ( tr ( "About " ) + APP_NAME );
}
QString CAboutDlg::GetVersionAndNameStr ( const bool bWithHtml )
{
QString strVersionText = "";
// name, short description and GPL hint
if ( bWithHtml )
{
strVersionText += "<b>";
}
else
{
strVersionText += " *** ";
}
strVersionText += APP_NAME + tr ( ", Version " ) + VERSION;
if ( bWithHtml )
{
strVersionText += "</b><br>";
}
else
{
strVersionText += "\n *** ";
}
if ( !bWithHtml )
{
strVersionText += tr ( "Internet Jam Session Software" );
strVersionText += "\n *** ";
}
strVersionText += tr ( "Under the GNU General Public License (GPL)" );
return strVersionText;
}
// Licence dialog --------------------------------------------------------------
CLicenceDlg::CLicenceDlg ( QWidget* parent ) : QDialog ( parent )
{
/*
The licence dialog is structured as follows:
- text box with the licence text on the top
- check box: I &agree to the above licence terms
- Accept button (disabled if check box not checked)
- Decline button
*/
setWindowIcon ( QIcon ( QString::fromUtf8 ( ":/png/main/res/fronticon.png" ) ) );
resize ( 700, 450 );
QVBoxLayout* pLayout = new QVBoxLayout ( this );
QHBoxLayout* pSubLayout = new QHBoxLayout;
QTextBrowser* txvLicence = new QTextBrowser ( this );
QCheckBox* chbAgree = new QCheckBox ( tr ( "I &agree to the above licence terms" ), this );
butAccept = new QPushButton ( tr ( "Accept" ), this );
QPushButton* butDecline = new QPushButton ( tr ( "Decline" ), this );
pSubLayout->addStretch();
pSubLayout->addWidget ( chbAgree );
pSubLayout->addWidget ( butAccept );
pSubLayout->addWidget ( butDecline );
pLayout->addWidget ( txvLicence );
pLayout->addLayout ( pSubLayout );
// set some properties
butAccept->setEnabled ( false );
butAccept->setDefault ( true );
txvLicence->setOpenExternalLinks ( true );
// define the licence text (similar to what we have in Ninjam)
txvLicence->setText (
"<p><big>" + tr (
"By connecting to this server and agreeing to this notice, you agree to the "
"following:" ) + "</big></p><p><big>" + tr (
"You agree that all data, sounds, or other works transmitted to this server "
"are owned and created by you or your licensors, and that you are making these "
"data, sounds or other works available via the following Creative Commons "
"License (for more information on this license, see " ) +
"<i><a href=""http://creativecommons.org/licenses/by-nc-sa/4.0"">"
"http://creativecommons.org/licenses/by-nc-sa/4.0</a></i>):</big></p>"
"<h3>Attribution-NonCommercial-ShareAlike 4.0</h3>"
"<p>" + tr ( "You are free to:" ) +
"<ul>"
"<li><b>" + tr ( "Share" ) + "</b> - " +
tr ( "copy and redistribute the material in any medium or format" ) + "</li>"
"<li><b>" + tr ( "Adapt" ) + "</b> - " +
tr ( "remix, transform, and build upon the material" ) + "</li>"
"</ul>" + tr ( "The licensor cannot revoke these freedoms as long as you follow the "
"license terms." ) + "</p>"
"<p>" + tr ( "Under the following terms:" ) +
"<ul>"
"<li><b>" + tr ( "Attribution" ) + "</b> - " +
tr ( "You must give appropriate credit, provide a link to the license, and indicate "
"if changes were made. You may do so in any reasonable manner, but not in any way "
"that suggests the licensor endorses you or your use." ) + "</li>"
"<li><b>" + tr ( "NonCommercial" ) + "</b> - " +
tr ( "You may not use the material for commercial purposes." ) + "</li>"
"<li><b>" + tr ( "ShareAlike" ) + "</b> - " +
tr ( "If you remix, transform, or build upon the material, you must distribute your "
"contributions under the same license as the original." ) + "</li>"
"</ul><b>" + tr ( "No additional restrictions" ) + "</b> — " +
tr ( "You may not apply legal terms or technological measures that legally restrict "
"others from doing anything the license permits." ) + "</p>" );
QObject::connect ( chbAgree, SIGNAL ( stateChanged ( int ) ),
this, SLOT ( OnAgreeStateChanged ( int ) ) );
QObject::connect ( butAccept, SIGNAL ( clicked() ),
this, SLOT ( accept() ) );
QObject::connect ( butDecline, SIGNAL ( clicked() ),
this, SLOT ( reject() ) );
}
// Musician profile dialog -----------------------------------------------------
CMusProfDlg::CMusProfDlg ( CClient* pNCliP,
QWidget* parent ) :
QDialog ( parent ),
pClient ( pNCliP )
{
/*
The musician profile dialog is structured as follows:
- label with edit box for alias/name
- label with combo box for instrument
- label with combo box for country flag
- label with edit box for city
- label with combo box for skill level
- OK button
*/
setWindowTitle ( tr ( "Musician Profile" ) );
setWindowIcon ( QIcon ( QString::fromUtf8 ( ":/png/main/res/fronticon.png" ) ) );
QVBoxLayout* pLayout = new QVBoxLayout ( this );
QHBoxLayout* pButSubLayout = new QHBoxLayout;
QLabel* plblAlias = new QLabel ( tr ( "Alias/Name" ), this );
pedtAlias = new QLineEdit ( this );
QLabel* plblInstrument = new QLabel ( tr ( "Instrument" ), this );
pcbxInstrument = new QComboBox ( this );
QLabel* plblCountry = new QLabel ( tr ( "Country" ), this );
pcbxCountry = new QComboBox ( this );
QLabel* plblCity = new QLabel ( tr ( "City" ), this );
pedtCity = new QLineEdit ( this );
QLabel* plblSkill = new QLabel ( tr ( "Skill" ), this );
pcbxSkill = new QComboBox ( this );
QPushButton* butClose = new QPushButton ( tr ( "&Close" ), this );
QGridLayout* pGridLayout = new QGridLayout;
plblAlias->setSizePolicy ( QSizePolicy::Minimum, QSizePolicy::Expanding );
pGridLayout->addWidget ( plblAlias, 0, 0 );
pGridLayout->addWidget ( pedtAlias, 0, 1 );
plblInstrument->setSizePolicy ( QSizePolicy::Minimum, QSizePolicy::Expanding );
pGridLayout->addWidget ( plblInstrument, 1, 0 );
pGridLayout->addWidget ( pcbxInstrument, 1, 1 );
plblCountry->setSizePolicy ( QSizePolicy::Minimum, QSizePolicy::Expanding );
pGridLayout->addWidget ( plblCountry, 2, 0 );
pGridLayout->addWidget ( pcbxCountry, 2, 1 );
plblCity->setSizePolicy ( QSizePolicy::Minimum, QSizePolicy::Expanding );
pGridLayout->addWidget ( plblCity, 3, 0 );
pGridLayout->addWidget ( pedtCity, 3, 1 );
plblSkill->setSizePolicy ( QSizePolicy::Minimum, QSizePolicy::Expanding );
pGridLayout->addWidget ( plblSkill, 4, 0 );
pGridLayout->addWidget ( pcbxSkill, 4, 1 );
pButSubLayout->addStretch();
pButSubLayout->addWidget ( butClose );
pLayout->addLayout ( pGridLayout );
pLayout->addLayout ( pButSubLayout );
// we do not want to close button to be a default one (the mouse pointer
// may jump to that button which we want to avoid)
butClose->setAutoDefault ( false );
butClose->setDefault ( false );
// Instrument pictures combo box -------------------------------------------
// add an entry for all known instruments
for ( int iCurInst = 0; iCurInst < CInstPictures::GetNumAvailableInst(); iCurInst++ )
{
// create a combo box item with text, image and background color
QColor InstrColor;
pcbxInstrument->addItem ( QIcon ( CInstPictures::GetResourceReference ( iCurInst ) ),
CInstPictures::GetName ( iCurInst ),
iCurInst );
switch ( CInstPictures::GetCategory ( iCurInst ) )
{
case CInstPictures::IC_OTHER_INSTRUMENT: InstrColor = QColor ( Qt::blue ); break;
case CInstPictures::IC_WIND_INSTRUMENT: InstrColor = QColor ( Qt::green ); break;
case CInstPictures::IC_STRING_INSTRUMENT: InstrColor = QColor ( Qt::red ); break;
case CInstPictures::IC_PLUCKING_INSTRUMENT: InstrColor = QColor ( Qt::cyan ); break;
case CInstPictures::IC_PERCUSSION_INSTRUMENT: InstrColor = QColor ( Qt::white ); break;
case CInstPictures::IC_KEYBOARD_INSTRUMENT: InstrColor = QColor ( Qt::yellow ); break;
case CInstPictures::IC_MULTIPLE_INSTRUMENT: InstrColor = QColor ( Qt::black ); break;
}
InstrColor.setAlpha ( 10 );
pcbxInstrument->setItemData ( iCurInst, InstrColor, Qt::BackgroundRole );
}
// sort the items in alphabetical order
pcbxInstrument->model()->sort ( 0 );
// Country flag icons combo box --------------------------------------------
// add an entry for all known country flags
for ( int iCurCntry = static_cast<int> ( QLocale::AnyCountry );
iCurCntry < static_cast<int> ( QLocale::LastCountry ); iCurCntry++ )
{
// exclude the "None" entry since it is added after the sorting
if ( static_cast<QLocale::Country> ( iCurCntry ) != QLocale::AnyCountry )
{
// get current country enum
QLocale::Country eCountry = static_cast<QLocale::Country> ( iCurCntry );
// try to load icon from resource file name
QIcon CurFlagIcon;
CurFlagIcon.addFile ( CLocale::GetCountryFlagIconsResourceReference ( eCountry ) );
// only add the entry if a flag is available
if ( !CurFlagIcon.isNull() )
{
// create a combo box item with text and image
pcbxCountry->addItem ( QIcon ( CurFlagIcon ),
QLocale::countryToString ( eCountry ),
iCurCntry );
}
}
}
// sort country combo box items in alphabetical order
pcbxCountry->model()->sort ( 0, Qt::AscendingOrder );
// the "None" country gets a special icon and is the very first item
QIcon FlagNoneIcon;
FlagNoneIcon.addFile ( ":/png/flags/res/flags/flagnone.png" );
pcbxCountry->insertItem ( 0,
FlagNoneIcon,
tr ( "None" ),
static_cast<int> ( QLocale::AnyCountry ) );
// Skill level combo box ---------------------------------------------------
// create a pixmap showing the skill level colors
QPixmap SLPixmap ( 16, 11 ); // same size as the country flags
SLPixmap.fill ( QColor::fromRgb ( RGBCOL_R_SL_NOT_SET,
RGBCOL_G_SL_NOT_SET,
RGBCOL_B_SL_NOT_SET ) );
pcbxSkill->addItem ( QIcon ( SLPixmap ), tr ( "None" ), SL_NOT_SET );
SLPixmap.fill ( QColor::fromRgb ( RGBCOL_R_SL_BEGINNER,
RGBCOL_G_SL_BEGINNER,
RGBCOL_B_SL_BEGINNER ) );
pcbxSkill->addItem ( QIcon ( SLPixmap ), tr ( "Beginner" ), SL_BEGINNER );
SLPixmap.fill ( QColor::fromRgb ( RGBCOL_R_SL_INTERMEDIATE,
RGBCOL_G_SL_INTERMEDIATE,
RGBCOL_B_SL_INTERMEDIATE ) );
pcbxSkill->addItem ( QIcon ( SLPixmap ), tr ( "Intermediate" ), SL_INTERMEDIATE );
SLPixmap.fill ( QColor::fromRgb ( RGBCOL_R_SL_SL_PROFESSIONAL,
RGBCOL_G_SL_SL_PROFESSIONAL,
RGBCOL_B_SL_SL_PROFESSIONAL ) );
pcbxSkill->addItem ( QIcon ( SLPixmap ), tr ( "Expert" ), SL_PROFESSIONAL );
// Add help text to controls -----------------------------------------------
// fader tag
QString strFaderTag = "<b>" + tr ( "Musician Profile" ) + ":</b> " + tr (
"Set your name or an alias here so that the other musicians you want to play with "
"know who you are. Additionally you may set an instrument picture of "
"the instrument you play and a flag of the country you are living in. "
"The city you live in and the skill level playing your instrument "
"may also be added." ) + "<br>" + tr (
"What you set here will appear at your fader on the mixer board when "
"you are connected to a " ) + APP_NAME + tr ( " server. This tag will "
"also show up at each client which is connected to the same server as "
"you. If the name is left empty, the IP address is shown instead." );
pedtAlias->setWhatsThis ( strFaderTag );
pedtAlias->setAccessibleName ( tr ( "Alias or name edit box" ) );
pcbxInstrument->setWhatsThis ( strFaderTag );
pcbxInstrument->setAccessibleName ( tr ( "Instrument picture button" ) );
pcbxCountry->setWhatsThis ( strFaderTag );
pcbxCountry->setAccessibleName ( tr ( "Country flag button" ) );
pedtCity->setWhatsThis ( strFaderTag );
pedtCity->setAccessibleName ( tr ( "City edit box" ) );
pcbxSkill->setWhatsThis ( strFaderTag );
pcbxSkill->setAccessibleName ( tr ( "Skill level combo box" ) );
// Connections -------------------------------------------------------------
QObject::connect ( pedtAlias, SIGNAL ( textChanged ( const QString& ) ),
this, SLOT ( OnAliasTextChanged ( const QString& ) ) );
QObject::connect ( pcbxInstrument, SIGNAL ( activated ( int ) ),
this, SLOT ( OnInstrumentActivated ( int ) ) );
QObject::connect ( pcbxCountry, SIGNAL ( activated ( int ) ),
this, SLOT ( OnCountryActivated ( int ) ) );
QObject::connect ( pedtCity, SIGNAL ( textChanged ( const QString& ) ),
this, SLOT ( OnCityTextChanged ( const QString& ) ) );
QObject::connect ( pcbxSkill, SIGNAL ( activated ( int ) ),
this, SLOT ( OnSkillActivated ( int ) ) );
QObject::connect ( butClose, SIGNAL ( clicked() ),
this, SLOT ( accept() ) );
}
void CMusProfDlg::showEvent ( QShowEvent* )
{
// Update the controls with the current client settings --------------------
// set the name
pedtAlias->setText ( pClient->ChannelInfo.strName );
// select current instrument
pcbxInstrument->setCurrentIndex (
pcbxInstrument->findData ( pClient->ChannelInfo.iInstrument ) );
// select current country
pcbxCountry->setCurrentIndex (
pcbxCountry->findData (
static_cast<int> ( pClient->ChannelInfo.eCountry ) ) );
// set the city
pedtCity->setText ( pClient->ChannelInfo.strCity );
// select the skill level
pcbxSkill->setCurrentIndex (
pcbxSkill->findData (
static_cast<int> ( pClient->ChannelInfo.eSkillLevel ) ) );
}
void CMusProfDlg::OnAliasTextChanged ( const QString& strNewName )
{
// check length
if ( strNewName.length() <= MAX_LEN_FADER_TAG )
{
// refresh internal name parameter
pClient->ChannelInfo.strName = strNewName;
// update channel info at the server
pClient->SetRemoteInfo();
}
else
{
// text is too long, update control with shortend text
pedtAlias->setText ( strNewName.left ( MAX_LEN_FADER_TAG ) );
}
}
void CMusProfDlg::OnInstrumentActivated ( int iCntryListItem )
{
// set the new value in the data base
pClient->ChannelInfo.iInstrument =
pcbxInstrument->itemData ( iCntryListItem ).toInt();
// update channel info at the server
pClient->SetRemoteInfo();
}
void CMusProfDlg::OnCountryActivated ( int iCntryListItem )
{
// set the new value in the data base
pClient->ChannelInfo.eCountry = static_cast<QLocale::Country> (
pcbxCountry->itemData ( iCntryListItem ).toInt() );
// update channel info at the server
pClient->SetRemoteInfo();
}
void CMusProfDlg::OnCityTextChanged ( const QString& strNewCity )
{
// check length
if ( strNewCity.length() <= MAX_LEN_SERVER_CITY )
{
// refresh internal name parameter
pClient->ChannelInfo.strCity = strNewCity;
// update channel info at the server
pClient->SetRemoteInfo();
}
else
{
// text is too long, update control with shortend text
pedtCity->setText ( strNewCity.left ( MAX_LEN_SERVER_CITY ) );
}
}
void CMusProfDlg::OnSkillActivated ( int iCntryListItem )
{
// set the new value in the data base
pClient->ChannelInfo.eSkillLevel = static_cast<ESkillLevel> (
pcbxSkill->itemData ( iCntryListItem ).toInt() );
// update channel info at the server
pClient->SetRemoteInfo();
}
// Help menu -------------------------------------------------------------------
CHelpMenu::CHelpMenu ( const bool bIsClient, QWidget* parent ) : QMenu ( tr ( "&Help" ), parent )
{
// standard help menu consists of about and what's this help
if ( bIsClient )
{
addAction ( tr ( "Getting &Started..." ), this, SLOT ( OnHelpClientGetStarted() ) );
addAction ( tr ( "Software &Manual..." ), this, SLOT ( OnHelpSoftwareMan() ) );
}
else
{
addAction ( tr ( "Getting &Started..." ), this, SLOT ( OnHelpServerGetStarted() ) );
}
addSeparator();
addAction ( tr ( "What's &This" ), this, SLOT ( OnHelpWhatsThis() ), QKeySequence ( Qt::SHIFT + Qt::Key_F1 ) );
addSeparator();
addAction ( tr ( "&About..." ), this, SLOT ( OnHelpAbout() ) );
}
/******************************************************************************\
* Other Classes *
\******************************************************************************/
// Network utility functions ---------------------------------------------------
bool NetworkUtil::ParseNetworkAddress ( QString strAddress,
CHostAddress& HostAddress )
{
QHostAddress InetAddr;
quint16 iNetPort = DEFAULT_PORT_NUMBER;
// init requested host address with invalid address first
HostAddress = CHostAddress();
// parse input address for the type "IP4 address:port number" or
// "[IP6 address]:port number" assuming the syntax is correctly given
QStringList slAddress = strAddress.split ( ":" );
QString strSep = ":";
bool bIsIP6 = false;
if ( slAddress.count() > 2 )
{
// IP6 address
bIsIP6 = true;
strSep = "]:";
}
QString strPort = strAddress.section ( strSep, 1, 1 );
if ( !strPort.isEmpty() )
{
// a colon is present in the address string, try to extract port number
iNetPort = strPort.toInt();
// extract address port before separator (should be actual internet address)
strAddress = strAddress.section ( strSep, 0, 0 );
if ( bIsIP6 )
{
// remove "[" at the beginning
strAddress.remove ( 0, 1 );
}
}
// first try if this is an IP number an can directly applied to QHostAddress
if ( !InetAddr.setAddress ( strAddress ) )
{
// it was no vaild IP address, try to get host by name, assuming
// that the string contains a valid host name string
const QHostInfo HostInfo = QHostInfo::fromName ( strAddress );
if ( HostInfo.error() == QHostInfo::NoError )
{
// apply IP address to QT object
if ( !HostInfo.addresses().isEmpty() )
{
// use the first IP address
InetAddr = HostInfo.addresses().first();
}
}
else
{
return false; // invalid address
}
}
HostAddress = CHostAddress ( InetAddr, iNetPort );
return true;
}
CHostAddress NetworkUtil::GetLocalAddress()
{
QTcpSocket socket;
socket.connectToHost ( WELL_KNOWN_HOST, WELL_KNOWN_PORT );
if ( socket.waitForConnected ( IP_LOOKUP_TIMEOUT ) )
{
return CHostAddress ( socket.localAddress(), 0 );
}
else
{
qWarning() << "could not determine local IPv4 address:"
<< socket.errorString()
<< "- using localhost";
return CHostAddress( QHostAddress::LocalHost, 0 );
}
}
QString NetworkUtil::GetCentralServerAddress ( const ECSAddType eCentralServerAddressType,
const QString& strCentralServerAddress )
{
switch ( eCentralServerAddressType )
{
case AT_CUSTOM: return strCentralServerAddress;
case AT_ALL_GENRES: return CENTSERV_ALL_GENRES;
case AT_GENRE_ROCK: return CENTSERV_GENRE_ROCK;
case AT_GENRE_JAZZ: return CENTSERV_GENRE_JAZZ;
case AT_GENRE_CLASSICAL_FOLK: return CENTSERV_GENRE_CLASSICAL_FOLK;
default: return DEFAULT_SERVER_ADDRESS; // AT_DEFAULT
}
}
// Instrument picture data base ------------------------------------------------
CVector<CInstPictures::CInstPictProps>& CInstPictures::GetTable()
{
// make sure we generate the table only once
static bool TableIsInitialized = false;
static CVector<CInstPictProps> vecDataBase;
if ( !TableIsInitialized )
{
// instrument picture data base initialization
// NOTE: Do not change the order of any instrument in the future!
// NOTE: The very first entry is the "not used" element per definition.
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "None" ), ":/png/instr/res/instruments/none.png", IC_OTHER_INSTRUMENT ) ); // special first element
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Drum Set" ), ":/png/instr/res/instruments/drumset.png", IC_PERCUSSION_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Djembe" ), ":/png/instr/res/instruments/djembe.png", IC_PERCUSSION_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Electric Guitar" ), ":/png/instr/res/instruments/eguitar.png", IC_PLUCKING_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Acoustic Guitar" ), ":/png/instr/res/instruments/aguitar.png", IC_PLUCKING_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Bass Guitar" ), ":/png/instr/res/instruments/bassguitar.png", IC_PLUCKING_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Keyboard" ), ":/png/instr/res/instruments/keyboard.png", IC_KEYBOARD_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Synthesizer" ), ":/png/instr/res/instruments/synthesizer.png", IC_KEYBOARD_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Grand Piano" ), ":/png/instr/res/instruments/grandpiano.png", IC_KEYBOARD_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Accordion" ), ":/png/instr/res/instruments/accordeon.png", IC_KEYBOARD_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Vocal" ), ":/png/instr/res/instruments/vocal.png", IC_OTHER_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Microphone" ), ":/png/instr/res/instruments/microphone.png", IC_OTHER_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Harmonica" ), ":/png/instr/res/instruments/harmonica.png", IC_WIND_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Trumpet" ), ":/png/instr/res/instruments/trumpet.png", IC_WIND_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Trombone" ), ":/png/instr/res/instruments/trombone.png", IC_WIND_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "French Horn" ), ":/png/instr/res/instruments/frenchhorn.png", IC_WIND_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Tuba" ), ":/png/instr/res/instruments/tuba.png", IC_WIND_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Saxophone" ), ":/png/instr/res/instruments/saxophone.png", IC_WIND_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Clarinet" ), ":/png/instr/res/instruments/clarinet.png", IC_WIND_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Flute" ), ":/png/instr/res/instruments/flute.png", IC_WIND_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Violin" ), ":/png/instr/res/instruments/violin.png", IC_STRING_INSTRUMENT ) );
vecDataBase.Add ( CInstPictProps ( QCoreApplication::translate ( "CMusProfDlg", "Cello" ), ":/png/instr/res/instruments/cello.png", IC_STRING_INSTRUMENT ) );