-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
115 lines (85 loc) · 2.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @module audio
*
* High-level audio container
*/
'use strict'
const isPlainObj = require('is-plain-obj')
const AudioBufferList = require('audio-buffer-list')
const createBuffer = require('audio-buffer-from')
const isAudioBuffer = require('is-audio-buffer')
const db = require('decibels')
let {isMultisource} = require('./src/util')
module.exports = Audio
// conversions
Audio.gain = db.toGain
Audio.db = db.fromGain
Audio.prototype.time = function offsetToTime (offset) {
return offset / this.sampleRate
}
Audio.prototype.offset = function timeToOffset (time) {
return Math.ceil(time * this.sampleRate)
}
// @constructor
function Audio(source, options) {
if (!(this instanceof Audio)) return new Audio(source, options)
// handle channels-only options
if (typeof options === 'number') options = {channels: options}
if (isPlainObj(source)) {
options = source
source = null
}
if (!options) options = {}
// enable metrics
if (options.stats) this.stats = true
if (options.data) source = options.data
let context = options.context
// empty case
if (source === undefined || typeof source === 'number') {
options.duration = source || 0
if (options.duration < 0) throw Error('Duration should not be negative')
source = null
this.buffer = new AudioBufferList(createBuffer(options))
}
// audiobufferlist case
if (source instanceof AudioBufferList) {
this.buffer = source
}
// other Audio instance
else if (source instanceof Audio) {
this.buffer = source.buffer.clone()
}
// audiobuffer case
else if (isAudioBuffer(source)) {
this.buffer = new AudioBufferList(source)
}
// array with malformed data
else if (isMultisource(source)) {
throw Error('Bad argument. Use `Audio.from` to create joined audio.')
}
// fall back to buffer
else {
try {
let buf = createBuffer(source, options)
this.buffer = new AudioBufferList(buf)
}
catch (e) {
throw e
}
}
// slice by length
if (options.length != null) {
if (this.buffer.length > options.length) {
this.buffer = this.buffer.slice(0, options.length)
}
else if (this.buffer.length < options.length) {
this.buffer.append(options.length - this.buffer.length)
}
}
// TODO: remix channels if provided in options
}
require('./src/core')
require('./src/playback')
require('./src/metrics')
require('./src/manipulations')
require('./src/alias')