-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTalkingChart.js
More file actions
2499 lines (1086 loc) · 103 KB
/
TalkingChart.js
File metadata and controls
2499 lines (1086 loc) · 103 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
/******************************************
** TalkingChart.js: ***
** get input data as array and ***
** play the audio based on the ***
** input array. ***
** Author: Di Ren, ***
** Haik Sahakian ***
******************************************/
/*version 1.1:
bug: it can not run more than 5 times
only work on chrome for now*/
/*version 1.2: it works on both firefox and chrome, and it can run more than five times
bug: 1. after around 20 times, it does not work
2. if you doulbe click the play button, it gose crazy*/
/*version 1.3: it works on safari now but still has those two bugs
bug: 1. after 22 times, it does not work
2. if you doulbe click the play button, it gose crazy*/
/*version 1.4: fix bug of if you doulbe click the play button, it gose crazy
bug: 1. after 22 times, it does not work
*/
/*version 1.5: make demo, make one tone and tones function
bug: 1. after 22 times, it does not work
*/
/*version 1.6: work with as many tones as you want.
fix bug: after 22 times, it does not work. Now it fixed but have no idea how that fixed
bug: TypeError: Value being assigned to AudioParam.value is not a finite floating-point value.
*/
/*version 2.0: put everything into class
bug: TypeError: Value being assigned to AudioParam.value is not a finite floating-point value.
*/
/*version 2.1: add function: pickUpDataSample, setminNoteDuration, setMaxPlayTimeInMilliseconds, set volume
solve: TypeError: Value being assigned to AudioParam.value is not a finite floating-point value. the problem is the setInterval run one more time than it should be.
*/
/*version 2.2: Change lib name to "Talking chart"
add Parameter as input, change the way to use it
add null into data, and change normalization function
TODO: deal with more than two tones, now one tone and two tones are hard coded
*/
/*version 2.3: dynamical create a div or some thing which contain this object and also the play button
TODO: deal with more than two tones, now one tone and two tones are hard coded
*/
/*version 2.4: 7/30/2014
add: debugModeOn: true, in each voice add :frequencyLow: 300, range: 300,
change reInit function to fix cannot play more than one time problem, change order of init function to fix problem with only one tone
TODO: deal with more than two tones, now one tone and two tones are hard coded
*/
/*note:
1. to make dynamical create a div work, user have to put this lib after the body tag, because it need id from a tag in the html file, so that tag has to define before this lib
*/
// Main Talking Chart class
function TalkingChart (parameters) {
// Parameters that can be set by the user
this.announcement =""; //announcement which is the text before the play button
this.containerId; //the container id that user gives, for dynamic create html and css code
this.debugMode= true; //debug mode: to show log in console if they got error, default should be false, but here set it to true for first errorLog
this.maxPlayTimeInMilliseconds = 5000; //total play time, in ms
this.minNoteDuration = 200; //play speed in ms
this.playButtonText = "Play Chart"; //text for the play button
this.playVoicesConcurrently = true; //play two voice together
this.separateVoicesInStereo = true; //play difference data in two ears
this.volume = 1; //volume for all,
this.condenseDataToFit = true; //when it is true then use pickUpDataSample function, default is true
this.parameters; // a copy of the input parameter object
this.voice1FrequencyLowDefault = 700; //Default value of voice 1 frequency low
this.frequencyRangeDefault = 300; //Default value of frequency range
this.voice2FrequencyLowDefault = 350; //Default value of voice 1 frequency low
// Private variables
this.isPlaying; //true: it is playing, false: it is not playing. to start and stop
this.audioNode =[]; //all the audio nodes
this.inputData =[]; //all the input data, two for now
this.inputNormalized =[]; //save normalized input
this.maxInputLength; //length of input data, will set the first input data's length
this.numberOfInput; //number of input data
this.timeIntervalNode; //for time interval, as global var
this.pulseRequested = false; // Set in init(). True when any voice has had pulsing turned on.
this.firstTimePlay; //is this the first time play or not, to make sure we need call reInit function or not
this.dynamicallyCreateHtml; //if containerId is defined, then it is on, if containerId is undefined, then keep this off;
//TODO: for pro version (kidding), they can set difference voice for difference
// Create an audio context, if the browser supports it
this.TalkingChartContext = null;
if (window.AudioContext) this.TalkingChartContext = new AudioContext();
if (window.webkitAudioContext) this.TalkingChartContext = new webkitAudioContext();
if (this.TalkingChartContext == null) {
this.errorLog("Browser version error, please use latest version of Chrome, Firefox, or Safari.", "error");
return;
}
// Call init to initialize the library if a parameters object was passed in to the constructor
if (parameters) {
this.init(parameters);
}
}
//put all data as input, they should be array
TalkingChart.prototype.init= function(parameters) {
this.parameters = parameters;
if (!this.parameters) {
this.errorLog("No parameters supplied.", "error");
}
this.firstTimePlay = true;
if(typeof this.parameters.debugMode == "undefined"){
this.setdebugMode(false);
}
else{
this.setdebugMode(this.parameters.debugMode);
}
//no input
if( typeof this.parameters.voice1 == "undefined" && typeof this.parameters.voice2 == "undefined"){
this.errorLog("input should have voice1","error");
}
//put voice2 only, use voice2 instead of voice 1
else if (typeof this.parameters.voice1 == "undefined" && typeof this.parameters.voice2 != "undefined"){
if(typeof this.parameters.voice2.data == "undefined"){
this.errorLog("Voice 2 is defined but input data is missing", "error");
}
else{
//number of data
this.numberOfInput = 1; //only one data input
this.audioNode[0] = new TalkingChartNode(); //create each TalkingChartNode
this.inputData[0] = this.parameters.voice2.data.slice(0);
//playVoicesConcurrently, since only one voice, it does not matter, true as default
//only one voice, so separateVoicesInStereo should be false
this.separateVoicesInStereo = false;
//set for voice 2
if(typeof this.parameters.voice2.playVoiceAsPulse !="undefined"){
this.audioNode[0].setPlayVoiceAsPulse(this.parameters.voice2.playVoiceAsPulse);
}
this.isPlaying = false; //init is not playing
//set volume
if (parameters.volume)
this.volume = parameters.volume;
this.setVolume(this.volume);
//minNoteDuration
if(typeof this.parameters.minNoteDuration != "undefined"){
this.setminNoteDuration(this.parameters.minNoteDuration);//minNoteDuration as 200 as default, blind people could set to 100-150, (just guess)
}
//set maxPlayTimeInMilliseconds
if (typeof this.parameters.maxPlayTimeInMilliseconds != "undefined"){
if(this.parameters.maxPlayTimeInMilliseconds > 0){
this.setMaxPlayTimeInMilliseconds(this.parameters.maxPlayTimeInMilliseconds); //5 sec as default for maxPlayTimeInMilliseconds if input is less than 0, then use default
}
}
//set containerId
if(typeof this.parameters.containerId == "undefined"){
this.containerId = null;
//no container, so do not dynamically create html
this.dynamicallyCreateHtml = false;
}
else{
this.containerId= this.parameters.containerId;
this.dynamicallyCreateHtml = true;
if(typeof this.parameters.playButtonText =="undefined"){
this.setPlayButtonText("play"); //set play as default
}
else{
this.setPlayButtonText(this.parameters.playButtonText);
}
}
var tempData;
tempData = this.inputData[0].slice(0);
//must clean first and then pick up data
//clean the data
this.cleanData(tempData);
//set condenseDataToFit
if (typeof this.parameters.condenseDataToFit != "undefined"){
this.setCondenseDataToFit(this.parameters.condenseDataToFit);
}
var temp = this.pickUpDataSample(tempData);
this.inputNormalized[0]= this.normaliztion(temp); //normalize input data
this.audioNode[0].TalkingChartContext = this.TalkingChartContext; //pass TalkingChartContext to talking chart node
this.audioNode[0].nodeInit(this.inputNormalized[0]); // TalkingChartNode initialize
//set frequency and range, voice 2
if(typeof this.parameters.voice2.frequencyLow == "undefined" ){
if(typeof this.parameters.voice2.range == "undefined"){
this.audioNode[0].setFrequencyRange(this.voice1FrequencyLowDefault,this.frequencyRangeDefault); //default value is this.voice1FrequencyLowDefault(700), range this.frequencyRangeDefault(300)
}
else{
this.audioNode[0].setFrequencyRange(this.voice1FrequencyLowDefault,this.parameters.voice2.range); //frequency low undefined, range defined
}
}
//lower frequency set
else{
if(typeof this.parameters.voice2.range == "undefined"){
this.audioNode[0].setFrequencyRange(this.parameters.voice2.frequencyLow,this.frequencyRangeDefault); //default range is this.frequencyRangeDefault(300)
}
else{
this.audioNode[0].setFrequencyRange(this.parameters.voice2.frequencyLow,this.parameters.voice2.range); //frequency low undefined, range defined
}
}
this.audioNode[0].setVolume(this.volume);
//set name of data
if (typeof this.parameters.voice2.announcement != "undefined"){
this.audioNode[0].setDataName(this.parameters.voice2.announcement);//"" for data name as default
}
//set pulseRequested as false first
this.setPulseRequested(false); //pulseRequested default value is false
for(var i=0; i< this.numberOfInput; i++){
if(this.audioNode[i].playVoiceAsPulse){
//TODO: pulse does not work well on Chrome and Safari, so stop pulse if user use chrome or safari
var browserVersion = window.navigator.userAgent;
var browserVersionFirefox = browserVersion.indexOf("Firefox");
if(browserVersionFirefox>0){
this.setPulseRequested(true);
}
else{
this.errorLog("for pulse feature, we only support for FireFox for now. Sorry for inconvenience.","info");
}
break;
}
}
//connect
this.oneToneSetup();
}
}
//put voice1 only
else if (typeof this.parameters.voice1 != "undefined" && typeof this.parameters.voice2 == "undefined"){
if(typeof this.parameters.voice1.data == "undefined"){
this.errorLog("input data is required","error");
}
else{
this.audioNode[0] = new TalkingChartNode(); //create each TalkingChartNode
//number of data
this.numberOfInput = 1; //only one data input
this.inputData[0] = this.parameters.voice1.data.slice(0);
//playVoicesConcurrently, since only one voice, it does not matter, true as default
//only one voice, so separateVoicesInStereo should be false
this.separateVoicesInStereo = false;
//set setPlayVoiceAsPulse(on)
if(typeof this.parameters.voice1.playVoiceAsPulse !="undefined"){
this.audioNode[0].setPlayVoiceAsPulse(this.parameters.voice1.playVoiceAsPulse);
}
this.isPlaying = false; //init is not playing
//set volume
if (parameters.volume)
this.volume = parameters.volume;
this.setVolume(this.volume);
//minNoteDuration
if(typeof this.parameters.minNoteDuration != "undefined"){
this.setminNoteDuration(this.parameters.minNoteDuration);//minNoteDuration as 200 as default, blind people could set to 100-150, (just guess)
}
//set maxPlayTimeInMilliseconds
if (typeof this.parameters.maxPlayTimeInMilliseconds != "undefined"){
if(this.parameters.maxPlayTimeInMilliseconds > 0){
this.setMaxPlayTimeInMilliseconds(this.parameters.maxPlayTimeInMilliseconds);//5 sec as default for maxPlayTimeInMilliseconds if input is less than 0, then use default
}
}
//set containerId
if(typeof this.parameters.containerId == "undefined"){
this.containerId = null;
//no container, so do not dynamically create html
this.dynamicallyCreateHtml = false;
}
else{
this.containerId= this.parameters.containerId;
this.dynamicallyCreateHtml = true;
if(typeof this.parameters.playButtonText == "undefined"){
this.setPlayButtonText("play"); //set play as default
}
else{
this.setPlayButtonText(this.parameters.playButtonText);
}
}
var tempData;
tempData = this.inputData[0].slice(0);
//must clean first and then pick up data
//clean the data
this.cleanData(tempData);
//set condenseDataToFit
if (typeof this.parameters.condenseDataToFit != "undefined"){
this.setCondenseDataToFit(this.parameters.condenseDataToFit);
}
var temp = this.pickUpDataSample(tempData);
this.inputNormalized[0]= this.normaliztion(temp); //normalize input data
this.audioNode[0].TalkingChartContext = this.TalkingChartContext; //pass TalkingChartContext to talking chart node
this.audioNode[0].nodeInit(this.inputNormalized[0]); // TalkingChartNode initialize
//set frequency and range, voice 1
if(typeof this.parameters.voice1.frequencyLow == "undefined" ){
if(typeof this.parameters.voice1.range == "undefined"){
this.audioNode[0].setFrequencyRange(this.voice1FrequencyLowDefault,this.frequencyRangeDefault); //default value is this.voice1FrequencyLowDefault(700), range this.frequencyRangeDefault(300)
}
else{
this.audioNode[0].setFrequencyRange(this.voice1FrequencyLowDefault,this.parameters.voice1.range); //frequency low undefined, range defined
}
}
//lower frequency set
else{
if(typeof this.parameters.voice1.range == "undefined"){
this.audioNode[0].setFrequencyRange(this.parameters.voice1.frequencyLow,this.frequencyRangeDefault); //default range is this.frequencyRangeDefault(300)
}
else{
this.audioNode[0].setFrequencyRange(this.parameters.voice1.frequencyLow,this.parameters.voice1.range); //frequency low undefined, range defined
}
}
this.audioNode[0].setVolume(this.volume);
//set name(announcement) of data
if (typeof this.parameters.voice1.announcement != "undefined"){
this.audioNode[0].setDataName(this.parameters.voice1.announcement); //"" for data announcement as default
}
//set pulseRequested as false first
this.setPulseRequested(false); //pulseRequested default value is false
for(var i=0; i< this.numberOfInput; i++){
if(this.audioNode[i].playVoiceAsPulse){
//TODO: pulse does not work well on Chrome and Safari, so stop pulse if user use chrome or safari
var browserVersion = window.navigator.userAgent;
var browserVersionFirefox = browserVersion.indexOf("Firefox");
if(browserVersionFirefox>0){
this.setPulseRequested(true);
}
else{
this.errorLog("for pulse feature, we only support for FireFox for now. Sorry for inconvenience.","info");
}
break;
}
}
//connect
this.oneToneSetup();
}
}
//have two input
else if (typeof this.parameters.voice1 != "undefined" && typeof this.parameters.voice2 != "undefined"){
if(typeof this.parameters.voice1.data == "undefined" || typeof this.parameters.voice2.data == "undefined"){
this.errorLog("input data is required","error");
}
else{
//minNoteDuration
if(typeof this.parameters.minNoteDuration != "undefined"){
this.setminNoteDuration(this.parameters.minNoteDuration);//minNoteDuration as 200 as default, blind people could set to 100-150, (just guess)
}
//playVoicesConcurrently
if(typeof this.parameters.playVoicesConcurrently != "undefined"){
this.playVoicesConcurrently = this.parameters.playVoicesConcurrently;//set playVoicesConcurrently to true as default
}
//separateVoicesInStereo play difference data in difference ears
if(typeof this.parameters.separateVoicesInStereo != "undefined"){
this.separateVoicesInStereo = this.parameters.separateVoicesInStereo;//set separateVoicesInStereo to true as default
}
this.inputData[0] = this.parameters.voice1.data.slice(0);
this.inputData[1] = this.parameters.voice2.data.slice(0);
this.maxInputLength = Math.max(this.inputData[0].length,this.inputData[1].length);
this.isPlaying = false; //init is not playing
//set volume
if (parameters.volume)
this.volume = parameters.volume;
this.setVolume(this.volume);
//number of data
this.numberOfInput = 2; //only one data input
//set maxPlayTimeInMilliseconds
if (typeof this.parameters.maxPlayTimeInMilliseconds != "undefined"){
if(this.parameters.maxPlayTimeInMilliseconds > 0){
this.setMaxPlayTimeInMilliseconds(this.parameters.maxPlayTimeInMilliseconds);//5 sec as default for maxPlayTimeInMilliseconds if input is less than 0, then use default
}
}
//set containerId
if(typeof this.parameters.containerId == "undefined"){
this.containerId = null;
//no container, so do not dynamically create html
this.dynamicallyCreateHtml = false;
}
else{
this.containerId= this.parameters.containerId;
this.dynamicallyCreateHtml = true;
if(typeof this.parameters.playButtonText == "undefined"){
this.setPlayButtonText("play"); //set play as default
}
else{
this.setPlayButtonText(this.parameters.playButtonText);
}
}
//set condenseDataToFit
if (typeof this.parameters.condenseDataToFit != "undefined"){
this.setCondenseDataToFit(this.parameters.condenseDataToFit);
}
var tempData;
for(i=0; i<this.numberOfInput; i++) {
tempData = this.inputData[i].slice(0);
//must clean first and then pick up data
//clean the data
this.cleanData(tempData);
var temp = this.pickUpDataSample(tempData);
this.inputNormalized[i]= this.normaliztion(temp);
this.audioNode[i] = new TalkingChartNode(); //create each TalkingChartNode
this.audioNode[i].TalkingChartContext = this.TalkingChartContext; //pass TalkingChartContext to talking chart node
this.audioNode[i].nodeInit(this.inputNormalized[i]); // TalkingChartNode initialize
this.audioNode[i].setVolume(this.volume);
}
//set setPlayVoiceAsPulse(on)
if(typeof this.parameters.voice1.playVoiceAsPulse !="undefined"){
this.audioNode[0].setPlayVoiceAsPulse(this.parameters.voice1.playVoiceAsPulse);
}
//set for voice 2
if(typeof this.parameters.voice2.playVoiceAsPulse !="undefined"){
this.audioNode[1].setPlayVoiceAsPulse(this.parameters.voice2.playVoiceAsPulse);
}
//set pulseRequested as false first
this.setPulseRequested(false); //pulseRequested default value is false
for(var i=0; i< this.numberOfInput; i++){
if(this.audioNode[i].playVoiceAsPulse){
//TODO: pulse does not work well on Chrome and Safari, so stop pulse if user use chrome or safari
var browserVersion = window.navigator.userAgent;
var browserVersionFirefox = browserVersion.indexOf("Firefox");
if(browserVersionFirefox>0){
this.setPulseRequested(true);
}
else{
this.errorLog("for pulse feature, we only support for FireFox for now. Sorry for inconvenience.","info");
}
break;
}
}
//set frequency and range, voice 1
if(typeof this.parameters.voice1.frequencyLow == "undefined" ){
if(typeof this.parameters.voice1.range == "undefined"){
this.audioNode[0].setFrequencyRange(this.voice1FrequencyLowDefault,this.frequencyRangeDefault); //default value is this.voice1FrequencyLowDefault(700), range this.frequencyRangeDefault(300)
}
else{
this.audioNode[0].setFrequencyRange(this.voice1FrequencyLowDefault,this.parameters.voice1.range); //frequency low undefined, range defined
}
}
//lower frequency set
else{
if(typeof this.parameters.voice1.range == "undefined"){
this.audioNode[0].setFrequencyRange(this.parameters.voice1.frequencyLow,this.frequencyRangeDefault); //default range is this.frequencyRangeDefault(300)
}
else{
this.audioNode[0].setFrequencyRange(this.parameters.voice1.frequencyLow,this.parameters.voice1.range); //frequency low undefined, range defined
}
}
//set frequency and range, voice 2
if(typeof this.parameters.voice2.frequencyLow == "undefined" ){
if(typeof this.parameters.voice2.range == "undefined"){
this.audioNode[1].setFrequencyRange(this.voice2FrequencyLowDefault,this.frequencyRangeDefault); //default value is 300, range 300
}
else{