Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sound addon #3494

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"addons/xterm-addon-serialize/src/tsconfig.json",
"addons/xterm-addon-serialize/test/tsconfig.json",
"addons/xterm-addon-serialize/benchmark/tsconfig.json",
"addons/xterm-addon-sound/src/tsconfig.json",
"addons/xterm-addon-sound/test/tsconfig.json",
"addons/xterm-addon-unicode11/src/tsconfig.json",
"addons/xterm-addon-unicode11/test/tsconfig.json",
"addons/xterm-addon-web-links/src/tsconfig.json",
Expand Down
19 changes: 19 additions & 0 deletions addons/xterm-addon-sound/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021, The xterm.js authors (https://github.com/xtermjs/xterm.js)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions addons/xterm-addon-sound/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## xterm-addon-sound

TODO...
78 changes: 78 additions & 0 deletions addons/xterm-addon-sound/fixture/example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash

function notes() {
echo -n "Notes: "
echo -n "𝄞 ⁴/₄ "
echo -ne "C5 \x1b[7;32;1,~"
echo -ne "D5 \x1b[7;32;3,~"
echo -ne "E5 \x1b[7;32;5,~"
echo -ne "F5 \x1b[7;32;6,~"
echo -n " 𝄀 "
echo -ne "G5 \x1b[7;32;8,~"
echo -ne "A5 \x1b[7;32;10,~"
echo -ne "B5 \x1b[7;32;12,~"
echo -ne "C5 \x1b[7;32;13,~"
echo -n " 𝄀"
}

function volume_levels() {
echo -n "Volume levels: "
echo -ne "0 \x1b[0;32;1,~"
echo -ne "\x1b[0;32;1,~"
echo -ne "1 \x1b[1;32;1,~"
echo -ne "\x1b[0;32;1,~"
echo -ne "2 \x1b[2;32;1,~"
echo -ne "\x1b[0;32;1,~"
echo -ne "3 \x1b[3;32;1,~"
echo -ne "\x1b[0;32;1,~"
echo -ne "4 \x1b[4;32;1,~"
echo -ne "\x1b[0;32;1,~"
echo -ne "5 \x1b[5;32;1,~"
echo -ne "\x1b[0;32;1,~"
echo -ne "6 \x1b[6;32;1,~"
echo -ne "\x1b[0;32;1,~"
echo -ne "7 \x1b[7;32;1,~"
}

function duration() {
echo -n "Durations (in s): "
echo -ne "1/32 "
for i in {1..32}
do
echo -ne "\x1b[7;1;1,~\x1b[7;1;3,~\x1b[7;1;5,~\x1b[7;1;6,~"
done

echo -ne "1/16 "
for i in {1..16}
do
echo -ne "\x1b[7;2;1,~\x1b[7;2;3,~\x1b[7;2;5,~\x1b[7;2;6,~"
done

echo -ne "1/8 "
for i in {1..8}
do
echo -ne "\x1b[7;4;1,~\x1b[7;4;3,~\x1b[7;4;5,~\x1b[7;4;6,~"
done

echo -ne "1/4 "
for i in {1..4}
do
echo -ne "\x1b[7;8;1,~\x1b[7;8;3,~\x1b[7;8;5,~\x1b[7;8;6,~"
done

echo -ne "1/2 "
for i in {1..2}
do
echo -ne "\x1b[7;16;1,~\x1b[7;16;3,~\x1b[7;16;5,~\x1b[7;16;6,~"
done

echo -ne "1/1 \x1b[7;32;1,~\x1b[7;32;3,~\x1b[7;32;5,~\x1b[7;32;6,~"
}

notes
echo
volume_levels
echo
duration
echo
echo "Done."
26 changes: 26 additions & 0 deletions addons/xterm-addon-sound/fixture/sndcat/sndcat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import wave
import base64
import sys


def create_sound_sequence(channels, width, rate, frames):
b64data = base64.b64encode(frames).decode('ascii')
return f'\x1bP{channels};{width};{rate};{len(frames)};{len(b64data)}a{b64data}\x1b\\'


def stream_wav(filename):
with wave.open(filename) as wav:
channels = wav.getnchannels()
width = wav.getsampwidth()
rate = wav.getframerate()
frames = wav.getnframes()
print({'channels': channels, 'width': width, 'rate': rate, 'frames': frames})
pos = 0
while pos < frames:
print(create_sound_sequence(channels, width, rate, wav.readframes(rate)), end='')
pos += rate
print()


if __name__ == '__main__':
stream_wav(sys.argv[1])
21 changes: 21 additions & 0 deletions addons/xterm-addon-sound/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "xterm-addon-sound",
"version": "0.1.0",
"author": {
"name": "The xterm.js authors",
"url": "https://xtermjs.org/"
},
"main": "lib/xterm-addon-sound.js",
"types": "typings/xterm-addon-sound.d.ts",
"repository": "https://github.com/xtermjs/xterm.js",
"license": "MIT",
"scripts": {
"build": "../../node_modules/.bin/tsc -p .",
"prepackage": "npm run build",
"package": "../../node_modules/.bin/webpack",
"prepublishOnly": "npm run package"
},
"peerDependencies": {
"xterm": "^4.0.0"
}
}
106 changes: 106 additions & 0 deletions addons/xterm-addon-sound/src/AudioSampleHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Base64 } from './base64';

type ParamsArray = (number | number[])[];
interface IParams {
/** from ctor */
maxLength: number;
maxSubParamsLength: number;

/** param values and its length */
params: Int32Array;
length: number;

/** methods */
clone(): IParams;
toArray(): ParamsArray;
reset(): void;
addParam(value: number): void;
addSubParam(value: number): void;
hasSubParams(idx: number): boolean;
getSubParams(idx: number): Int32Array | null;
getSubParamsAll(): {[idx: number]: Int32Array};
}
interface IDcsHandler {
hook(params: IParams): void;
put(data: Uint32Array, start: number, end: number): void;
unhook(success: boolean): boolean | Promise<boolean>;
}
interface IWaveSnippet {
// number of channels
channels: number;
// sampling width
width: number;
// sampling rate
rate: number;
byteLength: number;
byteLengthB64: number;
}

export class AudioSampleHandler implements IDcsHandler {
private _buffer = new Uint8Array(1000000);
private _buffer2 = new Uint8Array(1000000);
private _pos = 0;
private _waveSnippet: IWaveSnippet | undefined;

constructor(private _terminal: any) {}

public hook(params: IParams): void {
console.log(params.toArray());
// TODO: sanitize params / default params
this._waveSnippet = {
channels: params.params[0],
width: params.params[1],
rate: params.params[2],
byteLength: params.params[3],
byteLengthB64: params.params[4]
}
this._pos = 0;
}

public put(data: Uint32Array, start: number, end: number): void {
if (!this._waveSnippet) {
return;
}
this._buffer.set(data.subarray(start, end), this._pos);
this._pos += end - start;
}

public unhook(success: boolean): boolean | Promise<boolean> {
if (!this._waveSnippet) {
return true;
}
const idx = Base64.decode(this._buffer, this._buffer2, this._pos);

// quick hack - load and play
return new Promise(async res => {
if (!this._waveSnippet) {
return res(true);
}
const ctx = (this._terminal as any)._core?._soundService.constructor.audioContext as AudioContext;
const audioBuffer = ctx.createBuffer(
this._waveSnippet.channels,
22050,
this._waveSnippet.rate
);

for (let c = 0; c < this._waveSnippet.channels; ++c) {
const data = audioBuffer.getChannelData(c);

// TODO: iterate 2nd channel
const buf16 = new Int16Array(this._buffer2.buffer, 0, idx / 2);
for (let i = 0; i < 22050; ++i) {
data[i] = buf16[i] / 32768;
//const s = buf16[i];
//data[i] = s < 0 ? s / 32768 : s / 32767;
}
}

this._waveSnippet = undefined;
const source = ctx.createBufferSource();
source.buffer = audioBuffer;
source.connect(ctx.destination);
source.start();
source.onended = () => res(true);
});
}
}
Loading