Luca Soldi

  • Home
  • Blog
  • Projects
  • Github
  • About me

How to get duration of Wav file in React Native

    Home Android How to get duration of Wav file in React Native
    NextPrevious

    How to get duration of Wav file in React Native

    By admin | Android, iOS | 0 comment | 3 January, 2019 | 0

    I’m writing an audio app in React Native that plays WAV file from local storage.

    I was looking for a simple and little module to get the duration of WAV files, but I didn’t find nothing that I really like, so I started to study how the WAV file header is formatted.

    (image from Soundfile)

     

    How to read file in React Native to Buffer

    In React Native we cannot use the NodeJS standard “fs” standard module, so I installed react-native-fs (https://github.com/itinance/react-native-fs).

    With this library we can open the first 40 byte of the WAV file and serialize its data in Buffer to be able to get the fields we need to determine how long is the song.

    We need:

    1. Chunk Size (from byte 4 to 8)
    2. Sample Rate (from byte 24 to 28)
    3. Num Channels (from byte 22 to 24)
    4. Bit per Sample (from byte 34 to 38)

    and the duration is simple as ->

    Chunk Size / (Sample Rate * Num Channels * (Bit per Sample / 8))

     

    React Native has not support for Buffer natively, so you have to install it (npm install –save buffer) and import it as it shown below

    global.Buffer = global.Buffer || require(‘buffer’).Buffer;

     

    export const getWavDuration = (file) => {
        let buffer = new Buffer(40);
        RNFS.read(RNFS.MainBundlePath + "/" + file, 40, 0, 'ascii')
            .then(result => {
                //DATA               BYTES
                ////////////////////////////////////
                //chunk_size         5-6-7-8
                //sample_rate        25-26-27-28
                //num_channel        23-24
                //bit_per_sample     35-36-37-38
    
                buffer = Buffer.from(result, 'ascii').toString('hex').match(/.{1,2}/g);
                const chunk_size = parseInt(buffer.slice(4, 8).reverse().join(""), 16);
                const sample_rate = parseInt(buffer.slice(24, 28).reverse().join(""), 16);
                const num_channel = parseInt(buffer.slice(22, 24).reverse().join(""), 16);
                const bit_per_sample = parseInt(buffer.slice(34, 38).reverse().join(""), 16);
    
                const duration = parseInt(chunk_size / (sample_rate * num_channel * (bit_per_sample / 8)));
            });
    };

    The buffer array is reversed because the data chunks are little endian 🙂

    Credits

    The icons in the featured image are under copyright.

    Icons made by Freepik from Flaticon is licensed by CC 3.0

    No tags.

    admin

    More posts by admin

    Related Post

    • REST API Best Security Practices [PRANK]

      By admin | 0 comment

      I love to see how internet is a secure place nowadays 😀 Apps and WebApps everywhere that use very reliable REST APIs. This time I found a perfect example to follow if you want toRead more

    • Download ItaloLive media content

      By admin | 0 comment

      Boring long trip on an Italian high-speed train? It’s time to update your home media library 😀 Connecting to the train Wi-Fi you can access to an internal nginx server that serves see some mediaRead more

    • How to get artworks for your music library

      By admin | 0 comment

      I’m working on a script to automate tagging and ordering my music library. I love artworks so I had to find a way to take them from the net. I found 2 ways to getRead more

    • My Desk Led Light (APA102 + Arduino Nano + HM10)

      By admin | 2 comments

      I recently changed home and I wanted to create something different in my new work desk. Months ago I created ambilight for my TV with some meters of APA102 led and ambilight software and IRead more

    • [POC] How to travel for free with Italian public transports

      By admin | 0 comment

      Hey remember! It’s a proof of concept 🙂 Before publishing this article I verified that the company not use this technology anymore. Some years ago I did some experiments with my rechargeable card used inRead more

    • Unbrick Proxmark3 with a Raspberry Pi and OpenOCD

      By admin | 4 comments

      Months ago I bricked my Proxmark3 while I was experimenting with the creation of a new antenna for a tag. The leds blinked in a strange way at boot and my mac not recognized anymore theRead more

    Leave a Comment

    Cancel reply

    Your email address will not be published. Required fields are marked *

    NextPrevious

    Categories

    • Android
    • Arduino
    • Bluetooth
    • iOS
    • Random Stuff
    • Raspberry Pi
    • RFID
    • Scraping

    Archives

    • December 2020
    • June 2019
    • January 2019
    • October 2018
    • September 2018
    • May 2017
    • January 2017
    Copyright 2017 Luca Soldi | All Rights Reserved
    • About me
    • Blog
    • Home
    • Projects

    Luca Soldi