diff --git a/.babelrc b/.babelrc deleted file mode 100644 index fb890d2..0000000 --- a/.babelrc +++ /dev/null @@ -1,13 +0,0 @@ -{ - "presets": [ - [ - "es2015", - { - "modules": false - } - ] - ], - "plugins": [ - "external-helpers" - ] -} \ No newline at end of file diff --git a/example/index.html b/example/index.html index 216c466..ece6493 100644 --- a/example/index.html +++ b/example/index.html @@ -7,32 +7,25 @@

It should play audio if everying went well!

-

Yea! recoreded audio is not in good qualtity though!

- - + diff --git a/example/server/16bit-8000.raw b/example/server/16bit-8000.raw new file mode 100644 index 0000000..a922271 Binary files /dev/null and b/example/server/16bit-8000.raw differ diff --git a/example/server/32bit (float)-8000.raw b/example/server/32bit (float)-8000.raw new file mode 100644 index 0000000..f9404fd Binary files /dev/null and b/example/server/32bit (float)-8000.raw differ diff --git a/example/server/server.js b/example/server/server.js index aaa5d2b..2feed77 100644 --- a/example/server/server.js +++ b/example/server/server.js @@ -1,25 +1,19 @@ const WebSocket = require('ws'); const fs = require('fs'); -const opusPackets = './raw_opus/'; -let packets = [], - source = [], - interval = 0, - count = 0, +const pcm_file = './16bit-8000.raw'; +let interval = 0, + sampleRate = 8000, + channels = 2, + bytesChunk = (sampleRate * channels), + offset = 0, + pcmData, wss; -fs.readdir(opusPackets, (err, files) => { - files.forEach(function(file) { - fs.readFile(opusPackets+file, (err, data) => { - if (err) throw err; - source.push(data); - count++; - if (files.length == count) { - packets = source.slice(); - openSocket(); - } - }); - }); +fs.readFile(pcm_file, (err, data) => { + if (err) throw err; + pcmData = data; + openSocket(); }); @@ -32,26 +26,24 @@ function openSocket() { clearInterval(interval); } interval = setInterval(function() { - sendPacket(); - }, 10); + sendData(); + }, 500); }); } -function sendPacket() { - let packet; - if (packets.length == 0 && interval){ +function sendData() { + let payload; + if (offset >= pcmData.length) { clearInterval(interval); - packets = source.slice(); + offset = 0; return; } - packet = packets.shift(); + payload = pcmData.subarray(offset, (offset + bytesChunk)); + offset += bytesChunk; wss.clients.forEach(function each(client) { if (client.readyState === WebSocket.OPEN) { - client.send(packet); - if (packets.length % 100 == 0){ - console.log(`Remainging packets ${packets.length}`); - } + client.send(payload); } }); } \ No newline at end of file diff --git a/karma.conf.js b/karma.conf.js deleted file mode 100644 index 9cc9073..0000000 --- a/karma.conf.js +++ /dev/null @@ -1,77 +0,0 @@ -// Karma configuration -// Generated on Fri Nov 03 2017 16:10:03 GMT+0600 (+06) - -module.exports = function(config) { - config.set({ - - // base path that will be used to resolve all patterns (eg. files, exclude) - basePath: '', - - - // frameworks to use - // available frameworks: https://npmjs.org/browse/keyword/karma-adapter - frameworks: ['mocha'], - - // list of files / patterns to load in the browser - files: [ - 'test/*.js' - ], - - // list of files to exclude - exclude: [ - ], - - // preprocess matching files before serving them to the browser - // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor - preprocessors: { - 'test/*.js': ['rollup'] - }, - - rollupPreprocessor: { - plugins: [ - require('rollup-plugin-node-globals')(), - require('rollup-plugin-node-builtins')(), - require('rollup-plugin-babel')() - ], - format: 'iife', // Helps prevent naming collisions. - name: 'Decoder', // Required for 'iife' format. - sourcemap: 'inline' // Sensible for testing. - }, - - // test results reporter to use - // possible values: 'dots', 'progress' - // available reporters: https://npmjs.org/browse/keyword/karma-reporter - reporters: ['progress'], - - - // web server port - port: 9876, - - - // enable / disable colors in the output (reporters and logs) - colors: true, - - - // level of logging - // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG - logLevel: config.LOG_INFO, - - - // enable / disable watching file and executing tests whenever any file changes - autoWatch: false, - - - // start these browsers - // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher - browsers: ['Chrome'], - - - // Continuous Integration mode - // if true, Karma captures browsers, runs the tests and exits - singleRun: true, - - // Concurrency level - // how many browser should be started simultaneous - concurrency: Infinity - }) -} diff --git a/package.json b/package.json index 8c6fd57..366d2b4 100644 --- a/package.json +++ b/package.json @@ -14,24 +14,7 @@ }, "author": "Samir Das", "devDependencies": { - "babel-plugin-external-helpers": "^6.22.0", - "babel-preset-es2015": "^6.24.1", - "babel-register": "^6.26.0", - "karma": "^1.7.1", - "karma-chrome-launcher": "^2.2.0", - "karma-mocha": "^1.3.0", - "karma-rollup-preprocessor": "^5.0.1", - "mocha": "^4.0.1", - "path": "^0.12.7", - "rollup": "^0.50.0", - "rollup-plugin-babel": "^2.7.1", - "rollup-plugin-buble": "^0.16.0", - "rollup-plugin-eslint": "^3.0.0", - "rollup-plugin-node-builtins": "^2.1.2", - "rollup-plugin-node-globals": "^1.1.0", - "rollup-plugin-node-resolve": "^3.0.0", - "rollup-plugin-replace": "^1.1.1", - "rollup-plugin-uglify": "^1.0.1" + "ws": "^3.3.0" }, "dependencies": {} } diff --git a/src/pcm-player.js b/pcm-player.js similarity index 88% rename from src/pcm-player.js rename to pcm-player.js index 105683d..fd7322d 100644 --- a/src/pcm-player.js +++ b/pcm-player.js @@ -3,16 +3,16 @@ function PCMPlayer(option) { } PCMPlayer.prototype.init = function(option) { - var default = { + var defaults = { encoding: '16bitInt', channels: 1, sampleRate: 8000, - flushingTime: 200 + flushingTime: 1000 }; - this.option = Object.assign({}, default, option); + this.option = Object.assign({}, defaults, option); this.samples = new Float32Array(); this.flush = this.flush.bind(this); - this.interval = setInterval(this.flush, this.flushingTime); + this.interval = setInterval(this.flush, this.option.flushingTime); this.maxValue = this.getMaxValue(); this.typedArray = this.getTypedArray(); this.createContext(); @@ -31,10 +31,10 @@ PCMPlayer.prototype.getMaxValue = function () { PCMPlayer.prototype.getTypedArray = function () { var typedArrays = { - '8bitInt': 'Int8Array', - '16bitInt': 'Int16Array', - '32bitInt': 'Int32Array', - '32bitFloat': 'Float32Array' + '8bitInt': Int8Array, + '16bitInt': Int16Array, + '32bitInt': Int32Array, + '32bitFloat': Float32Array } return typedArrays[this.option.encoding] ? typedArrays[this.option.encoding] : typedArrays['16bitInt']; @@ -53,7 +53,7 @@ PCMPlayer.prototype.isTypedArray = function(data) { }; PCMPlayer.prototype.feed = function(data) { - if (!this.isTypedArray(isTypedArray)) return; + if (!this.isTypedArray(data)) return; data = this.getFormatedValue(data); var tmp = new Float32Array(this.samples.length + data.length); tmp.set(this.samples, 0); @@ -114,6 +114,7 @@ PCMPlayer.prototype.flush = function() { if (this.startTime < this.audioCtx.currentTime) { this.startTime = this.audioCtx.currentTime; } + console.log('start vs current '+this.startTime+' vs '+this.audioCtx.currentTime); bufferSource.buffer = audioBuffer; bufferSource.connect(this.gainNode); bufferSource.start(this.startTime); diff --git a/rollup.config.js b/rollup.config.js deleted file mode 100644 index 528edfb..0000000 --- a/rollup.config.js +++ /dev/null @@ -1,28 +0,0 @@ -/* global process */ - -// Rollup plugins -import babel from 'rollup-plugin-babel'; -import eslint from 'rollup-plugin-eslint'; -import replace from 'rollup-plugin-replace'; -import uglify from 'rollup-plugin-uglify'; - -export default { - input: 'src/opus-to-pcm.js', - output: { - file: 'dist/opus_to_pcm.js', - format: 'iife', - name: 'Decoder', - sourcemap: false, //inline - }, - plugins: [ - eslint(), - babel({ - exclude: 'node_modules/**', - }), - replace({ - exclude: 'node_modules/**', - ENV: JSON.stringify(process.env.NODE_ENV || 'development'), - }), - (process.env.NODE_ENV === 'production' && uglify()), - ], -}; \ No newline at end of file diff --git a/test/event.js b/test/event.js deleted file mode 100644 index 1bec7e9..0000000 --- a/test/event.js +++ /dev/null @@ -1,31 +0,0 @@ -import {equal, deepEqual} from 'assert'; -import Event from '../src/utils/event.js'; - -var event = new Event('XYZ'); - -var callback = function() { -}; - -describe('event tests --', function() { - - beforeEach(function(){ - event.offAll(); - event.on('topic1',callback); - event.on('topic1',callback); - event.on('topic2',callback); - }); - - it('listener should be 2 for the topic1', function() { - equal(event.listener.topic1.length, 2); - }); - - it('event dispatcher should be return true for a successful dispatcher', function() { - equal(event.dispatch('topic1', true), true); - }); - - it('listener should be zero after removing all listeners', function() { - event.offAll(); - deepEqual(event.listener, {}); - }); -}); - diff --git a/test/ogg.js b/test/ogg.js deleted file mode 100644 index 3e0cf96..0000000 --- a/test/ogg.js +++ /dev/null @@ -1,34 +0,0 @@ -import {equal} from 'assert'; -import Ogg from '../src/utils/ogg.js'; -var channel = 1, - decoder = new Ogg(channel), - audioCtx = new (window.AudioContext || window.webkitAudioContext)(), - sampleRate = audioCtx.sampleRate; - - -describe('Ogg tests -- ', function() { - it('sample rate should be same system sample rate', function() { - equal(decoder.getSampleRate(), sampleRate); - }); - - it('magic Signature of ID header should be Opus Head', function() { - var idHeader = decoder.getIDHeader(); - var dv = new DataView(idHeader.buffer); - equal(dv.getUint32(0, true), 1937076303); - equal(dv.getUint32(4, true), 1684104520); - }); - - it('magic Signature of comment header should be Opus Tags', function() { - var commonHeader = decoder.getCommentHeader(); - var dv = new DataView(commonHeader.buffer); - equal(dv.getUint32(0, true), 1937076303); - equal(dv.getUint32(4, true), 1936154964); - }); - - it('page header should be started with OggS', function() { - var segmentData = new Uint8Array(20); - var page = decoder.getPage(segmentData, 4); - var dv = new DataView(page.buffer); - equal(dv.getUint32(0, true), 1399285583); - }); -}); \ No newline at end of file