WebAudioFont is a set of resources and associated technology that uses sample-based synthesis to play musical instruments in the browser. You can choose from thousands of instrument, see Catalog.
see LICENSE.md. Contact me if you need different license.
Hire me if you need advanced help (sss1024@gmail.com) - https://www.linkedin.com/in/sergeysurikov/
- simple example - code - open
- virtual drums - code - open
- virtual piano - code - open
- endless flute - code - open
- two voices - code - open
- sound fx - code - open
- realtime music - code - open
- dynamic loading - open
- mixer, equalizer and reverberation - open
- custom AHDSR envelope - open
- strum chord - open
- MIDI keyboard - open
- MIDI player - open
- Virtual instruments
- Interactive music generated on the fly
- Sound effects for non-music applications
Add a link to WebAudioFontPlayer.js and the instrument file. Invoke queueWaveTable.
Minimal HTML page
<html>
<head>
<script src='https://surikov.github.io/webaudiofont/npm/dist/WebAudioFontPlayer.js'></script>
<script src='https://surikov.github.io/webaudiofontdata/sound/0250_SoundBlasterOld_sf2.js'></script>
<script>
var AudioContextFunc = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContextFunc();
var player=new WebAudioFontPlayer();
player.loader.decodeAfterLoading(audioContext, '_tone_0250_SoundBlasterOld_sf2');
function play(){
player.queueWaveTable(audioContext, audioContext.destination
, _tone_0250_SoundBlasterOld_sf2, 0, 12*4+7, 2);
return false;
}
</script>
</head>
<body>
<p><a href='javascript:play();'>Hello, world!</a></p>
</body>
</html>
See live example.
Use queueWaveTable to play single note. Use queueChord, queueSnap, queueStrumDown, queueStrumUp to play chords. See live example for strumming.
Use time for notes to define sequence. See live example of sequencer.
Use the startLoad and waitLoad functions.
var instr=null;
var AudioContextFunc = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContextFunc();
var player=new WebAudioFontPlayer();
function changeInstrument(path,name){
player.loader.startLoad(audioContext, path, name);
player.loader.waitLoad(function () {
instr=window[name];
});
}
changeInstrument('https://surikov.github.io/webaudiofontdata/sound/0290_Aspirin_sf2_file.js','_tone_0290_Aspirin_sf2_file');
All loaded instruments are cached in memory.
See live example
Use WebAudioFontChannel to create a chain of 10-band equalizers. Use WebAudioFontReverberator to add echo.
See live example
See tutorial
WebAudioFont doesn't require installation. It hosts all code and instruments at GitHub Pages.
var webaudiofont = require('webaudiofont');
var player = new WebAudioFontPlayer();
...
"devDependencies": {
"webaudiofont":"^2.5.0"
...
WebAudioFont uses Web Audio API to play instruments. Synthesizer uses wavetables to play sound. Instruments came from free soundfonts:
- open index page
- find an instrument
- copy the name of the file to include the instrument data
- copy the name of the variable to refer to the instrument
- add this info to a page
Use drumInfo and instrumentInfo in realtime. See live example.
WebAudioFontPlayer has the function queueWaveTable(audioContext, target, preset, when, pitch, duration, volume, slides)
Parameters:
- audioContext - AudioContext
- target - a node to connect to, for example audioContext.destination
- preset - variable with the instrument preset
- when - when to play, audioContext.currentTime or 0 to play now, audioContext.currentTime + 3 to play after 3 seconds
- pitch - note pitch from 0 to 127, for example 2+12*4 to play D of fourth octave (use MIDI key for drums)
- duration - note duration in seconds, for example 4 to play 4 seconds
- volume - 0.0 <=1.0 volume (0 is 'no value', 'no value' is 1)
- slides - array of pitch bends
The function queueWaveTable returns an envelope object. You can use this object to cancel a sound or to access AudioBufferSourceNode.
Use queueChord, queueStrumUp, queueStrumDown, queueSnap for chords. See live example.
Feel free to ask for any kind of help at project issues.
See TypeScript implementation.
The catalog consists of a full set of MIDI standards. Each instrument has 5-10 sound variations from different soundfonts.
- Piano
- Acoustic Grand Piano: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
- Bright Acoustic Piano: 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21
- Electric Grand Piano: 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31
- Honky-tonk Piano: 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42
- Electric Piano 1: 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57
- Electric Piano 2: 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69
- Harpsichord: 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80
- Clavinet: 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88
- Chromatic Percussion
- Celesta: 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98
- Glockenspiel: 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106
- Music Box: 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115
- Vibraphone: 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123
- Marimba: 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132
- Xylophone: 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140
- Tubular Bells: 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151
- Dulcimer: 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159
- Organ
- Drawbar Organ: 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169
- Percussive Organ: 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179
- Rock Organ: 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189
- Church Organ: 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199
- Reed Organ: 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210
- Accordion: 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222
- Harmonica: 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230
- Tango Accordion: 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243
- Guitar
- Acoustic Guitar (nylon): 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255
- Acoustic Guitar (steel): 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273
- Electric Guitar (jazz): 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285
- Electric Guitar (clean): 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298
- Electric Guitar (muted): 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314
- Overdriven Guitar: 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332
- Distortion Guitar: 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353
- Guitar Harmonics: 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365
- Bass
- Acoustic Bass: 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374
- Electric Bass (finger): 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383
- Electric Bass (pick): 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392
- Fretless Bass: 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400
- Slap Bass 1: 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408
- Slap Bass 2: 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417
- Synth Bass 1: 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433
- Synth Bass 2: 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446
- Strings
- Violin: 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457
- Viola: 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465
- Cello: 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474
- Contrabass: 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482
- Tremolo Strings: 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491
- Pizzicato Strings: 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499
- Orchestral Harp: 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507
- Timpani: 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516
- Ensemble
- String Ensemble 1: 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
- String Ensemble 2: 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552
- Synth Strings 1: 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566
- Synth Strings 2: 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575
- Choir Aahs: 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587
- Voice Oohs: 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599
- Synth Choir: 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607
- Orchestra Hit: 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616
- Brass
- Trumpet: 617 | 618 | 619 | 620 | 621 | 622 | 623
- Trombone: 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631
- Tuba: 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639
- Muted Trumpet: 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647
- French Horn: 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658
- Brass Section: 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670
- Synth Brass 1: 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682
- Synth Brass 2: 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694
- Reed
- Soprano Sax: 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702
- Alto Sax: 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711
- Tenor Sax: 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720
- Baritone Sax: 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728
- Oboe: 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736
- English Horn: 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744
- Bassoon: 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753
- Clarinet: 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761
- Pipe
- Piccolo: 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770
- Flute: 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780
- Recorder: 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788
- Pan Flute: 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799
- Blown bottle: 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810
- Shakuhachi: 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820
- Whistle: 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828
- Ocarina: 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836
- Synth Lead
- Lead 1 (square): 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845
- Lead 2 (sawtooth): 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855
- Lead 3 (calliope): 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867
- Lead 4 (chiff): 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877
- Lead 5 (charang): 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891
- Lead 6 (voice): 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902
- Lead 7 (fifths): 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912
- Lead 8 (bass + lead): 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922
- Synth Pad
- Pad 1 (new age): 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943
- Pad 2 (warm): 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953
- Pad 3 (polysynth): 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964
- Pad 4 (choir): 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975
- Pad 5 (bowed): 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985
- Pad 6 (metallic): 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996
- Pad 7 (halo): 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007
- Pad 8 (sweep): 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016
- Synth Effects
- FX 1 (rain): 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028
- FX 2 (soundtrack): 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038
- FX 3 (crystal): 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1050 | 1051 | 1052
- FX 4 (atmosphere): 1053 | 1054 | 1055 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068
- FX 5 (brightness): 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 | 1078 | 1079 | 1080 | 1081 | 1082 | 1083
- FX 6 (goblins): 1084 | 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1093 | 1094
- FX 7 (echoes): 1095 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107
- FX 8 (sci-fi): 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 1118 | 1119
- Ethnic
- Sitar: 1120 | 1121 | 1122 | 1123 | 1124 | 1125 | 1126 | 1127 | 1128
- Banjo: 1129 | 1130 | 1131 | 1132 | 1133 | 1134 | 1135 | 1136
- Shamisen: 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146
- Koto: 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | 1155 | 1156 | 1157
- Kalimba: 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165
- Bagpipe: 1166 | 1167 | 1168 | 1169 | 1170 | 1171 | 1172 | 1173
- Fiddle: 1174 | 1175 | 1176 | 1177 | 1178 | 1179 | 1180 | 1181 | 1182 | 1183 | 1184
- Shanai: 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191
- Percussive
- Tinkle Bell: 1192 | 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | 1199
- Agogo: 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1207 | 1208
- Steel Drums: 1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216
- Woodblock: 1217 | 1218 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227
- Taiko Drum: 1228 | 1229 | 1230 | 1231 | 1232 | 1233 | 1234 | 1235 | 1236 | 1237 | 1238 | 1239 | 1240
- Melodic Tom: 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | 1248 | 1249 | 1250 | 1251
- Synth Drum: 1252 | 1253 | 1254 | 1255 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261
- Reverse Cymbal: 1262 | 1263 | 1264 | 1265 | 1266 | 1267 | 1268 | 1269 | 1270 | 1271 | 1272
- Sound effects
- Guitar Fret Noise: 1273 | 1274 | 1275 | 1276 | 1277 | 1278 | 1279 | 1280 | 1281 | 1282
- Breath Noise: 1283 | 1284 | 1285 | 1286 | 1287 | 1288 | 1289 | 1290 | 1291 | 1292
- Seashore: 1293 | 1294 | 1295 | 1296 | 1297 | 1298 | 1299 | 1300 | 1301 | 1302 | 1303 | 1304 | 1305 | 1306 | 1307 | 1308 | 1309 | 1310
- Bird Tweet: 1311 | 1312 | 1313 | 1314 | 1315 | 1316 | 1317 | 1318 | 1319 | 1320 | 1321 | 1322 | 1323
- Telephone Ring: 1324 | 1325 | 1326 | 1327 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 | 1334 | 1335 | 1336 | 1337 | 1338
- Helicopter: 1339 | 1340 | 1341 | 1342 | 1343 | 1344 | 1345 | 1346 | 1347 | 1348 | 1349 | 1350 | 1351 | 1352 | 1353 | 1354 | 1355 | 1356 | 1357 | 1358 | 1359 | 1360 | 1361 | 1362 | 1363 | 1364
- Applause: 1365 | 1366 | 1367 | 1368 | 1369 | 1370 | 1371 | 1372 | 1373 | 1374 | 1375 | 1376 | 1377 | 1378 | 1379 | 1380 | 1381
- Gunshot: 1382 | 1383 | 1384 | 1385 | 1386 | 1387 | 1388 | 1389 | 1390 | 1391 | 1392 | 1393 | 1394
- Percussion
- Bass Drum 2: see Drums part at Catalog
- Bass Drum 1: see Drums part at Catalog
- Side Stick/Rimshot: see Drums part at Catalog
- Snare Drum 1: see Drums part at Catalog
- Hand Clap: see Drums part at Catalog
- Snare Drum 2: see Drums part at Catalog
- Low Tom 2: see Drums part at Catalog
- Closed Hi-hat: see Drums part at Catalog
- Low Tom 1: see Drums part at Catalog
- Pedal Hi-hat: see Drums part at Catalog
- Mid Tom 2: see Drums part at Catalog
- Open Hi-hat: see Drums part at Catalog
- Mid Tom 1: see Drums part at Catalog
- High Tom 2: see Drums part at Catalog
- Crash Cymbal 1: see Drums part at Catalog
- High Tom 1: see Drums part at Catalog
- Ride Cymbal 1: see Drums part at Catalog
- Chinese Cymbal: see Drums part at Catalog
- Ride Bell: see Drums part at Catalog
- Tambourine: see Drums part at Catalog
- Splash Cymbal: see Drums part at Catalog
- Cowbell: see Drums part at Catalog
- Crash Cymbal 2: see Drums part at Catalog
- Vibra Slap: see Drums part at Catalog
- Ride Cymbal 2: see Drums part at Catalog
- High Bongo: see Drums part at Catalog
- Low Bongo: see Drums part at Catalog
- Mute High Conga: see Drums part at Catalog
- Open High Conga: see Drums part at Catalog
- Low Conga: see Drums part at Catalog
- High Timbale: see Drums part at Catalog
- Low Timbale: see Drums part at Catalog
- High Agogo: see Drums part at Catalog
- Low Agogo: see Drums part at Catalog
- Cabasa: see Drums part at Catalog
- Maracas: see Drums part at Catalog
- Short Whistle: see Drums part at Catalog
- Long Whistle: see Drums part at Catalog
- Short Guiro: see Drums part at Catalog
- Long Guiro: see Drums part at Catalog
- Claves: see Drums part at Catalog
- High Wood Block: see Drums part at Catalog
- Low Wood Block: see Drums part at Catalog
- Mute Cuica: see Drums part at Catalog
- Open Cuica: see Drums part at Catalog
- Mute Triangle: see Drums part at Catalog
- Open Triangle: see Drums part at Catalog