<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: seb</title>
    <description>The latest articles on DEV Community by seb (@sebseb).</description>
    <link>https://dev.to/sebseb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F728836%2Fcd580e71-bf6d-4c18-bec9-130f613253ed.jpeg</url>
      <title>DEV Community: seb</title>
      <link>https://dev.to/sebseb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sebseb"/>
    <language>en</language>
    <item>
      <title>Routines I Use to Stay Happy &amp; Zen While Spending Multiple Hours Per Day in Front of a Screen</title>
      <dc:creator>seb</dc:creator>
      <pubDate>Mon, 07 Feb 2022 08:25:03 +0000</pubDate>
      <link>https://dev.to/sebseb/routines-i-use-to-stay-happy-zen-while-spending-multiple-hours-per-day-in-front-of-a-screen-5b6k</link>
      <guid>https://dev.to/sebseb/routines-i-use-to-stay-happy-zen-while-spending-multiple-hours-per-day-in-front-of-a-screen-5b6k</guid>
      <description>&lt;p&gt;As web developers we tend to spend many hours day in and day out staring at a computer screen and in mostly static positions. This may over time put a toll on our physical and mental wellbeing.&lt;/p&gt;

&lt;p&gt;This is something I care about deeply because to me optimizing physical and mental wellbeing is an absolute priority, and over the years I've implemented different tools, routines and techniques that allow me to stay healthy and take care of my body even while I'm building my current projects in front of a computer. In case you're curious, these days my two main projects are &lt;a href="https://fffuel.co/"&gt;fffuel&lt;/a&gt;, where I build a collection of SVG generators, design tools and HTML templates as well as &lt;a href="https://muted.io/"&gt;muted.io&lt;/a&gt;, where I create interactive references for music theory.&lt;/p&gt;

&lt;p&gt;In this article I'll share some of the things I do to stay healthy and active while doing creative work on a computer.&lt;/p&gt;

&lt;p&gt;Note that these are just my personal choices and experiences and shouldn't be viewed as medical advice of any sort.&lt;br&gt;
Please seek out the advice from a licensed health care provider for any physical or mental health concern.&lt;/p&gt;

&lt;h2&gt;
  
  
  Taking Mini-Breaks
&lt;/h2&gt;

&lt;p&gt;I like to use this Pomodoro app on my Mac called &lt;strong&gt;Be Focused&lt;/strong&gt;, but I use it to remind me to take a short break every 20 minutes instead of using it for the normal Pomodoro productivity technique. So I have it setup so that every 20 minutes there's a gentle chime reminding me to take a short break, where I move around, do things like air squats, stretch for a bit and look outside the windows at the trees and the nature I can see from my place.&lt;/p&gt;

&lt;p&gt;I also put together a little web app called &lt;a href="https://rrrelax.app/"&gt;rrrelax&lt;/a&gt; to encourage taking long deep breaths. Using this app for a minute here and there during the day can be an other options for mini breaks that are calming.&lt;/p&gt;

&lt;h3&gt;
  
  
  Staying Focused
&lt;/h3&gt;

&lt;p&gt;When I'm not in a mini-break, I make it a point to be laser-focused on what I'm doing. For this, I like to turn off notifications and keep my environment free of distractions. I don't think we are fundamentally made for multitasking, and I find it much more fulfilling to go deep into a task and let the rest fade away.&lt;/p&gt;

&lt;h2&gt;
  
  
  Going Analog
&lt;/h2&gt;

&lt;p&gt;I've gotten into the habit of writing my todos and ideas on actual paper. I enjoy the tactile feel of writing on paper and it feels better to me to cross something off physically on paper than on a digital todo app. I even keep my upcoming due dates written on paper.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lunch Breaks
&lt;/h2&gt;

&lt;p&gt;I try to make it a point not to eat while I'm at the computer. I find that doing so makes me eat too fast and less mindfully, plus it encourages me to eat too much and not realize when I'm full. This one is trickier for a recovering type-A like me, but I find that not much beats taking a long lunch break outside in the sunshine. Trying it out for a few days might prove to be a real game changer by allowing to come back refreshed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Protecting My Eyes
&lt;/h2&gt;

&lt;p&gt;In my opinion, just like a smart construction worker would protect their hands from wear and tear, a smart knowledge worker would protect their eyes from strain and overuse. On top of taking mini-breaks every 20 minutes or so and looking away at a distance, I implement a few other things to project my eyes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I use a tool like &lt;a href="https://justgetflux.com/"&gt;f.Lux&lt;/a&gt; to reduce the amount of blue light emitted by the screen. Some people use f.lux in the evenings only, but for me I'm now used to have it on all of them time, except when I need to see colors with more accuracy. When I turn it off now I'm always amazed at how harsh the 'normal' colors feel.&lt;/li&gt;
&lt;li&gt;On top of using f.lux, I also wear blue-blocking glasses to reduce the amount of blue light that reaches my eyes from screens.&lt;/li&gt;
&lt;li&gt;The default font size on most websites tends to be way too small in my opinion. I like to make most of the longer text I read much larger, including in my code editor. My screen is large enough, so why not make it easier on my eyes, I feel.&lt;/li&gt;
&lt;li&gt;I like to use the &lt;a href="https://ethanschoonover.com/solarized/"&gt;Solarized color theme&lt;/a&gt; in my code editor, as I find it gentler on the eyes from the less extreme contrast ratio. Duck Duck Go, if that also happens to be your search engine of choice, can also easily be setup to use the same colors as the Solarized theme. This way the things you look at the most have a contrast ratio that's less intense and more gentle.&lt;/li&gt;
&lt;li&gt;I'm not a big fan of reading long blog posts on a computer screen. These days when I can I'll either print them on paper and read them while I'm outside in nature, or I'll use a plugin such as &lt;a href="https://speechify.com/"&gt;Speechify&lt;/a&gt; so I listen to articles instead of reading them. While I'm listening I can even do simple movements like mini Qigong routines.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Typing
&lt;/h2&gt;

&lt;p&gt;This one was a serious time and willpower commitment, but years ago I decided to learn to type using the &lt;a href="https://colemak.com/"&gt;Colemak&lt;/a&gt; keyboard layout, and today I'm very thankful that I went through the challenge of re-learning to type, because my keystrokes are so much more efficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  Natural Light
&lt;/h2&gt;

&lt;p&gt;During the day natural light is a must for me and I find artificial light to be harsh and depressing. I know this is not always easy to implement, but for me I took that as my most important consideration when I decided on the location of my workstation. When the evening comes, if I happen to still be at the computer, I like to use side lamps instead of overhead light, and I also use red-tinted light bulbs so that the lighting is more conducive to calm and to keeping with a healthy circadian rhythm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up my Environment
&lt;/h2&gt;

&lt;p&gt;If you happen to work from home and are able to setup the environment around you during the day however you like, I find that there are a few things that are conductive to more movement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keeping a yoga mat nearby and unrolled. I have a yoga mat near me that's always unrolled and ready for me. I like to go into a quick downward dog or child's pose here and there&lt;/li&gt;
&lt;li&gt;Having a pull-up bar. I have a pull-up bar that I've setup in a doorway. The pull-up bar was inexpensive and I use it for pull-ups through the day here and there, and oftentimes I'll also use it just to hang for a bit and stretch my spine. It feels really good to me and now I don't get how I went so many years without a pull-up bar like that&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Peaceful Environment
&lt;/h3&gt;

&lt;p&gt;On top of having an environment around me that's conductive to movement and exposure to natural light, I also make a point to make it a peaceful and calming place. For this, I do a few things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Diffusing some essential oils around my workstation. I like citrus oil blends, as I feel that it makes the place feel lighter and brighter&lt;/li&gt;
&lt;li&gt;Keeping organic elements around like plants and crystals&lt;/li&gt;
&lt;li&gt;Playing music that's appropriate for my current mood. That can often be very calm, soothing instrumental music, but sometimes it's also very energetic music. At times I'll find a track that really does it for me, so I'll put it on repeat for as long as I'm feeling it. The rhythmic and repetitive aspect help me stay in the flow of things.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Standing Desk
&lt;/h2&gt;

&lt;p&gt;A standing desk is a must for me now, and I barely sit in front of a computer these days. My current standing desk is the Readydesk, which has been really perfect for me. Standing on hard floor statically doesn't work well for me either, so I found this 'anti-fatigue' mat called the &lt;strong&gt;Topo mat&lt;/strong&gt; which mimics terrain and has different shapes where I can move my feet and be on a more natural surface that has some give. I alternate between standing on that mat and standing on a balance board from Fluid Stance, and at times I also stand on these wooden pieces called SoleSteps, which help with my posture and arches.&lt;/p&gt;

&lt;p&gt;I keep a stool around for the minimal amount of sitting that I do. The stool is simpler and takes less room than a chair, it encourages me to keep a straight back when I'm sitting on it and it also encourages me not to sit on it too long since it's not as comfortable as a padded chair.&lt;/p&gt;

&lt;p&gt;I find that alternating between these different options is really optimal for me, as it keep me active in different ways and eliminates the problem with being static.&lt;/p&gt;

&lt;h2&gt;
  
  
  Taking More Time to Myself
&lt;/h2&gt;

&lt;p&gt;I decided to check out of the hustle and bustle, and now when it's pretty and warm out my first priority is to go spend time in nature, with one of my favorite activities being hiking in the mountains. I can easily bring a book or a journal with me while out in nature, so that I can still learn, think and take notes.&lt;/p&gt;

&lt;p&gt;It can seem counter productive, but I've found over time that, at least for me, doing 8-hour workdays or so is actually slowing me down because it has too much of a negative impact of my energy levels, mood, creativity and general outlook. I found that often the best ideas will come to me as I'm out in nature. So for me personally, 4-5 hours of focused productivity is plenty, and much more than that ends up being a net negative for the most part.&lt;/p&gt;

&lt;p&gt;In terms of having a more big picture view of the value of work and time spent being productive, I really enjoyed two books in particular: The &lt;a href="https://fourhourworkweek.com/"&gt;4-Hour Workweek&lt;/a&gt; by Tim Ferriss and &lt;a href="https://www.amazon.com/Deep-Work-Focused-Success-Distracted/dp/1455586692"&gt;Deep Work&lt;/a&gt; by Cal Newport.&lt;/p&gt;

&lt;p&gt;Note however that having a totally different big picture view on the value of productive hours took years to fully sink in and implement in a way that works well for me. It wasn't done overnight, and I made choices over time that made it easier, like living outside of a major urban area and close to nature and cutting down on expenses that would make my lifestyle more expansive to maintain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Technology Fasts
&lt;/h3&gt;

&lt;p&gt;One thing that I've really enjoyed is doing is a weekly technology fast, where I'll avoid screens for an entire day once in a while. It can be challenging at first, and might not be feasible for every stage of one's career, but when possible I find that to be very beneficial and resourceful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Self Care
&lt;/h2&gt;

&lt;p&gt;Outside of spending as much time in nature as I can, when possible I also like to do some other forms of self care, including things like meditation, taking baths, ecstatic dancing or going for long walks. Once in a while I also like to offer myself a special session. Some ideas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Getting a massage&lt;/li&gt;
&lt;li&gt;Spa day&lt;/li&gt;
&lt;li&gt;Going to a yoga class&lt;/li&gt;
&lt;li&gt;Reiki session&lt;/li&gt;
&lt;li&gt;Sensory deprivation tanks&lt;/li&gt;
&lt;li&gt;Acupuncture&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I really resonated with a quote I saw from an Instagram post that went a bit like this: &lt;strong&gt;"Self-care is Productive"&lt;/strong&gt;. It might not seem so in the short-term, but in the long term I find this to be absolutely true.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sleep
&lt;/h2&gt;

&lt;p&gt;Sleep is a major priority for me and and skipping on sleep is not an option at all for me at this point. I have found that being productive is often more about energy management than time management, meaning that having more energy beats having more time, and nothing correlates as much with having energy than getting plenty of optimal sleep for me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parting Words
&lt;/h2&gt;

&lt;p&gt;In my view, humans shouldn't be serving computers, so I've designed an environment over the years that's more and more human-friendly and use technology in a way that serves me instead of the other way around.&lt;/p&gt;

&lt;p&gt;I'm hoping that some of the things I've implemented in my own life will inspire some positive changes in yours as well. ✨&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>mentalhealth</category>
    </item>
    <item>
      <title>Playing Sound on the Web Using Tone.js and Alpine.js</title>
      <dc:creator>seb</dc:creator>
      <pubDate>Tue, 02 Nov 2021 00:12:29 +0000</pubDate>
      <link>https://dev.to/sebseb/playing-sound-on-the-web-using-tonejs-and-alpinejs-17ad</link>
      <guid>https://dev.to/sebseb/playing-sound-on-the-web-using-tonejs-and-alpinejs-17ad</guid>
      <description>&lt;p&gt;I've been having a ton of fun creating interactive musical tools and references over at &lt;a href="https://muted.io/"&gt;muted.io&lt;/a&gt;. Things like an &lt;a href="https://muted.io/circle-of-fifths/"&gt;interactive circle of 5ths&lt;/a&gt;, a &lt;a href="https://muted.io/major-minor-scales/"&gt;reference to all major and minor scales&lt;/a&gt; and a tool to &lt;a href="https://muted.io/chords-in-keys/"&gt;play chords in keys&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Under the hood, these tools are powered by the Tone.js library, which is a set of utilities build on top of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API"&gt;Web Audio API&lt;/a&gt;, which makes it easier to deal with audio in the browser from a musician's perspective. For the aformentioned tools, the user interactions are handled using &lt;a href="https://alpinejs.dev/"&gt;Alpine.js&lt;/a&gt;. I've found that the combination of Tone.js + Alpine.js really works like a charm.&lt;/p&gt;

&lt;p&gt;This short post gives you a little primer on how you'd go about setting things up to play audio files in the browser in such a fashion.&lt;/p&gt;

&lt;p&gt;First things first, you'll want to have both Tone.js and Alpine.js loaded onto your page. If you have a look at the &lt;a href="https://tonejs.github.io/"&gt;Tone.js documentation&lt;/a&gt; it'll tell you installation instruction via &lt;code&gt;npm&lt;/code&gt;, but personally I've been enjoying working with just a call to the minified script file itself. To do that via a CDN, you can add this in your page's &lt;code&gt;head&lt;/code&gt; section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.32/Tone.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then similarly for &lt;a href="https://alpinejs.dev/essentials/installation"&gt;installing Alpine.js&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that on a site like muted.io I've decided to load Tone.js only when the user has scrolled passed the relevant portion of the page. I'm using Alpine's &lt;a href="https://alpinejs.dev/plugins/intersect"&gt;Intersect plugin&lt;/a&gt; to accomplish that. This is of course optional and I may talk about the details of that in a future post.&lt;/p&gt;




&lt;p&gt;With the setup out of the way, you should now see a message in your browser console that says something like &lt;strong&gt;_ Tone.js v14.8.32 _ &lt;/strong&gt;, meaning that Tone.js has been properly loaded and is ready to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tone.js Sampler
&lt;/h2&gt;

&lt;p&gt;A sampler is an instrument that makes it easy to playback different audio files. Tone.js offers its own &lt;a href="https://tonejs.github.io/docs/14.7.77/Sampler"&gt;sampler instrument&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sampler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Tone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Sampler&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;C3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;C3.mp3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;D#3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ds3.mp3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;F#3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fs3.mp3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;A3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A3.mp3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;C4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;C4.mp3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;D#4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ds4.mp3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;F#4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fs4.mp3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;A4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A4.mp3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;release&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/sounds/piano/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;toDestination&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code block I'm instantiating a sampler and passing in a path to audio files for different musical notes on the piano. In this case I'm using piano samples from the &lt;a href="https://archive.org/details/SalamanderGrandPianoV3"&gt;Salamander Grand Piano V3&lt;/a&gt; project, but you could use any of your own samples. In this case, the sounds are in my project's directory under &lt;code&gt;/sounds/piano/&lt;/code&gt;. You'll notice also that not all notes are included, that's because Tone.js is smart enough to repitch the samples and make up for any missing pitches in that way. This is really useful in saving on loading time for samples.&lt;/p&gt;

&lt;p&gt;This setup works great in a musical contact for playing sounds that actually correspond to musical pitches, but you could of course use a sampler to trigger totally unrelated sounds. You could for example decide that &lt;code&gt;C4&lt;/code&gt; triggers the sound of a toucan while &lt;code&gt;A4&lt;/code&gt; is for an abrasive dog bark. 🐕&lt;/p&gt;

&lt;h2&gt;
  
  
  Playing the Sounds
&lt;/h2&gt;

&lt;p&gt;Now that we have our sampler instrument setup, we're ready to start listening to user interactions and trigger the sounds. Let's first define a simple function that triggers the passed-in note:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;play&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;C4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;sampler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;triggerAttackRelease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;8n&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, calling &lt;code&gt;play()&lt;/code&gt; will trigger the audio file associated with the note provided (or default to &lt;code&gt;C4&lt;/code&gt;) in your sampler for a duration of an 8th note. The default BPM value in Tone.js is &lt;code&gt;120&lt;/code&gt;, which will be what controls how long a &lt;code&gt;8th&lt;/code&gt; note is. You can tweak the BPM value like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Tone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bpm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 96 BPM instead of 120&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have our &lt;code&gt;play&lt;/code&gt; function in place, we can use Alpine to setup a listnener on something like a button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;click&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;play('A3')&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Play&lt;/span&gt; &lt;span class="nx"&gt;A3&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And done! You should now hear the sample that your sampler has for &lt;code&gt;A3&lt;/code&gt;. Note here that the button click is important because modern browsers require a user interaction like a button click to start playing sounds on a page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Separating the attack from the release
&lt;/h3&gt;

&lt;p&gt;Earlier we made use of the &lt;code&gt;triggerAttackRelease&lt;/code&gt; on our sampler, which takes care of triggering the sample and also of releasing that trigger after the duration provided (a &lt;code&gt;8th&lt;/code&gt; note in our example). What if instead we wanted to play a sound for as long as the user is currently pushing a button? This is often useful for long samples that are to be played only while a note is activated (e.g.: a button is pressed). We can easily decouple the operation by using the &lt;code&gt;triggerAttack&lt;/code&gt; and &lt;code&gt;triggerRelease&lt;/code&gt; methods instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;startPlay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;sampler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;triggerAttack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;stopPlay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;sampler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;triggerRelease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that you could also pass in an array with multiple notes at once to any of those methods (&lt;code&gt;triggerAttackRelease&lt;/code&gt;, &lt;code&gt;triggerAttack&lt;/code&gt;, &lt;code&gt;triggerRelease&lt;/code&gt;), allowing you to trigger things like chords, if you're triggering sounds in a musical context.&lt;/p&gt;

&lt;p&gt;And now, we can once again make use of Alpine's event handling capabilities to :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt;
  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;mousedown.stop=&lt;/span&gt;&lt;span class="s"&gt;"startPlay('A4');"&lt;/span&gt;
  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;mouseup.stop=&lt;/span&gt;&lt;span class="s"&gt;"stopPlay('A4');"&lt;/span&gt;
  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;touchstart.stop.prevent=&lt;/span&gt;&lt;span class="s"&gt;"startPlay('A4');"&lt;/span&gt;
  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;touchend.stop.prevent=&lt;/span&gt;&lt;span class="s"&gt;"stopPlay('A4');"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Play long sample
&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I'm using the &lt;code&gt;mousedown&lt;/code&gt; and &lt;code&gt;mouseup&lt;/code&gt; events to decouple the button press and button unpress. You'll also notice that I'm using &lt;code&gt;touchstart&lt;/code&gt; and &lt;code&gt;touchend&lt;/code&gt;, which fixes the issue that touch screen devices don't have a mousedown or mouseup event. To stop the event's propagation, I'm using the &lt;a href="https://alpinejs.dev/directives/on#stop"&gt;&lt;code&gt;stop&lt;/code&gt; modifier&lt;/a&gt; on all events, and to prevent the default behavior I'm also using the &lt;a href="https://alpinejs.dev/directives/on#prevent"&gt;&lt;code&gt;prevent&lt;/code&gt; modifier&lt;/a&gt; on the touch events. This fixes an issue where the event would otherwise be triggered twice on devices with a mouse.&lt;/p&gt;




&lt;p&gt;That's it! Hopefully this short introduction was enough to show you how easy it can be to trigger sounds in the browser and start having fun with that in your own projects! ✨ 🔊&lt;/p&gt;

&lt;p&gt;For the sake of brevity, I kept the part involving Alpine.js very short and sweet in this post. In a real-world scenario, you'll likely want to make use of &lt;code&gt;x-data&lt;/code&gt; to do things like keep track of the notes/sounds being played:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;x-data=&lt;/span&gt;&lt;span class="s"&gt;"{ currentNote: 'A4' }"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;click=&lt;/span&gt;&lt;span class="s"&gt;"play(currentNote);"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Play note&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  ...
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>music</category>
    </item>
  </channel>
</rss>
