Skip to content

Commit

Permalink
fix(dev): added commitlint, updated husky hooks, minor library changes
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkelTem committed Apr 11, 2021
1 parent 4fcde59 commit 6572f2c
Show file tree
Hide file tree
Showing 8 changed files with 405 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .husky/commitlint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run lint
npm run lint -s -- --max-warnings=0
6 changes: 6 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
"header-max-length": [0, "always", 150],
},
};
8 changes: 4 additions & 4 deletions examples/04-play-file-on-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import pino from 'pino';

import { cred1 } from './utils';
import zello, { DEFAULT_ZELLO_OPTIONS, Zello, getAudioFileStreamCtl } from '../lib';
import zello, { DEFAULT_ZELLO_OPTIONS, Zello, getAudioFileStream } from '../lib';

const pinoLogger = pino(DEFAULT_ZELLO_OPTIONS.logger);

Expand All @@ -21,7 +21,7 @@ if (!existsSync(filename)) {

const samplingRate = 16000;
const frameSize = 60;
const stream = getAudioFileStreamCtl(filename, pinoLogger, {
const stream = getAudioFileStream(filename, pinoLogger, {
samplingRate,
volumeFactor: 0.3,
});
Expand All @@ -31,7 +31,7 @@ async function main() {
try {
await z.ctl.run(function* ({ macros }) {
yield macros.login(cred1);
yield macros.sendAudio(stream.stream, {
yield macros.sendAudio(stream, {
transcode: { samplingRate, frameSize, bitrateKbps: 32, channels: 1 },
});
});
Expand All @@ -43,7 +43,7 @@ async function main() {
async function shutdown() {
if (z && z.ctl.status() === 'OPEN') {
console.warn('Closing...');
await stream.close();
await stream.destroy();
await z.ctl.close();
}
}
Expand Down
28 changes: 9 additions & 19 deletions lib/audio.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import prism from 'prism-media';
import pEvent from 'p-event';
import { Duplex, PassThrough, Readable } from 'stream';
import { Logger } from 'pino';
import { Channels, FFmpegArgs, FrameSize, OpusInfo, SamplingRate, StreamGetter, StreamGetterOptions } from './types';
Expand Down Expand Up @@ -206,23 +205,20 @@ export function initAudioStream(
};
}

type AudioFileStreamCtl = {
stream: Readable;
close: () => Promise<unknown>;
};

export function getAudioFileStreamCtl(
path: string,
parentLogger: Logger,
options?: FileStreamOptions,
): AudioFileStreamCtl {
const logger = parentLogger.child({ facility: 'getFileStream' });
export function getAudioFileStream(path: string, parentLogger: Logger, options?: FileStreamOptions): Readable {
const logger = parentLogger.child({ facility: 'getAudioFileStream' });

const rs = fs.createReadStream(path);
rs.on('error', (err) => {
logger.error(err, 'Read stream error');
throw err;
});
return getAutoDecodeStream(rs, logger, options);
}

export function getAutoDecodeStream(stream: Readable, parentLogger: Logger, options?: FileStreamOptions): Duplex {
const logger = parentLogger.child({ facility: 'getAutoDecodeStream' });

const { samplingRate, volumeFactor, ffmpegArgs }: Required<FileStreamOptions> =
options != null ? { ...FILE_STREAM_DEFAULT_OPTIONS, ...options } : FILE_STREAM_DEFAULT_OPTIONS;

Expand All @@ -249,11 +245,5 @@ export function getAudioFileStreamCtl(
logger.error(err, 'Transcoder stream error');
throw err;
});
return {
stream: rs.pipe(transcodeStream),
close: async () => {
rs.destroy();
await pEvent(rs, 'close');
},
};
return stream.pipe(transcodeStream);
}
Loading

0 comments on commit 6572f2c

Please sign in to comment.