Skip to content

Commit

Permalink
Support for node 😂
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Feb 16, 2016
1 parent ef3089d commit d6b8b33
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ build/*.min.js: build/Pxtone.js build/pxtnDecoder.js

build/Pxtone.js: build/
mkdir -p temp && \
browserify -t babelify -s Pxtone src/index.js --no-commondir --igv global -i web-audio-api -i iconv-lite -o temp/Pxtone.js && \
browserify -t babelify -s Pxtone src/index.js --no-commondir --igv global -i web-audio-api -i text-encoding -o temp/Pxtone.js && \
echo "/*! PxtoneJS" v`node -pe "require('./package.json').version"` "http://git.io/PxtoneJS */" | cat - temp/Pxtone.js > build/Pxtone.js && \
$(RM) -rf temp

Expand Down
1 change: 1 addition & 0 deletions src/Pxtone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import createDecoder from "./createDecoder";

import { checkAudioContext } from "./value";


const _ = createPrivateStorage();

export default class Pxtone {
Expand Down
2 changes: 1 addition & 1 deletion src/createDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import "./polyfill";
import textDecoder from "./textDecoder";
import { checkArguments, getAptSampleRate } from "./value";

const Worker = global.Worker;

const Worker = global.Worker || (() => {});

function uint8SetAscii(offset, str) {
const strLength = str.length;
Expand Down
3 changes: 2 additions & 1 deletion src/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import "promise-decode-audio-data";

// for node
const AudioContext = require("web-audio-api").AudioContext;

if(AudioContext) {

const ctx = new AudioContext();
ctx._kill();

const noop = () => {};
const isPromiseBased = ctx.decodeAudioData(new Uint32Array([ 1179011410, 48, 1163280727, 544501094, 16, 131073, 44100, 176400, 1048580, 1635017060, 8, 0, 0, 0, 0 ]).buffer, noop, noop);
const isPromiseBased = ctx.decodeAudioData(new ArrayBuffer(), noop, noop);

if(!isPromiseBased) {
const original = AudioContext.prototype.decodeAudioData;
Expand Down
6 changes: 1 addition & 5 deletions src/textDecoder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// load only in node
const decode = require("iconv-lite").decode;
const TextDecoder = global.TextDecoder || require("text-encoding").TextDecoder;

export default function textDecoder(arraybuffer, charset = "shift_jis") {
if(arraybuffer.byteLength === 1 && (new Uint8Array(arraybuffer))[0] === 255)
Expand All @@ -19,8 +18,5 @@ export default function textDecoder(arraybuffer, charset = "shift_jis") {
};
reader.readAsText(blob, charset);
});
}).catch(() => {
// iconv-lite for node
resolve(decode(new Buffer(arraybuffer), charset));
});
}
1 change: 0 additions & 1 deletion src/value.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const AudioContext = global.AudioContext || global.webkitAudioContext || require("web-audio-api").AudioContext;


export function checkArguments(ctx, type, buffer, ch, sps, bps) {
let errStr = "Invalid arguments:";
let isErr = false;
Expand Down

0 comments on commit d6b8b33

Please sign in to comment.