|
| 1 | +# wave-resampler |
| 2 | +Copyright (c) 2019 Rafael da Silva Rocha. |
| 3 | +https://github.com/rochars/wave-resampler |
| 4 | + |
| 5 | +[](https://www.npmjs.com/package/wave-resampler) [](https://rochars.github.io/wave-resampler/docs) [](https://rochars.github.io/wave-resampler/test/index.html) |
| 6 | +[](https://codecov.io/gh/rochars/wave-resampler) [](https://travis-ci.org/rochars/wave-resampler) [](https://ci.appveyor.com/project/rochars/wave-resampler) [](https://scrutinizer-ci.com/g/rochars/wave-resampler/) |
| 7 | + |
| 8 | +PCM audio resampler written entirely in JavaScript. |
| 9 | + |
| 10 | +## Install |
| 11 | +``` |
| 12 | +npm install wave-resampler |
| 13 | +``` |
| 14 | + |
| 15 | +## Use |
| 16 | + |
| 17 | +### Node |
| 18 | +```javascript |
| 19 | +const waveResampler = require('wave-resampler'); |
| 20 | +// resample 48kHz samples to 44.1kHz |
| 21 | +let newSamples = waveResampler.resample(samples, 48000, 44100); |
| 22 | +``` |
| 23 | +or |
| 24 | +```javascript |
| 25 | +import { resampler } from 'wave-resampler'; |
| 26 | +let newSamples = resample(samples, 48000, 44100); |
| 27 | +``` |
| 28 | + |
| 29 | +### Browser |
| 30 | +Use the **wave-resampler.js** file in the *dist* folder: |
| 31 | +```html |
| 32 | +<script src="wave-resampler.js"></script> |
| 33 | +<script> |
| 34 | + var newSamples = waveResampler.resample(samples, 48000, 44100); |
| 35 | +</script> |
| 36 | +``` |
| 37 | + |
| 38 | +Or load it from the [jsDelivr](https://cdn.jsdelivr.net/npm/wave-resampler) CDN: |
| 39 | +```html |
| 40 | +<script src="https://cdn.jsdelivr.net/npm/wave-resampler"></script> |
| 41 | +``` |
| 42 | + |
| 43 | +Or load it from [unpkg](https://unpkg.com/wave-resampler): |
| 44 | +```html |
| 45 | +<script src="https://unpkg.com/wave-resampler"></script> |
| 46 | +``` |
| 47 | + |
| 48 | +## Advanced use |
| 49 | + |
| 50 | +### Resampling methods |
| 51 | +- **point**: Nearest point interpolation, lowest quality, no LPF by default, fastest |
| 52 | +- **linear**: Linear interpolation, low quality, no LPF by default, fast |
| 53 | +- **cubic**: Cubic interpolation, use LPF by default **(default method)** |
| 54 | +- **sinc**: Windowed sinc interpolation, use LPF by default, slowest |
| 55 | + |
| 56 | +To use a different interpolation method: |
| 57 | +```javascript |
| 58 | +// Will use 'sinc' method |
| 59 | +resample(48000, 44100, {method: "sinc"}); |
| 60 | + |
| 61 | +// Will use 'linear' method |
| 62 | +resample(48000, 44100, {method: "linear"}); |
| 63 | +``` |
| 64 | + |
| 65 | +### LPF |
| 66 | +You can turn the LPF on and off for any resampling method: |
| 67 | +```javascript |
| 68 | +// Will use 'sinc' method with no LPF |
| 69 | +let newSamples = resample(48000, 44100, {method: "sinc", LPF: false}); |
| 70 | + |
| 71 | +// Will use 'linear' method with LPF |
| 72 | +let newSamples = resample(48000, 44100, {method: "linear", LPF: true}); |
| 73 | +``` |
| 74 | + |
| 75 | +The default LPF is a IIR LPF. You may define what type of LPF will be used |
| 76 | +by changing the LPFType attribute on the *toSampleRate()* param. |
| 77 | +You can use **IIR** or **FIR**: |
| 78 | +```javascript |
| 79 | +// Will use 'linear' method with a FIR LPF |
| 80 | +let newSamples = resample(48000, 44100, |
| 81 | + {method: "linear", LPF: true, LPFType: 'FIR'}); |
| 82 | + |
| 83 | +// Will use 'linear' method with a IIR LPF, the default |
| 84 | +let newSamples = resample(48000, 44100, |
| 85 | + {method: "linear", LPF: true}); |
| 86 | +``` |
| 87 | + |
| 88 | +You can change the order of the LPF for both IIR and FIR with |
| 89 | +the attribute LPFOrder: |
| 90 | +```javascript |
| 91 | +let newSamples = resample(48000, 44100, |
| 92 | + {method: "linear", LPF: true, LPFType: 'IIR', LPFOrder: 2}); |
| 93 | +``` |
| 94 | + |
| 95 | +The default order for IIR is 16 and for FIR is 71. |
| 96 | + |
| 97 | +## API |
| 98 | +```javascript |
| 99 | +/** |
| 100 | + * Change the sample rate of the samples to a new sample rate. |
| 101 | + * @param {!Array|!TypedArray} samples The original samples. |
| 102 | + * @param {number} oldSampleRate The original sample rate. |
| 103 | + * @param {number} sampleRate The target sample rate. |
| 104 | + * @param {?Object} details The extra configuration, if needed. |
| 105 | + * @return {!Float64Array} the new samples. |
| 106 | + */ |
| 107 | +export function resample(samples, oldSampleRate, sampleRate, details={}) {} |
| 108 | +``` |
| 109 | + |
| 110 | +### LICENSE |
| 111 | +Copyright (c) 2019 Rafael da Silva Rocha. |
| 112 | + |
| 113 | +Permission is hereby granted, free of charge, to any person obtaining |
| 114 | +a copy of this software and associated documentation files (the |
| 115 | +"Software"), to deal in the Software without restriction, including |
| 116 | +without limitation the rights to use, copy, modify, merge, publish, |
| 117 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 118 | +permit persons to whom the Software is furnished to do so, subject to |
| 119 | +the following conditions: |
| 120 | + |
| 121 | +The above copyright notice and this permission notice shall be |
| 122 | +included in all copies or substantial portions of the Software. |
| 123 | + |
| 124 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 125 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 126 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 127 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 128 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 129 | +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 130 | +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments