Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Aug 29, 2024
1 parent fc3242e commit d227473
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 50 deletions.
35 changes: 19 additions & 16 deletions docs/2-hear.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ <h2 id="let-your-computer-speak"><a class="header" href="#let-your-computer-spea
<p>The formal definition of a wave with frequency \(f\), is something like this:</p>
<p>\(sin(2\pi ft)\)</p>
<p>\(t\) is the variable of time in seconds, and \(f\) is the frequency. Plot this function and you'll see that the function oscillates between -1 and 1, for exactly \(f\) times during 1 second.</p>
<p>[IMAGE: Plot of sine wave]</p>
<p><img src="assets/sine.png" alt="sin(2.pi.5t) does 5 full cycles in 1 second" />{ width=250px }</p>
<p>Since \(t\) is a real number, there are infinite possible values for it (Even in a limited range like \([0,1]\)). We can't apply infinite different voltages to our magnetic plate in a finite time. A solution is to increase \(t\) with an fixed number, and take a <strong>sample</strong> out of the function. How fast we take samples from our function determines our <em><strong>sampling rate</strong></em>. A sampling rate of 100 means that we are taking 100 samples per second. In order to reach sampling rate of 100, we have to increase \(t\) by 0.001 everytime we take a new sample. The sampling rate should be high enough so that we are able to generate a wide range of frequencies, otherwise it'll become useless. A sampling-rate of around ~40000 is enough to be able to generate sounds that are hearable by a human.</p>
<p>A very standard sampling-rate for sounds is 44100. <code>pacat</code> uses it by default too. The next step is to substitute the \(t\) variable with \(\frac{n}{44100}\) where \(n\) is the sample number: \(sin(2\pi f\frac{n}{44100})\), then we should just create a loop in which the value of \(n\) by 1 (Effectively causing the value of \(t\) increases by \(\frac{1}{44100}\))</p>
<pre><code class="language-python=">import sys
Expand Down Expand Up @@ -257,34 +257,34 @@ <h2 id="let-your-computer-speak"><a class="header" href="#let-your-computer-spea
<p>Redirect your script's output to the <code>pacat</code> program and hear the voice of your program:</p>
<pre><code class="language-bash">pacat &lt;(python3 music.py)
</code></pre>
<p>[POINTER]</p>
<p>Now let's combine two frequencies and hear how it sounds!</p>
<h2 id="frequency-combos"><a class="header" href="#frequency-combos">Frequency combos!</a></h2>
<p>Remember that each arbitrary signal can be assumed as nothing but composition of sine waves? A fascinating fact is, our ears are actually able to recognize the existence of two different frequencies in a single signal, and we can actually experiment that! Instead of defining our <code>f(t)</code> function as a single sine, we can embed two sine components in a single signal by adding them together:</p>
<pre><code class="language-python">def f(t):
a = math.sin(t * 2 * math.pi * 440)
b = math.sin(t * 2 * math.pi * 660)
return (a + b) / 2
a = math.sin(t * 2 * math.pi * 440) * 0.5
b = math.sin(t * 2 * math.pi * 660) * 0.5
return a + b
</code></pre>
<p>We are taking an average from <code>a</code> and <code>b</code> so that the output value remains between -1 and 1.</p>
<p>If you hear this function, you will actually notice that it consists of two sounds. Your brain can successfully decomposite the output wave into two sounds, and this is amazing! The reason that you can recognize the 440Hz and 660Hz sound in the output of this script is the same reason you can hear your friend in a loud room full of noise, your brain is able to decouple sounds with different frequencies.</p>
<p>We are multiplying <code>a</code> and <code>b</code> by <code>0.5</code> so that the output value remains between -1 and 1, otherwise the value of <code>a + b</code> may go below -1 or above 1, which doesn't make sense to our audio device. When you apply a coefficient to a signal, the sound will remain the same, only the energy/intensity of it (Its volume), will decrease/increase! Here we are halving the volume of our original sine function so that some space remains for the other sine component.</p>
<p>If you hear this function, you will actually notice that it consists of two sounds. Your brain can successfully decompose the output wave into two sounds, and this is amazing! The reason that you can recognize the 440Hz and 660Hz sound in the output of this script is the same reason you can hear your friend in a loud room full of noise, your brain is able to decouple sounds with different frequencies.</p>
<h2 id="invent-your-sound-pallete"><a class="header" href="#invent-your-sound-pallete">Invent your sound pallete!</a></h2>
<p>Now that we are able to generate sounds of different frequencies, I want you to do an experiment. Try generating frequencies that are of the form \(2^nf\). E.g. try hearing these frequencies: \(440, 880, 1760, 3520, \dots\)</p>
<pre><code class="language-python=">def f(t):
sec = t * sample_rate
if sec &lt; 1:
if t &lt; 1:
return math.sin(t * 2 * math.pi * 440)
elif sec &lt; 2:
elif t &lt; 2:
return math.sin(t * 2 * math.pi * 440 * 2) # 880Hz
elif sec &lt; 3:
elif t &lt; 3:
return math.sin(t * 2 * math.pi * 440 * 2 * 2) # 1760Hz
elif sec &lt; 4:
elif t &lt; 4:
return math.sin(t * 2 * math.pi * 440 * 2 * 2 * 2) # 3520Hz
</code></pre>
<p>In the code above, we are generating different frequencies through time. We start with 440Hz, and we double the frequency every 1 second.</p>
<p>Hear them carefully, and then compare how they sound when their frequency is doubled. Try a different coefficient. Generate sounds that are of the form: \(1.5^nf\): \(440, 660, 990, 1485, \dots\)</p>
<p>We can discover something very strange and important here. Sounds that are generated when the frequency is doubled each time, are very similar to each other (At least, to our brain). While in the second experiment, sounds seem to be different with each other. If the sounds that are generated in the first experiment are similar, then what makes them different?</p>
<p>Let's try another experiment (This time without coding!). Play one of your favorite musics and try to sing on it. Try to sing on it with lower and higher pitches. Even though you have changed your voice, your singing still &quot;fits&quot; the song. By singing on a song with higher pitch, you won't shift your frequency by some number, but you will actually multiplying it by a power of two. A man and woman with totally different frequency ranges can both sing on the same song, but both of the singings will &quot;fit&quot; on the song, as long as the frequencies are multiplies of powers of 2. I think it's now safe two say, frequencies that are twiced every time, have same feelings.</p>
<p>Let's try another experiment (This time without coding!). Play one of your favorite musics and try to sing on it. Try to sing on it with lower and higher pitches. Even though you have changed your voice, your singing still &quot;fits&quot; the song. By singing on a song with higher pitch, you are not actually shifting your frequency by some number, but you will actually multiplying it by a power of two. A man and woman with totally different frequency ranges can both sing on the same song, but both of the singings will &quot;fit&quot; on the song, as long as the frequencies are multiplies of powers of 2. I think it's now safe two say, frequencies that are doubled (Or halved) every time, have same feelings.</p>
<p>Let's start with 300Hz. We calculate powers of two multiplied by the base frequency. Here is what we get:</p>
<p><code>..., 37.5Hz, 75Hz, 150Hz, 300Hz, 600Hz, 1200Hz, 2400Hz, ...</code></p>
<p>Let's agree that they are all basically the same &quot;sound&quot;, and let's put a name on them. Let's call them S1.</p>
<p>Although there areLet's agree that they are all basically the same &quot;sound&quot;, and let's put a name on them. Let's call them <strong>sound number 1</strong> or S1.</p>
<p>Whatever frequency we take from the range 300Hz-600Hz, there will also exist the corresponding &quot;same-feeling&quot; sound in ranges 75-150, 150-300, 600-1200, 1200-2400 and etc.</p>
<p>Conversely, we can also conclude that any random frequncy have an corresponding &quot;same-feeling&quot; frequency in 300Hz-600Hz range. In order to find its same-feeling frequency, we just need to halve/double it, until it gets between 300Hz-600Hz. (E.g. let's start with 1500Hz, we halve it 2 times and we get 375Hz which exists in the range, so 375Hz is the equivalent same-feeling sound of 1500Hz). We know there are infinite number of frequencies between 300Hz-600Hz. A single S1 is not enough for creating beautiful pieces of music. We could only build Morse codes out of the single sounds! So let's discover and define other sounds in the same range.</p>
<p>Imagine we define S2 as a sound that is in the middle of two S1s. Given that the self-feeling sounds are repeated in a loop, here is what we should expect:</p>
Expand Down Expand Up @@ -345,8 +345,11 @@ <h2 id="let-your-computer-speak"><a class="header" href="#let-your-computer-spea
F# = 440 * 1.05946^9 = 739.99
G = 440 * 1.05946^10 = 783.99
G# = 440 * 1.05946^11 = 830.61
A = 440 * 1.05946^12 = 440 * 2 = 880
</code></pre>
<hr />
<p>Just like painters, we now have a sound pallete!</p>
<p><img src="assets/pallete.png" alt="Musical notes are the colors of music!" /> { width=250px }</p>
<p>[POINTER]</p>
<p>Now that we have our pallete of sounds, we may start creating art! A simple melody consists of musical notes that come, stay for some time and then fade away. They won't last forever, and there will be times when no musical not is being played, silence itself is an ingredient of music.</p>
<p>Before generating the music itself, let's define the list of constants the will represent the frequencies of different musical notes:</p>
<pre><code class="language-python=">A = 440 # La
Expand Down
Binary file added docs/assets/pallete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/sine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 19 additions & 16 deletions docs/print.html
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ <h2 id="let-your-computer-speak"><a class="header" href="#let-your-computer-spea
<p>The formal definition of a wave with frequency \(f\), is something like this:</p>
<p>\(sin(2\pi ft)\)</p>
<p>\(t\) is the variable of time in seconds, and \(f\) is the frequency. Plot this function and you'll see that the function oscillates between -1 and 1, for exactly \(f\) times during 1 second.</p>
<p>[IMAGE: Plot of sine wave]</p>
<p><img src="assets/sine.png" alt="sin(2.pi.5t) does 5 full cycles in 1 second" />{ width=250px }</p>
<p>Since \(t\) is a real number, there are infinite possible values for it (Even in a limited range like \([0,1]\)). We can't apply infinite different voltages to our magnetic plate in a finite time. A solution is to increase \(t\) with an fixed number, and take a <strong>sample</strong> out of the function. How fast we take samples from our function determines our <em><strong>sampling rate</strong></em>. A sampling rate of 100 means that we are taking 100 samples per second. In order to reach sampling rate of 100, we have to increase \(t\) by 0.001 everytime we take a new sample. The sampling rate should be high enough so that we are able to generate a wide range of frequencies, otherwise it'll become useless. A sampling-rate of around ~40000 is enough to be able to generate sounds that are hearable by a human.</p>
<p>A very standard sampling-rate for sounds is 44100. <code>pacat</code> uses it by default too. The next step is to substitute the \(t\) variable with \(\frac{n}{44100}\) where \(n\) is the sample number: \(sin(2\pi f\frac{n}{44100})\), then we should just create a loop in which the value of \(n\) by 1 (Effectively causing the value of \(t\) increases by \(\frac{1}{44100}\))</p>
<pre><code class="language-python=">import sys
Expand Down Expand Up @@ -1808,34 +1808,34 @@ <h2 id="let-your-computer-speak"><a class="header" href="#let-your-computer-spea
<p>Redirect your script's output to the <code>pacat</code> program and hear the voice of your program:</p>
<pre><code class="language-bash">pacat &lt;(python3 music.py)
</code></pre>
<p>[POINTER]</p>
<p>Now let's combine two frequencies and hear how it sounds!</p>
<h2 id="frequency-combos"><a class="header" href="#frequency-combos">Frequency combos!</a></h2>
<p>Remember that each arbitrary signal can be assumed as nothing but composition of sine waves? A fascinating fact is, our ears are actually able to recognize the existence of two different frequencies in a single signal, and we can actually experiment that! Instead of defining our <code>f(t)</code> function as a single sine, we can embed two sine components in a single signal by adding them together:</p>
<pre><code class="language-python">def f(t):
a = math.sin(t * 2 * math.pi * 440)
b = math.sin(t * 2 * math.pi * 660)
return (a + b) / 2
a = math.sin(t * 2 * math.pi * 440) * 0.5
b = math.sin(t * 2 * math.pi * 660) * 0.5
return a + b
</code></pre>
<p>We are taking an average from <code>a</code> and <code>b</code> so that the output value remains between -1 and 1.</p>
<p>If you hear this function, you will actually notice that it consists of two sounds. Your brain can successfully decomposite the output wave into two sounds, and this is amazing! The reason that you can recognize the 440Hz and 660Hz sound in the output of this script is the same reason you can hear your friend in a loud room full of noise, your brain is able to decouple sounds with different frequencies.</p>
<p>We are multiplying <code>a</code> and <code>b</code> by <code>0.5</code> so that the output value remains between -1 and 1, otherwise the value of <code>a + b</code> may go below -1 or above 1, which doesn't make sense to our audio device. When you apply a coefficient to a signal, the sound will remain the same, only the energy/intensity of it (Its volume), will decrease/increase! Here we are halving the volume of our original sine function so that some space remains for the other sine component.</p>
<p>If you hear this function, you will actually notice that it consists of two sounds. Your brain can successfully decompose the output wave into two sounds, and this is amazing! The reason that you can recognize the 440Hz and 660Hz sound in the output of this script is the same reason you can hear your friend in a loud room full of noise, your brain is able to decouple sounds with different frequencies.</p>
<h2 id="invent-your-sound-pallete"><a class="header" href="#invent-your-sound-pallete">Invent your sound pallete!</a></h2>
<p>Now that we are able to generate sounds of different frequencies, I want you to do an experiment. Try generating frequencies that are of the form \(2^nf\). E.g. try hearing these frequencies: \(440, 880, 1760, 3520, \dots\)</p>
<pre><code class="language-python=">def f(t):
sec = t * sample_rate
if sec &lt; 1:
if t &lt; 1:
return math.sin(t * 2 * math.pi * 440)
elif sec &lt; 2:
elif t &lt; 2:
return math.sin(t * 2 * math.pi * 440 * 2) # 880Hz
elif sec &lt; 3:
elif t &lt; 3:
return math.sin(t * 2 * math.pi * 440 * 2 * 2) # 1760Hz
elif sec &lt; 4:
elif t &lt; 4:
return math.sin(t * 2 * math.pi * 440 * 2 * 2 * 2) # 3520Hz
</code></pre>
<p>In the code above, we are generating different frequencies through time. We start with 440Hz, and we double the frequency every 1 second.</p>
<p>Hear them carefully, and then compare how they sound when their frequency is doubled. Try a different coefficient. Generate sounds that are of the form: \(1.5^nf\): \(440, 660, 990, 1485, \dots\)</p>
<p>We can discover something very strange and important here. Sounds that are generated when the frequency is doubled each time, are very similar to each other (At least, to our brain). While in the second experiment, sounds seem to be different with each other. If the sounds that are generated in the first experiment are similar, then what makes them different?</p>
<p>Let's try another experiment (This time without coding!). Play one of your favorite musics and try to sing on it. Try to sing on it with lower and higher pitches. Even though you have changed your voice, your singing still &quot;fits&quot; the song. By singing on a song with higher pitch, you won't shift your frequency by some number, but you will actually multiplying it by a power of two. A man and woman with totally different frequency ranges can both sing on the same song, but both of the singings will &quot;fit&quot; on the song, as long as the frequencies are multiplies of powers of 2. I think it's now safe two say, frequencies that are twiced every time, have same feelings.</p>
<p>Let's try another experiment (This time without coding!). Play one of your favorite musics and try to sing on it. Try to sing on it with lower and higher pitches. Even though you have changed your voice, your singing still &quot;fits&quot; the song. By singing on a song with higher pitch, you are not actually shifting your frequency by some number, but you will actually multiplying it by a power of two. A man and woman with totally different frequency ranges can both sing on the same song, but both of the singings will &quot;fit&quot; on the song, as long as the frequencies are multiplies of powers of 2. I think it's now safe two say, frequencies that are doubled (Or halved) every time, have same feelings.</p>
<p>Let's start with 300Hz. We calculate powers of two multiplied by the base frequency. Here is what we get:</p>
<p><code>..., 37.5Hz, 75Hz, 150Hz, 300Hz, 600Hz, 1200Hz, 2400Hz, ...</code></p>
<p>Let's agree that they are all basically the same &quot;sound&quot;, and let's put a name on them. Let's call them S1.</p>
<p>Although there areLet's agree that they are all basically the same &quot;sound&quot;, and let's put a name on them. Let's call them <strong>sound number 1</strong> or S1.</p>
<p>Whatever frequency we take from the range 300Hz-600Hz, there will also exist the corresponding &quot;same-feeling&quot; sound in ranges 75-150, 150-300, 600-1200, 1200-2400 and etc.</p>
<p>Conversely, we can also conclude that any random frequncy have an corresponding &quot;same-feeling&quot; frequency in 300Hz-600Hz range. In order to find its same-feeling frequency, we just need to halve/double it, until it gets between 300Hz-600Hz. (E.g. let's start with 1500Hz, we halve it 2 times and we get 375Hz which exists in the range, so 375Hz is the equivalent same-feeling sound of 1500Hz). We know there are infinite number of frequencies between 300Hz-600Hz. A single S1 is not enough for creating beautiful pieces of music. We could only build Morse codes out of the single sounds! So let's discover and define other sounds in the same range.</p>
<p>Imagine we define S2 as a sound that is in the middle of two S1s. Given that the self-feeling sounds are repeated in a loop, here is what we should expect:</p>
Expand Down Expand Up @@ -1896,8 +1896,11 @@ <h2 id="let-your-computer-speak"><a class="header" href="#let-your-computer-spea
F# = 440 * 1.05946^9 = 739.99
G = 440 * 1.05946^10 = 783.99
G# = 440 * 1.05946^11 = 830.61
A = 440 * 1.05946^12 = 440 * 2 = 880
</code></pre>
<hr />
<p>Just like painters, we now have a sound pallete!</p>
<p><img src="assets/pallete.png" alt="Musical notes are the colors of music!" /> { width=250px }</p>
<p>[POINTER]</p>
<p>Now that we have our pallete of sounds, we may start creating art! A simple melody consists of musical notes that come, stay for some time and then fade away. They won't last forever, and there will be times when no musical not is being played, silence itself is an ingredient of music.</p>
<p>Before generating the music itself, let's define the list of constants the will represent the frequencies of different musical notes:</p>
<pre><code class="language-python=">A = 440 # La
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/searchindex.json

Large diffs are not rendered by default.

Loading

0 comments on commit d227473

Please sign in to comment.