Skip to content

Commit a57339b

Browse files
martin-henzwltan
authored andcommitted
Sound doc (source-academy#927)
* examples improved * documentation of sound library updated
1 parent 14e33ca commit a57339b

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed

public/externalLibs/sound/README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
To describe a Sound, you need a wave function, and the duration of the Sound in seconds.
2-
The wave function takes a time *t* (in seconds) as argument and returns the amplitude of the wave
3-
(a number between -1 and 1) at time *t*. An example wave function `my_wave` has this type:
2+
The wave function takes a non-negative time *t* (in seconds) as argument and returns the amplitude
3+
of the wave
4+
(a number between -1 and 1) at time *t*. In this library, we assume that as duration of a sound
5+
a non-negative number is given. An example wave function `my_wave` has this type:
46

57
`my_wave` : Number → Number
68

@@ -45,13 +47,14 @@ play(noise_sound(0.5));
4547

4648
after which you should hear half a second of noise. (If you don't, your browser does not support sound; use a different one or ask your Avenger for advice).
4749

48-
### Sound Discipline
50+
### Sound Property
4951

5052
The `make_sound` constructor ensures that all Sounds have the following property:
5153
```
5254
(get_wave(sound))(get_duration(sound) + t) === 0
5355
```
54-
for any number `t` > 0, regardless what the original wave of the Sound returns for `t`.
56+
for any number `t` 0, regardless what the original wave of the Sound returns for `t`.
5557
The wave will simply return 0 when the duration is up.
56-
This Sound discipline removes the need to change the wave function when
58+
This Sound property removes the need to explicitly change the wave function when
5759
the duration of a Sound changes.
60+

public/externalLibs/sound/soundToneMatrix.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,10 @@ function linear_decay(decay_period) {
495495
* Release ratios are given in the first, second and fourth
496496
* arguments, and the Sustain level is given in
497497
* the third argument as a fraction between 0 and 1.
498-
* @param {number} attack_ratio - proportion of sound in attack phase
499-
* @param {number} decay_ratio - proportion of sound decay phase
500-
* @param {number} sustain_level - sustain level between 0 and 1
501-
* @param {number} release_ratio - proportion of sound release phase
498+
* @param {Number} attack_ratio - proportion of sound in attack phase
499+
* @param {Number} decay_ratio - proportion of sound decay phase
500+
* @param {Number} sustain_level - sustain level between 0 and 1
501+
* @param {Number} release_ratio - proportion of sound release phase
502502
* @returns {function} envelope: function from sound to sound
503503
*/
504504
function adsr(attack_ratio, decay_ratio, sustain_level, release_ratio) {
@@ -534,8 +534,8 @@ function adsr(attack_ratio, decay_ratio, sustain_level, release_ratio) {
534534
* the second has twice the frequency, the third three times the
535535
* frequency etc.
536536
* @param {function} waveform - function from frequency and duration to sound
537-
* @param {number} base_frequency - frequency of the first harmonic
538-
* @param {number} duration - duration of the produced sound, in seconds
537+
* @param {Number} base_frequency - frequency of the first harmonic
538+
* @param {Number} duration - duration of the produced sound, in seconds
539539
* @param {list_of_envelope} envelopes - each a function from sound to sound
540540
* @returns {sound} resulting sound
541541
*/
@@ -561,8 +561,8 @@ function stacking_adsr(waveform, base_frequency, duration, envelopes) {
561561
/**
562562
* returns a sound that is reminiscent of a trombone, playing
563563
* a given note for a given <CODE>duration</CODE> of seconds
564-
* @param {number} note - midi note
565-
* @param {number} duration - duration in seconds
564+
* @param {Number} note - midi note
565+
* @param {Number} duration - duration in seconds
566566
* @returns {function} <CODE>stop</CODE> to stop recording,
567567
*/
568568
function trombone(note, duration) {
@@ -574,8 +574,8 @@ function trombone(note, duration) {
574574
/**
575575
* returns a sound that is reminiscent of a piano, playing
576576
* a given note for a given <CODE>duration</CODE> of seconds
577-
* @param {number} note - midi note
578-
* @param {number} duration - duration in seconds
577+
* @param {Number} note - midi note
578+
* @param {Number} duration - duration in seconds
579579
* @returns {function} <CODE>stop</CODE> to stop recording,
580580
*/
581581
function piano(note, duration) {
@@ -588,8 +588,8 @@ function piano(note, duration) {
588588
/**
589589
* returns a sound that is reminiscent of a bell, playing
590590
* a given note for a given <CODE>duration</CODE> of seconds
591-
* @param {number} note - midi note
592-
* @param {number} duration - duration in seconds
591+
* @param {Number} note - midi note
592+
* @param {Number} duration - duration in seconds
593593
* @returns {function} <CODE>stop</CODE> to stop recording,
594594
*/
595595
function bell(note, duration) {
@@ -603,8 +603,8 @@ function bell(note, duration) {
603603
/**
604604
* returns a sound that is reminiscent of a violin, playing
605605
* a given note for a given <CODE>duration</CODE> of seconds
606-
* @param {number} note - midi note
607-
* @param {number} duration - duration in seconds
606+
* @param {Number} note - midi note
607+
* @param {Number} duration - duration in seconds
608608
* @returns {function} <CODE>stop</CODE> to stop recording,
609609
*/
610610
function violin(note, duration) {
@@ -618,8 +618,8 @@ function violin(note, duration) {
618618
/**
619619
* returns a sound that is reminiscent of a cello, playing
620620
* a given note for a given <CODE>duration</CODE> of seconds
621-
* @param {number} note - midi note
622-
* @param {number} duration - duration in seconds
621+
* @param {Number} note - midi note
622+
* @param {Number} duration - duration in seconds
623623
* @returns {function} <CODE>stop</CODE> to stop recording,
624624
*/
625625
function cello(note, duration) {

public/externalLibs/sound/sounds.js

+27-21
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ function raw_to_audio(_data) {
147147

148148
/**
149149
* Makes a sound from a wave and a duration.
150-
* The wave is a function from time (in seconds)
150+
* The wave is a function from a non-negative time (in seconds)
151151
* to an amplitude value that should lie between
152152
* -1 and 1. The duration is given in seconds.
153153
* @param {function} wave - given wave function
154-
* @param {number} duration - in seconds
154+
* @param {Number} duration - in seconds
155155
* @returns {sound}
156156
*/
157157
function make_sound(wave, duration) {
@@ -160,7 +160,7 @@ function make_sound(wave, duration) {
160160

161161
/**
162162
* Accesses the wave of a sound.
163-
* The wave is a function from time (in seconds)
163+
* The wave is a function from a non-negative time (in seconds)
164164
* to an amplitude value that should lie between
165165
* -1 and 1.
166166
* @param {sound} sound - given sound
@@ -173,7 +173,7 @@ function get_wave(sound) {
173173
/**
174174
* Accesses the duration of a sound, in seconds.
175175
* @param {sound} sound - given sound
176-
* @returns {number} duration in seconds
176+
* @returns {Number} duration in seconds
177177
*/
178178
function get_duration(sound) {
179179
return tail(sound);
@@ -365,8 +365,14 @@ function stop() {
365365
/**
366366
* makes a new sound by combining the sounds in a given
367367
* list so that
368-
* they play consecutively, each next sound starting when the
369-
* previous sound ends
368+
* they are arranged consecutively. Let us say the durations
369+
* of the sounds are <CODE>d1</CODE>, ..., <CODE>dn</CODE> and the wave
370+
* functions are <CODE>w1</CODE>, ..., <CODE>wn</CODE>. Then the resulting
371+
* sound has the duration of the sum of <CODE>d1</CODE>, ..., <CODE>dn</CODE>.
372+
* The wave function <CODE>w</CODE> of the resulting sound uses <CODE>w1</CODE> for the first
373+
* <CODE>d1</CODE> seconds, <CODE>w2</CODE> for the next
374+
* <CODE>d2</CODE> seconds etc. We specify that <CODE>w(d1) = w2(0)</CODE>,
375+
* <CODE>w(d1+d2) = w3(0)</CODE>, etc
370376
* @param {list_of_sounds} sounds - given list of sounds
371377
* @returns {sound} resulting sound
372378
*/
@@ -414,7 +420,7 @@ function simultaneously(list_of_sounds) {
414420
/**
415421
* makes a sound of a given duration by randomly
416422
* generating amplitude values
417-
* @param {number} duration - duration of result sound, in seconds
423+
* @param {Number} duration - duration of result sound, in seconds
418424
* @returns {sound} resulting noise sound
419425
*/
420426
function noise_sound(duration) {
@@ -423,8 +429,8 @@ function noise_sound(duration) {
423429

424430
/**
425431
* makes a sine wave sound with given frequency and a given duration
426-
* @param {number} freq - frequency of result sound, in Hz
427-
* @param {number} duration - duration of result sound, in seconds
432+
* @param {Number} freq - frequency of result sound, in Hz, <CODE>freq</CODE> ≥ 0
433+
* @param {Number} duration - duration of result sound, in seconds
428434
* @returns {sound} resulting sine sound
429435
*/
430436
function sine_sound(freq, duration) {
@@ -433,7 +439,7 @@ function sine_sound(freq, duration) {
433439

434440
/**
435441
* makes a silence sound with a given duration
436-
* @param {number} duration - duration of result sound, in seconds
442+
* @param {Number} duration - duration of result sound, in seconds
437443
* @returns {sound} resulting silence sound
438444
*/
439445
function silence_sound(duration) {
@@ -448,7 +454,7 @@ function silence_sound(duration) {
448454
* See <a href="https://i.imgur.com/qGQgmYr.png">mapping from
449455
* letter name to midi notes</a>
450456
* @param {string} str - given letter name
451-
* @returns {number} midi value of the corresponding note
457+
* @returns {Number} midi value of the corresponding note
452458
*/
453459
function letter_name_to_midi_note(note) {
454460
// we don't consider double flat/ double sharp
@@ -511,26 +517,26 @@ function letter_name_to_midi_note(note) {
511517
* First converts <CODE>str</CODE> to a note using <CODE>letter_name_to_midi_note</CODE>
512518
* and then to a frequency using <CODE>midi_note_to_frequency</CODE>
513519
* @param {string} str - given letter name
514-
* @returns {number} frequency of corresponding note in Hz
520+
* @returns {Number} frequency of corresponding note in Hz
515521
*/
516522
function letter_name_to_frequency(note) {
517523
return midi_note_to_frequency(note_to_midi_note(note));
518524
}
519525

520526
/**
521527
* converts a midi note <CODE>n</CODE> to corresponding frequency.
522-
* The note is given as an integer number.
523-
* @param {number} n - given midi note
524-
* @returns {number} frequency of the note in Hz
528+
* The note is given as an integer Number.
529+
* @param {Number} n - given midi note
530+
* @returns {Number} frequency of the note in Hz
525531
*/
526532
function midi_note_to_frequency(note) {
527533
return 8.1757989156 * Math.pow(2, (note / 12));
528534
}
529535

530536
/**
531537
* makes a square wave sound with given frequency and a given duration
532-
* @param {number} freq - frequency of result sound, in Hz
533-
* @param {number} duration - duration of result sound, in seconds
538+
* @param {Number} freq - frequency of result sound, in Hz, <CODE>freq</CODE> ≥ 0
539+
* @param {Number} duration - duration of result sound, in seconds
534540
* @returns {sound} resulting square sound
535541
*/
536542
function square_sound(freq, duration) {
@@ -551,8 +557,8 @@ function square_sound(freq, duration) {
551557

552558
/**
553559
* makes a triangle wave sound with given frequency and a given duration
554-
* @param {number} freq - frequency of result sound, in Hz
555-
* @param {number} duration - duration of result sound, in seconds
560+
* @param {Number} freq - frequency of result sound, in Hz, <CODE>freq</CODE> ≥ 0
561+
* @param {Number} duration - duration of result sound, in seconds
556562
* @returns {sound} resulting triangle sound
557563
*/
558564
function triangle_sound(freq, duration) {
@@ -574,8 +580,8 @@ function triangle_sound(freq, duration) {
574580

575581
/**
576582
* makes a sawtooth wave sound with given frequency and a given duration
577-
* @param {number} freq - frequency of result sound, in Hz
578-
* @param {number} duration - duration of result sound, in seconds
583+
* @param {Number} freq - frequency of result sound, in Hz; <CODE>freq</CODE> ≥ 0
584+
* @param {Number} duration - duration of result sound, in seconds
579585
* @returns {sound} resulting sawtooth sound
580586
*/
581587
function sawtooth_sound(freq, duration) {

0 commit comments

Comments
 (0)