Sick Gaming
[Tut] This is How I Played a Sinus Tone in My Jupyter Notebook (Python) - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] This is How I Played a Sinus Tone in My Jupyter Notebook (Python) (/thread-100432.html)



[Tut] This is How I Played a Sinus Tone in My Jupyter Notebook (Python) - xSicKxBot - 12-19-2022

This is How I Played a Sinus Tone in My Jupyter Notebook (Python)

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;985736&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</p></div>
<p><strong>What/Why?</strong> I want to write a simple Python script that warns me if crypto price data (e.g., BTC) crosses a certain threshold. This can be useful for trading or some other apps, so I thought it would be fun to do it. </p>
<p>The tutorial in front of you simply documents my learnings on creating a sinus tone in my Jupyter Notebook—so it may benefit you as well. </p>
<p>If you want the whole tutorial on my mini project, you can check it out here on the Finxter blog: </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/how-i-created-a-python-script-that-warns-me-if-bitcoin-or-eth-price-is-above-a-certain-value/" data-type="post" data-id="985731" target="_blank" rel="noreferrer noopener">I Made a Python Script That Beeps When BTC or ETH Prices Drop</a></p>
<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1002" height="668" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-250.png" alt="" class="wp-image-987266" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-250.png 1002w, https://blog.finxter.com/wp-content/uploads/2022/12/image-250-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2022/12/image-250-768x512.png 768w" sizes="(max-width: 1002px) 100vw, 1002px" /></figure>
<h2>Challenge</h2>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Challenge</strong>: Write Python code in a Jupyter Notebook that creates a sinus tone when executed. </p>
<h2>Solution</h2>
<p>The easy way to solve this challenge is the following. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p class="has-base-background-color has-background">This code creates a sine wave with a frequency of 500 Hz and plays it in the IPython environment. The wave is created using the NumPy library by specifying the frequency and the length of the wave (15000*2). The rate of the wave is set to 10000 Hz and autoplay is set to <code>True</code> so that the wave will start playing immediately.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
from IPython.display import Audio # Create the tone as a NumPy Sinus Wave
wave = np.sin(2*np.pi*500*np.arange(15000*2)/15000) # Play the Sinus Wave (tone)
Audio(wave, rate=10000, autoplay=True)</pre>
<p>This generates the following beep sound in your Jupyter Notebook:</p>
<figure class="wp-block-audio"><audio controls src="https://blog.finxter.com/wp-content/uploads/2022/12/peep.mp3"></audio></figure>
<p>What happens if you change the <code>rate</code> argument of the <code>Audio()</code> function call to be 20000 instead of 10000?</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Play the Sinus Wave (tone)
Audio(wave, rate=20000, autoplay=True)</pre>
<p>The beep sound tone gets higher:</p>
<figure class="wp-block-audio"><audio controls src="https://blog.finxter.com/wp-content/uploads/2022/12/beep_20000.mp3"></audio></figure>
<p>You can play around with the Jupyter notebook here:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://colab.research.google.com/drive/1Kd4G_9sn6EFy7lZLLUYBsUQh7DXz-SSG?usp=sharing" target="_blank" rel="noreferrer noopener"><img decoding="async" loading="lazy" width="720" height="382" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-248.png" alt="" class="wp-image-987259" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-248.png 720w, https://blog.finxter.com/wp-content/uploads/2022/12/image-248-300x159.png 300w" sizes="(max-width: 720px) 100vw, 720px" /></a></figure>
</div>
<p>But what if you don’t have a Jupyter notebook but a normal Python script (Win/Linux/macOS)?</p>
<p>In that case, you cannot use the <code>IPython</code> library. Instead, follow the steps outlined in the following tutorial on the Finxter blog—you still can play beep sounds!</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/how-to-make-a-beep-sound-in-python-linux-macos-win/" data-type="post" data-id="147986" target="_blank" rel="noreferrer noopener">How to Make a Beep Sound in Python?</a></p>
<h2>Thanks for Reading! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2665.png" alt="♥" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </h2>
<p>You’re welcome to join our free email academy where I share all our coding projects and cheat sheets on a weekly basis:</p>
</div>


https://www.sickgaming.net/blog/2022/12/18/this-is-how-i-played-a-sinus-tone-in-my-jupyter-notebook-python/