{"id":130643,"date":"2022-12-18T14:31:26","date_gmt":"2022-12-18T14:31:26","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=985736"},"modified":"2022-12-18T14:31:26","modified_gmt":"2022-12-18T14:31:26","slug":"this-is-how-i-played-a-sinus-tone-in-my-jupyter-notebook-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/12\/18\/this-is-how-i-played-a-sinus-tone-in-my-jupyter-notebook-python\/","title":{"rendered":"This is How I Played a Sinus Tone in My Jupyter Notebook (Python)"},"content":{"rendered":"\n<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;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<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>\n<p>The tutorial in front of you simply documents my learnings on creating a sinus tone in my Jupyter Notebook&#8212;so it may benefit you as well. <\/p>\n<p>If you want the whole tutorial on my mini project, you can check it out here on the Finxter blog: <\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n<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=\"auto, (max-width: 1002px) 100vw, 1002px\" \/><\/figure>\n<h2>Challenge<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" 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>\n<h2>Solution<\/h2>\n<p>The easy way to solve this challenge is the following. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<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>\n<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\nfrom IPython.display import Audio # Create the tone as a NumPy Sinus Wave\nwave = np.sin(2*np.pi*500*np.arange(15000*2)\/15000) # Play the Sinus Wave (tone)\nAudio(wave, rate=10000, autoplay=True)<\/pre>\n<p>This generates the following beep sound in your Jupyter Notebook:<\/p>\n<figure class=\"wp-block-audio\"><audio controls src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/peep.mp3\"><\/audio><\/figure>\n<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>\n<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)\nAudio(wave, rate=20000, autoplay=True)<\/pre>\n<p>The beep sound tone gets higher:<\/p>\n<figure class=\"wp-block-audio\"><audio controls src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/beep_20000.mp3\"><\/audio><\/figure>\n<p>You can play around with the Jupyter notebook here:<\/p>\n<div class=\"wp-block-image\">\n<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=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/figure>\n<\/div>\n<p>But what if you don&#8217;t have a Jupyter notebook but a normal Python script (Win\/Linux\/macOS)?<\/p>\n<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&#8212;you still can play beep sounds!<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n<h2>Thanks for Reading! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2665.png\" alt=\"\u2665\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <\/h2>\n<p>You&#8217;re welcome to join our free email academy where I share all our coding projects and cheat sheets on a weekly basis:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) What\/Why? 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. The tutorial in front of you simply documents my [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-130643","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=130643"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130643\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}