├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jscsrc ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── audiobuffer-slice.js ├── bower.json ├── example ├── bower.json ├── bower_components │ └── audio-oscilloscope │ │ ├── .bower.json │ │ ├── CHANGELOG.md │ │ ├── LICENSE.md │ │ ├── README.md │ │ ├── audio-oscilloscope.js │ │ ├── bower.json │ │ ├── example │ │ ├── index.html │ │ ├── scripts │ │ │ └── main.js │ │ └── styles │ │ │ └── style.css │ │ └── package.json ├── index.html ├── media │ └── ethos-final-hope.mp3 ├── scripts │ └── main.js └── styles │ └── style.css └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | [*] 7 | # use line feed 8 | end_of_line = lf 9 | 10 | # ensure file ends with a newline when saving 11 | insert_final_newline = true 12 | 13 | # soft tabs 14 | indent_style = space 15 | 16 | # number of columns used for each indentation level 17 | indent_size = 2 18 | 19 | # remove any whitespace characters preceding newline characters 20 | trim_trailing_whitespace = true 21 | 22 | # character set 23 | charset = utf-8 24 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Convert line endings to LF (Line Feed) 2 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | //this will affect all the git repos 2 | git config --global core.excludesfile ~/.gitignore 3 | 4 | 5 | //update files since .ignore won't if already tracked 6 | git rm --cached 7 | 8 | # Compiled source # 9 | ################### 10 | *.com 11 | *.class 12 | *.dll 13 | *.exe 14 | *.o 15 | *.so 16 | 17 | # Packages # 18 | ############ 19 | # it's better to unpack these files and commit the raw source 20 | # git has its own built in compression methods 21 | *.7z 22 | *.dmg 23 | *.gz 24 | *.iso 25 | *.jar 26 | *.rar 27 | *.tar 28 | *.zip 29 | 30 | # Logs and databases # 31 | ###################### 32 | *.log 33 | *.sql 34 | *.sqlite 35 | 36 | # OS generated files # 37 | ###################### 38 | .DS_Store 39 | .DS_Store? 40 | ._* 41 | .Spotlight-V100 42 | .Trashes 43 | # Icon? 44 | ehthumbs.db 45 | Thumbs.db 46 | .cache 47 | .project 48 | .settings 49 | .tmproj 50 | *.esproj 51 | nbproject 52 | 53 | # Numerous always-ignore extensions # 54 | ##################################### 55 | *.diff 56 | *.err 57 | *.orig 58 | *.rej 59 | *.swn 60 | *.swo 61 | *.swp 62 | *.vi 63 | *~ 64 | *.sass-cache 65 | *.grunt 66 | *.tmp 67 | 68 | # Dreamweaver added files # 69 | ########################### 70 | _notes 71 | dwsync.xml 72 | 73 | # Komodo # 74 | ########################### 75 | *.komodoproject 76 | .komodotools 77 | 78 | # Node # 79 | ##################### 80 | node_modules 81 | 82 | # Bower # 83 | ##################### 84 | bower_components 85 | !example/bower_components 86 | 87 | # Folders to ignore # 88 | ##################### 89 | .hg 90 | .svn 91 | .CVS 92 | intermediate 93 | publish 94 | .idea 95 | .graphics 96 | _test 97 | _archive 98 | uploads 99 | tmp 100 | 101 | # Vim files to ignore # 102 | ####################### 103 | .VimballRecord 104 | .netrwhist 105 | -------------------------------------------------------------------------------- /.jscsrc: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "google", 3 | "disallowSpacesInAnonymousFunctionExpression": null, 4 | "excludeFiles": ["node_modules/**"] 5 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | This project adheres to [Semantic Versioning](http://semver.org/). 4 | 5 | ## [0.0.6] - 2017-07-21 6 | ### Changed 7 | - Use `audioBufferSlice` instead of `AudioBufferSlice`. 8 | 9 | ## [0.0.3] - 2015-11-25 10 | ### Changed 11 | - Use only one AudioContext instance instead of creating a new one each time. 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT license 2 | 3 | Copyright (C) 2015 Miguel Mota 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 | of the Software, and to permit persons to whom the Software is furnished to do 10 | so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # audiobuffer-slice 2 | 3 | Slice out a portion of an `AudioBuffer`. 4 | 5 | [![NPM](https://nodei.co/npm/audiobuffer-slice.png)](https://nodei.co/npm/audiobuffer-slice) 6 | 7 | # Install 8 | 9 | ```bash 10 | npm install audiobuffer-slice 11 | ``` 12 | 13 | # Usage 14 | 15 | See [example](https://github.com/miguelmota/audiobuffer-slice/tree/master/example) directory for full example. [Live example](https://lab.miguelmota.com/audiobuffer-slice/example) 16 | 17 | ```javascript 18 | var audioBufferSlice = require('audiobuffer-slice'); 19 | 20 | // audioBuffer slice from 10s to 15s; a 5 second (5000ms) slice. 21 | audioBufferSlice(audioBuffer, 10000, 15000, function(error, slicedAudioBuffer) { 22 | if (error) { 23 | console.error(error); 24 | } else { 25 | source.buffer = slicedAudioBuffer; 26 | } 27 | }); 28 | ``` 29 | 30 | # License 31 | 32 | MIT 33 | -------------------------------------------------------------------------------- /audiobuffer-slice.js: -------------------------------------------------------------------------------- 1 | (function(root) { 2 | 'use strict'; 3 | 4 | var audioContext = new (window.AudioContext || window.webkitAudioContext); 5 | 6 | function audioBufferSlice(buffer, begin, end, callback) { 7 | if (!(this instanceof audioBufferSlice)) { 8 | return new audioBufferSlice(buffer, begin, end, callback); 9 | } 10 | 11 | var error = null; 12 | 13 | var duration = buffer.duration; 14 | var channels = buffer.numberOfChannels; 15 | var rate = buffer.sampleRate; 16 | 17 | if (typeof end === 'function') { 18 | callback = end; 19 | end = duration; 20 | } 21 | 22 | // milliseconds to seconds 23 | begin = begin/1000; 24 | end = end/1000; 25 | 26 | if (begin < 0) { 27 | error = new RangeError('begin time must be greater than 0'); 28 | } 29 | 30 | if (end > duration) { 31 | error = new RangeError('end time must be less than or equal to ' + duration); 32 | } 33 | 34 | if (typeof callback !== 'function') { 35 | error = new TypeError('callback must be a function'); 36 | } 37 | 38 | var startOffset = rate * begin; 39 | var endOffset = rate * end; 40 | var frameCount = endOffset - startOffset; 41 | var newArrayBuffer; 42 | 43 | try { 44 | newArrayBuffer = audioContext.createBuffer(channels, endOffset - startOffset, rate); 45 | var anotherArray = new Float32Array(frameCount); 46 | var offset = 0; 47 | 48 | for (var channel = 0; channel < channels; channel++) { 49 | buffer.copyFromChannel(anotherArray, channel, startOffset); 50 | newArrayBuffer.copyToChannel(anotherArray, channel, offset); 51 | } 52 | } catch(e) { 53 | error = e; 54 | } 55 | 56 | callback(error, newArrayBuffer); 57 | } 58 | 59 | if (typeof exports !== 'undefined') { 60 | if (typeof module !== 'undefined' && module.exports) { 61 | exports = module.exports = audioBufferSlice; 62 | } 63 | exports.audioBufferSlice = audioBufferSlice; 64 | } else if (typeof define === 'function' && define.amd) { 65 | define([], function() { 66 | return audioBufferSlice; 67 | }); 68 | } else { 69 | root.audioBufferSlice = audioBufferSlice; 70 | } 71 | })(this); 72 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "audiobuffer-slice", 3 | "main": "audiobuffer-slice.js", 4 | "version": "0.0.5", 5 | "homepage": "https://github.com/miguelmota/audiobuffer-slice", 6 | "authors": [ 7 | "Miguel Mota " 8 | ], 9 | "description": "Slice out a portion of an AudioBuffer.", 10 | "keywords": [ 11 | "audio", 12 | "audiobuffer", 13 | "slice" 14 | ], 15 | "ignore": [ 16 | "**/.*", 17 | "node_modules", 18 | "bower_components", 19 | "test", 20 | "tests" 21 | ], 22 | "license": "MIT" 23 | } 24 | -------------------------------------------------------------------------------- /example/bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "0.0.0", 4 | "homepage": "https://github.com/miguelmota/audiobuffer-slice", 5 | "authors": [ 6 | "Miguel Mota " 7 | ], 8 | "license": "MIT", 9 | "ignore": [ 10 | "**/.*", 11 | "node_modules", 12 | "bower_components", 13 | "test", 14 | "tests" 15 | ], 16 | "dependencies": { 17 | "audio-oscilloscope": "*" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/.bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "audio-oscilloscope", 3 | "main": "audio-oscilloscope.js", 4 | "homepage": "https://github.com/miguelmota/audio-oscilloscope", 5 | "authors": [ 6 | "Miguel Mota " 7 | ], 8 | "description": "Audio oscilloscope in canvas.", 9 | "keywords": [ 10 | "audio", 11 | "sound", 12 | "canvas", 13 | "oscilloscope", 14 | "wave" 15 | ], 16 | "ignore": [ 17 | "**/.*", 18 | "node_modules", 19 | "bower_components", 20 | "test", 21 | "tests" 22 | ], 23 | "license": "MIT", 24 | "_release": "16d604344c", 25 | "_resolution": { 26 | "type": "branch", 27 | "branch": "master", 28 | "commit": "16d604344cdd139b88a42149fab9f9380f9f65d9" 29 | }, 30 | "_source": "git://github.com/miguelmota/audio-oscilloscope.git", 31 | "_target": "*", 32 | "_originalSource": "audio-oscilloscope", 33 | "_direct": true 34 | } -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | This project adheres to [Semantic Versioning](http://semver.org/). 4 | -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT license 2 | 3 | Copyright (C) 2015 Miguel Mota 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 | of the Software, and to permit persons to whom the Software is furnished to do 10 | so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/README.md: -------------------------------------------------------------------------------- 1 | # audio-oscilloscope 2 | 3 | Audio [oscilloscope](http://en.wikipedia.org/wiki/Oscilloscope) in canvas. 4 | 5 | [![NPM](https://nodei.co/npm/audio-oscilloscope.png)](https://nodei.co/npm/audio-oscilloscope) 6 | 7 | # Demo 8 | 9 | **[http://lab.moogs.io/audio-oscilloscope](http://lab.moogs.io/audio-oscilloscope)** 10 | 11 | # Install 12 | 13 | ```bash 14 | npm install audio-oscilloscope 15 | ``` 16 | 17 | ```bash 18 | bower install audio-oscilloscope 19 | ``` 20 | 21 | # Usage 22 | 23 | ```javascript 24 | var oscilloscope = AudioOscilloscope(document.getElementById('canvas'), { 25 | canvas: { 26 | width: function() { 27 | return window.innerWidth; 28 | }, 29 | height: 400 30 | }, 31 | canvasContext: { 32 | lineWidth: 2, 33 | fillStyle: 'rgb(0,0,0)', 34 | strokeStyle: 'green' 35 | } 36 | }); 37 | 38 | oscilloscope.draw(); 39 | 40 | navigator.getUserMedia({ 41 | audio: true 42 | }, function(stream) { 43 | var audioContext = new AudioContext(); 44 | var source = audioContext.createMediaStreamSource(stream); 45 | oscilloscope.addSource(source); 46 | }, function(error) { 47 | console.error(error); 48 | }); 49 | ``` 50 | 51 | # License 52 | 53 | MIT 54 | -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/audio-oscilloscope.js: -------------------------------------------------------------------------------- 1 | (function(root) { 2 | 'use strict'; 3 | 4 | function _isFunction(v) { 5 | return typeof v === 'function'; 6 | } 7 | 8 | function _result(v) { 9 | return _isFunction(v) ? v() : v; 10 | } 11 | 12 | function AudioOscilloscope(canvas, options) { 13 | if (!(this instanceof AudioOscilloscope)) { 14 | return new AudioOscilloscope(canvas, options); 15 | } 16 | options = options || {}; 17 | var canvasOptions = options.canvas || {}; 18 | var canvasContextOptions = options.canvasContext || {}; 19 | this.analyser = null; 20 | this.bufferLength = 0; 21 | this.dataArray = []; 22 | this.canvas = canvas; 23 | this.width = _result(canvasOptions.width) || this.canvas.width; 24 | this.height = _result(canvasOptions.height) || this.canvas.height; 25 | this.canvas.width = this.width; 26 | this.canvas.height = this.height; 27 | this.canvasContext = this.canvas.getContext('2d'); 28 | this.canvasContext.fillStyle = _result(canvasContextOptions.fillStyle) || 'rgb(255, 255, 255)'; 29 | this.canvasContext.strokeStyle = _result(canvasContextOptions.strokeStyle) || 'rgb(0, 0, 0)'; 30 | this.canvasContext.lineWidth = _result(canvasContextOptions.lineWidth) || 1; 31 | this.onDrawFrame = (_isFunction(options.onDrawFrame) ? options.onDrawFrame : function(){}); 32 | 33 | var self = this; 34 | window.onresize = function() { 35 | self.width = _result(canvasOptions.width) || self.canvas.width; 36 | self.height = _result(canvasOptions.height) || self.canvas.height; 37 | self.canvas.width = self.width; 38 | self.canvas.height = self.height; 39 | }; 40 | } 41 | 42 | AudioOscilloscope.prototype.addSource = function(streamSource) { 43 | this.streamSource = streamSource; 44 | this.audioContext = this.streamSource.context; 45 | this.analyser = this.audioContext.createAnalyser(); 46 | this.analyser.fftSize = 2048; 47 | this.bufferLength = this.analyser.frequencyBinCount; 48 | this.source = this.audioContext.createBufferSource(); 49 | this.dataArray = new Uint8Array(this.bufferLength); 50 | this.analyser.getByteTimeDomainData(this.dataArray); 51 | this.streamSource.connect(this.analyser); 52 | }; 53 | 54 | AudioOscilloscope.prototype.draw = function draw() { 55 | var analyser = this.analyser; 56 | var dataArray = this.dataArray; 57 | var bufferLength = this.bufferLength; 58 | var ctx = this.canvasContext; 59 | var w = this.width; 60 | var h = this.height; 61 | 62 | if (analyser) { 63 | analyser.getByteTimeDomainData(dataArray); 64 | } 65 | 66 | ctx.fillRect(0, 0, w, h); 67 | 68 | ctx.beginPath(); 69 | 70 | var sliceWidth = w * 1.0 / bufferLength; 71 | var x = 0; 72 | 73 | if (!bufferLength) { 74 | ctx.moveTo(0, this.height/2); 75 | } 76 | 77 | for (var i = 0; i < bufferLength; i++) { 78 | 79 | var v = dataArray[i] / 128.0; 80 | var y = v * (h/2); 81 | 82 | if (i === 0) { 83 | ctx.moveTo(x, y); 84 | } else { 85 | ctx.lineTo(x, y); 86 | } 87 | 88 | x += sliceWidth; 89 | } 90 | 91 | ctx.lineTo(w, h/2); 92 | ctx.stroke(); 93 | 94 | this.onDrawFrame(this); 95 | requestAnimationFrame(draw.bind(this)); 96 | }; 97 | 98 | AudioOscilloscope.prototype.on = function(type, callback) { 99 | if (type === 'drawFrame') { 100 | this.onDrawFrame = callback; 101 | } 102 | }; 103 | 104 | if (typeof exports !== 'undefined') { 105 | if (typeof module !== 'undefined' && module.exports) { 106 | exports = module.exports = AudioOscilloscope; 107 | } 108 | exports.AudioOscilloscope = AudioOscilloscope; 109 | } else if (typeof define === 'function' && define.amd) { 110 | define([], function() { 111 | return AudioOscilloscope; 112 | }); 113 | } else { 114 | root.AudioOscilloscope = AudioOscilloscope; 115 | } 116 | })(this); 117 | -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "audio-oscilloscope", 3 | "main": "audio-oscilloscope.js", 4 | "version": "0.0.1", 5 | "homepage": "https://github.com/miguelmota/audio-oscilloscope", 6 | "authors": [ 7 | "Miguel Mota " 8 | ], 9 | "description": "Audio oscilloscope in canvas.", 10 | "keywords": [ 11 | "audio", 12 | "sound", 13 | "canvas", 14 | "oscilloscope", 15 | "wave" 16 | ], 17 | "ignore": [ 18 | "**/.*", 19 | "node_modules", 20 | "bower_components", 21 | "test", 22 | "tests" 23 | ], 24 | "license": "MIT" 25 | } 26 | -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Audio oscilloscope 6 | 7 | 8 | 9 |

Audio oscilloscope

10 | 11 | 12 | 13 | 14 | 15 | 16 | Fork me on GitHub 17 | 18 | 19 | -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/example/scripts/main.js: -------------------------------------------------------------------------------- 1 | window.AudioContext = window.AudioContext || window.webkitAudioContext; 2 | navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia; 3 | window.URL = window.URL || window.webkitURL; 4 | 5 | var audioContext; 6 | var oscilloscope; 7 | 8 | function init() { 9 | oscilloscope = AudioOscilloscope(document.getElementById('canvas'), { 10 | canvas: { 11 | width: function() { 12 | return window.innerWidth; 13 | }, 14 | height: 400 15 | }, 16 | canvasContext: { 17 | lineWidth: 2, 18 | fillStyle: 'rgb(0,0,0)', 19 | strokeStyle: 'green' 20 | } 21 | }); 22 | 23 | oscilloscope.on('drawFrame', function(osc) { 24 | var c = osc.canvas; 25 | var ctx = osc.canvasContext; 26 | var gradient = ctx.createLinearGradient(0,0,c.width,0); 27 | gradient.addColorStop(0, randomColor()); 28 | gradient.addColorStop(0.5, randomColor()); 29 | gradient.addColorStop(1, randomColor()); 30 | ctx.strokeStyle = gradient; 31 | }); 32 | 33 | oscilloscope.draw(); 34 | 35 | try { 36 | audioContext = new AudioContext(); 37 | } catch (e) { 38 | alert('No web audio support in this browser!'); 39 | } 40 | 41 | navigator.getUserMedia({ 42 | audio: true 43 | }, startUserMedia, userMediaError); 44 | } 45 | 46 | function startUserMedia(stream) { 47 | var streamSource = audioContext.createMediaStreamSource(stream); 48 | oscilloscope.addSource(streamSource); 49 | } 50 | 51 | function userMediaError(error) { 52 | console.error(error); 53 | } 54 | 55 | function randomColor() { 56 | var i = 3; 57 | var colors = []; 58 | while(i--) { 59 | colors.push(random(0,255)); 60 | } 61 | return 'rgb(' + colors.join(',') + ')'; 62 | } 63 | 64 | function random(a,b) { 65 | return ((Math.random() * (b - a + 1) + a)|0); 66 | } 67 | 68 | window.onload = init; 69 | -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/example/styles/style.css: -------------------------------------------------------------------------------- 1 | * { 2 | box-sizing: border-box; 3 | margin: 0; 4 | padding: 0; 5 | } 6 | 7 | body { 8 | background: #000; 9 | font-family: "Courier New",Courier,monospace; 10 | color: #fff; 11 | } 12 | 13 | h1 { 14 | font-size: 26px; 15 | font-weight: normal; 16 | padding: 5px; 17 | } 18 | -------------------------------------------------------------------------------- /example/bower_components/audio-oscilloscope/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "audio-oscilloscope", 3 | "version": "0.0.1", 4 | "description": "Audio oscilloscope in canvas.", 5 | "main": "audio-oscilloscope.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "tape test/*.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/miguelmota/audio-oscilloscope" 15 | }, 16 | "keywords": [ 17 | "audio", 18 | "sound", 19 | "canvas", 20 | "oscilloscope", 21 | "wave" 22 | ], 23 | "author": "Miguel Mota (http://www.miguelmota.com/)", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/miguelmota/audio-oscilloscope/issues" 27 | }, 28 | "homepage": "https://github.com/miguelmota/audio-oscilloscope", 29 | "dependencies": { 30 | "tape": "^3.0.3" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | AudioBufferSlice 8 | 9 | 10 |

AudioBufferSlice

11 | 12 | 13 | 14 | 15 | Fork me on GitHub 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /example/media/ethos-final-hope.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/miguelmota/audiobuffer-slice/c1fb0f8d2ce4574629075b8a8c5beac8a7fc747b/example/media/ethos-final-hope.mp3 -------------------------------------------------------------------------------- /example/scripts/main.js: -------------------------------------------------------------------------------- 1 | 2 | (function() { 3 | var url = './media/ethos-final-hope.mp3'; 4 | var canvas = document.createElement('canvas'); 5 | document.body.appendChild(canvas); 6 | 7 | var oscilloscope = AudioOscilloscope(canvas, { 8 | canvas: { 9 | width: function() { 10 | return window.innerWidth; 11 | }, 12 | height: 200 13 | }, 14 | canvasContext: { 15 | lineWidth: 2, 16 | fillStyle: 'rgb(0,0,0)', 17 | strokeStyle: 'green' 18 | } 19 | }); 20 | 21 | oscilloscope.draw(); 22 | 23 | var startButton = document.createElement('button'); 24 | startButton.textContent = 'Start'; 25 | startButton.disabled = true; 26 | startButton.onclick = function() { 27 | source.start(); 28 | stopped = false; 29 | }; 30 | 31 | document.body.appendChild(startButton); 32 | 33 | var stopButton = document.createElement('button'); 34 | var stopped = false; 35 | stopButton.textContent = 'Stop'; 36 | stopButton.onclick = function() { 37 | source.stop(); 38 | stopped = true; 39 | }; 40 | 41 | document.body.appendChild(stopButton); 42 | 43 | var audioContext = new AudioContext(); 44 | var analyser = audioContext.createAnalyser(); 45 | var source = audioContext.createBufferSource(); 46 | 47 | var xhr = new XMLHttpRequest(); 48 | xhr.open('GET', url); 49 | xhr.responseType = 'arraybuffer'; 50 | xhr.onerror = handleError; 51 | xhr.onload = function() { 52 | if (xhr.status === 200) { 53 | handleBuffer(xhr.response); 54 | } else { 55 | console.error(xhr.statusText); 56 | } 57 | }; 58 | xhr.send(); 59 | 60 | function handleError(error) { 61 | console.error(error); 62 | } 63 | 64 | function handleBuffer(audioData) { 65 | audioContext.decodeAudioData(audioData, decodeDone); 66 | } 67 | 68 | function decodeDone(buffer) { 69 | var begin = 50000; 70 | var end = begin + 20000; 71 | 72 | AudioBufferSlice(buffer, begin, end, function(error, slicedAudioBuffer) { 73 | if (error) { 74 | console.error(error); 75 | } else { 76 | source.buffer = slicedAudioBuffer; 77 | 78 | var gainNode = audioContext.createGain(); 79 | gainNode.gain.value = 1; 80 | source.connect(gainNode); 81 | gainNode.connect(audioContext.destination); 82 | 83 | oscilloscope.addSource(source); 84 | startButton.disabled = false; 85 | } 86 | }); 87 | } 88 | })(); 89 | -------------------------------------------------------------------------------- /example/styles/style.css: -------------------------------------------------------------------------------- 1 | * { 2 | box-sizing: border-box; 3 | } 4 | 5 | body { 6 | font-family: "Courier New", Courier, monospace; 7 | font-size: 14px; 8 | padding: 20px; 9 | text-align: left; 10 | background: #000; 11 | color: #0f0 12 | } 13 | 14 | h1 { 15 | font-weight: normal; 16 | } 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "audiobuffer-slice", 3 | "version": "0.0.7", 4 | "description": "Slice out a portion of an AudioBuffer.", 5 | "main": "audiobuffer-slice.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "tape test/*.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/miguelmota/audiobuffer-slice" 15 | }, 16 | "keywords": [ 17 | "audio", 18 | "audiobuffer", 19 | "slice" 20 | ], 21 | "author": "Miguel Mota (http://www.miguelmota.com/)", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/miguelmota/audiobuffer-slice/issues" 25 | }, 26 | "homepage": "https://github.com/miguelmota/audiobuffer-slice", 27 | "dependencies": { 28 | "tape": "^3.0.3" 29 | } 30 | } 31 | --------------------------------------------------------------------------------