Skip to content

Native iOS function to determine the synchronisation offset between two substantially similar audio files

License

Notifications You must be signed in to change notification settings

piratestudios/react-native-audio-sync

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-native-audio-sync

Native iOS function to determine the synchronisation offset between two substantially similar audio files. Android not implemented (yet).

The underlying Swift code leverages Apple's Accelerate framework, in particular the vDSP_conv function, to perform a cross-correlation in order to determine the time difference of the first input relative to the second.

Installation

npm install react-native-audio-sync

Usage

Outputs the time difference of the first input relative to the second - a negative output indicates that audioFile1 is lagging behind audioFile2.

import {
  type SyncOffsetResult,
  calculateSyncOffset,
} from 'react-native-audio-sync';

calculateSyncOffset(
  `/path/to/audio/files/audioFile1.wav`,
  '/path/to/audio/files/audioFile2.wav'
)
  .then(({syncOffset}: SyncOffsetResult) => {
    console.log(`syncOffset: ${syncOffset}`);
  });

Example app

The example app includes five WAV files in its app bundle, designed to demonstrate the synchronisation code. They are each composed of two bars of silence in 4/4 at 120bpm but with the same hi-hat sample occurring at a different interval in each.

example-audio-files.png

From this, we should be able to make a few predictions:

  • the time offset between audioFile1 and audioFile2 will be -1s, as in 4/4 at 120bpm each beat lasts a second, and we can see from the image above that audioFile2 starts exactly one beat after audioFile1;
  • the difference between audioFile1 and audioFile3 should be -1.5s, as audioFile3 starts a beat and a half after audioFile1;
  • subdividing again, audioFile1 and audioFile4 should have a time offset of -1.75s, and finally;
  • audioFile5, starting a full bar after audioFile1, will have a time offset of -4s.

For good measure, we can therefore also easily predict that audioFile4 starts 0.25s after audioFile3.

To confirm these predictions, running the example app in the iOS simulator should give the following result: example-ios-simulator.png

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT License

Copyright (c) 2024 Daniel Kay

Contains Swift code which was ported from an original objC implementation Copyright (c) 2015 Patrik Vaberer https://github.com/Vaberer/audio_time_shifting/


Made with create-react-native-library

About

Native iOS function to determine the synchronisation offset between two substantially similar audio files

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 33.8%
  • TypeScript 18.9%
  • Kotlin 13.6%
  • Ruby 12.0%
  • Objective-C 8.9%
  • JavaScript 8.0%
  • Other 4.8%