├── .gitignore ├── .npmignore ├── index.js ├── package.json ├── LICENSE.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | test 7 | test.js 8 | demo/ 9 | .npmignore 10 | LICENSE.md -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = createAudioContext 2 | function createAudioContext (desiredSampleRate) { 3 | var AudioCtor = window.AudioContext || window.webkitAudioContext 4 | 5 | desiredSampleRate = typeof desiredSampleRate === 'number' 6 | ? desiredSampleRate 7 | : 44100 8 | var context = new AudioCtor() 9 | 10 | // Check if hack is necessary. Only occurs in iOS6+ devices 11 | // and only when you first boot the iPhone, or play a audio/video 12 | // with a different sample rate 13 | if (/(iPhone|iPad)/i.test(navigator.userAgent) && 14 | context.sampleRate !== desiredSampleRate) { 15 | var buffer = context.createBuffer(1, 1, desiredSampleRate) 16 | var dummy = context.createBufferSource() 17 | dummy.buffer = buffer 18 | dummy.connect(context.destination) 19 | dummy.start(0) 20 | dummy.disconnect() 21 | 22 | context.close() // dispose old context 23 | context = new AudioCtor() 24 | } 25 | 26 | return context 27 | } 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ios-safe-audio-context", 3 | "version": "1.0.1", 4 | "description": "create a WebAudio context that works in iOS and everywhere else", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "author": { 8 | "name": "Matt DesLauriers", 9 | "email": "dave.des@gmail.com", 10 | "url": "https://github.com/mattdesl" 11 | }, 12 | "dependencies": {}, 13 | "devDependencies": {}, 14 | "scripts": { 15 | "test": "node test.js" 16 | }, 17 | "keywords": [ 18 | "web", 19 | "audio", 20 | "context", 21 | "hack", 22 | "ios", 23 | "sampleRate", 24 | "bug", 25 | "44100", 26 | "fix", 27 | "workaround", 28 | "issue", 29 | "webaudio", 30 | "iphone", 31 | "ipad" 32 | ], 33 | "repository": { 34 | "type": "git", 35 | "url": "git://github.com/Jam3/ios-safe-audio-context.git" 36 | }, 37 | "homepage": "https://github.com/Jam3/ios-safe-audio-context", 38 | "bugs": { 39 | "url": "https://github.com/Jam3/ios-safe-audio-context/issues" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2015 Jam3 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ios-safe-audio-context 2 | 3 | [![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges) 4 | 5 | Creates a `AudioContext` that works as expected in desktop and mobile, particularly across iOS devices. 6 | 7 | ### Motivation 8 | 9 | There is a [bug in iOS](http://stackoverflow.com/questions/26336040/how-to-fix-changing-sample-rate-bug) where the AudioContext `sampleRate` is sometimes not what you would expect, and as a result, all WebAudio plays with heavy distortion. This occurs when you play an audio/video element with a different sample rate, or when you first boot up Safari (tested on iOS9.2, iPhone5S, no headphones). 10 | 11 | To get around this, we try to detect a broken state, and if so, play a dummy buffer before returning a *new* AudioContext which should have the desired sample rate (default 44100). 12 | 13 | ## Example 14 | 15 | On iOS, this function must be called from user gesture event, such as `touchend`. 16 | 17 | ```js 18 | const createAudioContext = require('ios-safe-audio-context') 19 | 20 | clickToPlay.addEventListener('touchend', () => { 21 | const audioContext = createAudioContext() 22 | 23 | // now you can use this context for playback 24 | }) 25 | ``` 26 | 27 | ## Usage 28 | 29 | [![NPM](https://nodei.co/npm/ios-safe-audio-context.png)](https://www.npmjs.com/package/ios-safe-audio-context) 30 | 31 | #### `context = createAudioContext([desiredSampleRate])` 32 | 33 | Returns a new AudioContext, only applying the hack if we detect a broken state (iOS). `desiredSampleRate` defaults to 44100. 34 | 35 | ## License 36 | 37 | MIT, see [LICENSE.md](http://github.com/Jam3/ios-safe-audio-context/blob/master/LICENSE.md) for details. 38 | --------------------------------------------------------------------------------