<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Michal Sek on Medium]]></title>
        <description><![CDATA[Stories by Michal Sek on Medium]]></description>
        <link>https://medium.com/@michal.sek?source=rss-f79f748c7e06------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*y-dch2Wkj5H0mWST</url>
            <title>Stories by Michal Sek on Medium</title>
            <link>https://medium.com/@michal.sek?source=rss-f79f748c7e06------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 11:02:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@michal.sek/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introducing React Native Audio API]]></title>
            <link>https://medium.com/swmansion/hello-react-native-audio-api-bb0f10347211?source=rss-f79f748c7e06------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb0f10347211</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[web-audio-api]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[music]]></category>
            <category><![CDATA[audio]]></category>
            <dc:creator><![CDATA[Michal Sek]]></dc:creator>
            <pubDate>Tue, 28 Jan 2025 15:23:00 GMT</pubDate>
            <atom:updated>2025-01-28T15:36:34.585Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ShVTbESZ8kq-kmmlp2C-9w.png" /></figure><p>Some time ago, we released react-native-audio-api. But what is it all about?</p><h3>What is react-native-audio-api?</h3><p><strong><em>react-native-audio-api</em> aims to bring the same tools available in browsers via the Web Audio API to React Native, offering precise, high-performant audio control and playback.</strong></p><p>By aligning with the Web Audio specification, we’re creating a single API that works seamlessly across native iOS, Android, browsers, and even standalone desktop applications. The React Native ecosystem currently lacks a high-performance API for creating audio, adding effects, or controlling basic parameters like volume for each audio separately — and we’re here to bridge that gap!</p><h3>Our end goal</h3><p>The Web Audio API can feel a bit counterintuitive at first, especially for developers new to digital sound processing. However, there are plenty of great libraries built on top of it, like <a href="https://github.com/Tonejs/Tone.js">tone.js</a> and <a href="https://github.com/goldfire/howler.js">howler.js</a>, which offer user-friendly abstractions over Web Audio. To find more examples check out <a href="https://github.com/notthetup/awesome-webaudio">the Awesome WebAudio list</a>.</p><p>By creating <em>react-native-audio-api</em> that is compatible with Web Audio, we’ll be able to bring those libraries to react-native.</p><h3>The Web Audio API in detail</h3><p>So far, I’ve highlighted what Web Audio can do and mentioned keywords like high performance — but how does it actually work in practice? Let me quote the description from <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Basic_concepts_behind_Web_Audio_API">MDN web docs</a>:</p><p><em>“The Web Audio API involves handling audio operations inside an audio context, and has been designed to allow modular routing. Basic audio operations are performed with audio nodes, which are linked together to form an audio routing graph. Several sources — with different types of channel layout — are supported even within a single context. This modular design provides the flexibility to create complex audio functions with dynamic effects.”</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fshvXhhRq-Trbu1GQlRO9Q.png" /></figure><p>Simply put, in the Audio API, the signal (our audio data) flows in one direction — from the source (e.g., a file or signal generator), through optional processing elements, and finally to the destination (e.g., device speakers). These components are all called audio nodes.</p><h3>Basic concepts behind the Audio API</h3><p>As mentioned above, we can distinguish three types of audio nodes:</p><ul><li><em>Audio Sources</em> — Source nodes are usually the starting point in the graph, and as the name suggests, they provide audio input into the graph. This input can be generated mathematically (<em>OscillatorNode</em>), loaded from recorded data (<em>AudioBufferSourceNode</em>), or obtained from a stream (<em>MediaStreamAudioSourceNode</em>).</li><li><em>Processing Nodes</em> — Processing nodes provide simple audio effects or modifiers. The most common examples include changing the volume of the source (<em>GainNode</em>) or cutting off specific frequencies (<em>BiquadFilterNode</em>).</li><li><em>Output Nodes</em> — The last type of nodes are output nodes, which, in most cases, connect directly to the device hardware, such as speakers or headphones.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N4pOBtoMMm28bgqnSk2YOQ.png" /></figure><p>The Audio API doesn’t impose any strict limits on how many source or processing nodes you can create and use simultaneously. It all depends on the device — some processors can handle over <strong>1000 sounds at once without stuttering</strong>.</p><p>However, there is one limitation with audio source nodes that may not be immediately obvious but is important when using the Web or React Native Audio API. <strong>Source nodes are one-time use only — once you call stop on a source node (or it stops automatically), you’ll need to create a new one of the same type.</strong></p><p>Now that I’ve covered the basics of what Web Audio is and how it works, let’s take a look at how it plays out in a simple use case.</p><h3>Playback using the React Native Audio API</h3><p>In this section, I’ll walk through a basic example of using the API. I’ll load an audio file into the application and play it as soon as it’s successfully downloaded. Let’s start with the full code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e23c049ecc80dab04dba9d2c63f40f17/href">https://medium.com/media/e23c049ecc80dab04dba9d2c63f40f17/href</a></iframe><p>As you can see, all the important code is in <em>handlePlay</em> callback. Let’s break it down line by line:</p><ol><li>To work with audio, we have to create an <em>AudioContext</em> object. It’s our main interface for interacting with both the web and native audio API. The audio context controls the lifetime of audio nodes and is also responsible for communicating with the device’s hardware interfaces.</li><li>In the next line, we’re using <em>expo-file-system</em> to download the audio file to the device’s local file system. This stems from a limitation of the current version of the React Native Audio API — audio files must be available on the device’s local system. In future versions, we plan to support using fetch or similar APIs to handle files entirely in memory, which is something that would be necessary in the standard browser Web Audio API.</li><li>The local file system path to the audio asset is then passed to the <em>decodeAudioDataSource</em> method of the audio context. This function loads the file into memory, decodes it if necessary and returns an instance of an <em>AudioBuffer</em> object. The <em>AudioBuffer</em> contains the raw audio data, converted to the device-specific parameters (like sample rate), and is ready to be played.</li><li>In the next line, we create our first source node. The <em>AudioBufferSourceNode</em>, as the name suggests, is a type of source node used to play audio from the audio buffers obtained in the previous step.</li><li>In the final lines, we connect the <em>playerNode</em> to another property of the audio context called <em>destination</em>. This is an output node that’s directly connected to the device’s hardware.</li><li>Lastly, we instruct the <em>playerNode</em> to start playing at the current time.</li></ol><p>As you can see, it’s a bit more complex than simply using an <em>&lt;audio&gt;</em> tag or <em>expo-audio</em>, but by having access to the raw audio data, we can manipulate the sound in any way we want. So, let’s take a look at a more complex example to explore the possibilities.</p><h3>Cross-fading audio</h3><p>Let’s imagine we’re building an audio player application and want it to smoothly transition from one song to another. We want the transition to take exactly one second, so we need to address two main points:</p><ul><li>Know the start time and duration of the first song to ensure we can precisely start the second song and know when to begin the transition.</li><li>Apply a volume-change effect to each song. This will allow the first song to fade out before it ends, while the second song fades in.</li></ul><p>The first point is easy to handle, as we saw in the previous example. The AudioContext has a <em>currentTime</em> property, which allows us to precisely start the songs or the transition. The <em>AudioBuffer </em>interface also has a <em>duration</em> property, which provides the remaining information to calculate the necessary timestamps. Now, let’s explore how we can address the second point.</p><h3>The GainNode and AudioParams</h3><p>To achieve the fade-in and fade-out effects, we’ll use processing nodes, specifically the <em>GainNode</em>, which I mentioned earlier in the article. The GainNode is one of the simplest nodes in the Audio API. It takes the audio data from its input and multiplies it by a scalar — the gain (or volume, if you prefer), which is an audio parameter.</p><p>The <em>AudioParam</em> is an interface that controls various aspects of audio nodes. In the case of the <em>GainNode</em>, the <em>AudioParam</em> is the gain (or volume). It allows us to control the value in several ways:</p><ul><li>by setting the value directly: <em>gain.value = 1</em></li><li>by specifying the value at a given time: <em>setValueAtTime(value, time)</em></li><li>by modifying the value over time, either using linear (<em>linearRampToValueAtTime(value, endTime)</em>) or exponential (<em>exponentialRampToValueAtTime(value, endTime)</em>) transition.</li></ul><h3>The solution</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vH5PyDN78byUp9ED9gL0Hw.png" /></figure><p>Let’s put everything together. We’ll load the files just like in the previous example, use the <em>audioContext.currentTime</em> property to calculate the start and stop times of the songs, and use the GainNode to create the fade-in and fade-out effects.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33968e5cfab361432865113f4b4c9120/href">https://medium.com/media/33968e5cfab361432865113f4b4c9120/href</a></iframe><p>Each song has its own <em>AudioBufferSourceNode</em> and <em>GainNode</em>, with both GainNodes connected to the audio context’s destination node. We schedule the second song to start one second before the first one finishes, based on its start time and duration. To create the fade in/out effects, we use the <em>linearRampToValueAtTime </em>method of the GainNode’s param.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FShzJ7VciJb4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fshorts%2FShzJ7VciJb4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FShzJ7VciJb4%2Fhq2.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7db38c5add3d855dc407d09b4cf8b98f/href">https://medium.com/media/7db38c5add3d855dc407d09b4cf8b98f/href</a></iframe><h3>A note about equal power cross-fade</h3><p>Using linear interpolation is a simple approach, but it can lead to a noticeable volume drop when transitioning between songs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DwhZ_9831jXpK8NOLAdL7g.png" /></figure><p>In a real-world scenario, we’d want to use an “equal power cross-fade,” which minimizes this effect, but that’s a topic for another article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J2p3tQLIbvRNB2ZFQvGUdg.png" /></figure><h3>Takeaways</h3><p>The React Native Audio API unlocks a whole new range of possibilities for working with audio in mobile and web apps. This article only scratches the surface of what’s possible. To learn more, check out our <a href="https://github.com/software-mansion-labs/react-native-audio-api">repository</a> and <a href="https://software-mansion-labs.github.io/react-native-audio-api/">documentation</a>, or dive into the full <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API">MDN docs on the Web Audio API</a>.</p><p>If you would like to see more examples using Web Audio API check out:</p><ul><li><a href="https://webaudioapi.com/samples/">Web Audio API samples</a></li><li><a href="https://learningsynths.ableton.com/">Learning Synths series</a> by Ableton</li></ul><p>Stay tuned for upcoming React Native Audio API updates from us!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb0f10347211" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swmansion/hello-react-native-audio-api-bb0f10347211">Introducing React Native Audio API</a> was originally published in <a href="https://medium.com/swmansion">Software Mansion</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>