Commit 6117ac6a authored by martin hou's avatar martin hou

feat: 增加新指令的实现与测试

parent e6bb5ae2
......@@ -343,6 +343,21 @@ declare class Jensen {
// 发送按键消息
sendKeyCode: (mode: number, keyCode: number, seconds?: number) => Promise<ReturnStruct['common']>;
// 获取录音状态
getRecordingStatus: (seconds?: number) => Promise<{ recording: null | string; duration: number; samples: number[] }>;
// 获取录音质量
getRecordingQuality: (seconds?: number) => Promise<{ quality: 'normal' | 'high' }>;
// 设置录音质量
setRecordingQuality: (quality: 'normal' | 'high', seconds?: number) => Promise<ReturnStruct['common']>;
// 获取录音输入设备
getAudioInputDevice: (seconds?: number) => Promise<{ device: 'bt-mic' | 'mic' }>;
// 设置录音输入设备
setAudioInputDevice: (device: 'bt-mic' | 'mic', seconds?: number) => Promise<ReturnStruct['common']>;
dump: (void);
}
......
......@@ -2,4 +2,4 @@ import { createRoot } from 'react-dom/client';
import { Home } from '.';
import { DeviceManager } from './mgr';
createRoot(document.getElementById('root') as HTMLDivElement).render(<DeviceManager />);
createRoot(document.getElementById('root') as HTMLDivElement).render(<Home />);
......@@ -731,6 +731,45 @@ export function Home() {
Logger.info('jensen', 'live', 'Enter MassStorage Mode: ' + JSON.stringify(rst));
}
const getRecordingStatus = async () => {
let jensen = getJensen();
if (jensen == null) return;
let rst = await jensen.getRecordingStatus(5);
Logger.info('jensen', 'recording', 'Get Recording Status: ' + JSON.stringify(rst));
}
const switchRecordingQuality = async () => {
let jensen = getJensen();
if (jensen == null) return;
let current = await jensen.getRecordingQuality(5);
let next = current.quality == 'normal' ? 'high' : 'normal';
let rst = await jensen.setRecordingQuality(next as 'normal' | 'high', 5);
Logger.info('jensen', 'quality', 'Set Recording Quality: ' + JSON.stringify(rst));
}
const getRecordingQuality = async () => {
let jensen = getJensen();
if (jensen == null) return;
let rst = await jensen.getRecordingQuality(5);
Logger.info('jensen', 'quality', 'Get Recording Quality: ' + JSON.stringify(rst));
}
const switchAudioInputDevice = async () => {
let jensen = getJensen();
if (jensen == null) return;
let current = await jensen.getAudioInputDevice(5);
let next = current.device == 'bt-mic' ? 'mic' : 'bt-mic';
let rst = await jensen.setAudioInputDevice(next as 'bt-mic' | 'mic', 5);
Logger.info('jensen', 'audio-input', 'Set Audio Input Device: ' + JSON.stringify(rst));
}
const getAudioInputDevice = async () => {
let jensen = getJensen();
if (jensen == null) return;
let rst = await jensen.getAudioInputDevice(5);
Logger.info('jensen', 'audio-input', 'Get Audio Input Device: ' + JSON.stringify(rst));
}
return (
<>
<div className="btn-container" style={{ display: 'flex', flexDirection: 'row', gap: '16px', padding: '16px', alignItems: 'center', flexWrap: 'wrap' }}>
......@@ -768,6 +807,13 @@ export function Home() {
<button onClick={() => sendKey(0x02, 0x03)}>Record (Long Click)</button>
<button onClick={() => sendKey(0x03, 0x05)}>Playback (Dbl Press)</button>
<button onClick={enterMassStorageMode}>MassStorage Mode</button>
<button onClick={getRecordingStatus}>Get Recording Status</button>
<button onClick={getRecordingQuality}>Get Recording Quality</button>
<button onClick={switchRecordingQuality}>Switch Recording Quality</button>
<button onClick={getAudioInputDevice}>Get Audio Input Device</button>
<button onClick={switchAudioInputDevice}>Switch Audio Input Device</button>
<span style={{ marginLeft: '8px' }}>Interval(ms): </span>
<input
type="number"
......
......@@ -49,6 +49,13 @@ const RECORD_TEST_END = 0xf009;
const WRITE_WEBUSB_TIMEOUT = 0xf010;
const READ_WEBUSB_TIMEOUT = 0xf011;
const GET_RECORDING_STATUS = 0x1d;
const GET_RECORDING_QUALITY = 0x1f;
const SET_RECORDING_QUALITY = 0x1e;
const GET_AUDIO_INPUT_DEV = 0x100a;
const SET_AUDIO_INPUT_DEV = 0x1009;
const COMMAND_NAMES = {
[INVAILD]: 'invalid-0',
[QUERY_DEVICE_INFO]: 'get-device-info',
......@@ -852,6 +859,39 @@ Jensen.prototype.sendKeyCode = async function (mode, keyCode, seconds)
return this.send(new Command(SEND_KEY_CODE).body([mode & 0xff, keyCode & 0xff]), seconds);
};
Jensen.prototype.getRecordingStatus = async function (seconds)
{
return this.send(new Command(GET_RECORDING_STATUS), seconds);
}
Jensen.prototype.getRecordingQuality = async function (seconds)
{
return this.send(new Command(GET_RECORDING_QUALITY), seconds);
}
Jensen.prototype.setRecordingQuality = async function (quality, seconds)
{
return this.send(new Command(SET_RECORDING_QUALITY).body([quality & 0xff]), seconds);
}
Jensen.prototype.getRecordingQuality = async function (seconds)
{
return this.send(new Command(GET_RECORDING_QUALITY), seconds);
}
Jensen.prototype.getAudioInputDevice = async function (seconds)
{
return this.send(new Command(GET_AUDIO_INPUT_DEV), seconds);
}
Jensen.prototype.setAudioInputDevice = async function (device, seconds)
{
// 0x00: 蓝牙麦克风, 0x01: 内置麦克风
// device: 'bt-mic' or 'mic'
const dev = 'bt-mic' == device ? 0x00 : 0x01;
return this.send(new Command(SET_AUDIO_INPUT_DEV).body([dev]), seconds);
}
Jensen.prototype.listFiles = async function () {
this.setFileListing(true);
let tag = 'filelist-' + this.serialNumber;
......@@ -1761,6 +1801,41 @@ Jensen.registerHandler(BT_DEV_LIST, (msg, jensen) => {
return devices;
});
Jensen.registerHandler(GET_RECORDING_STATUS, (msg) => {
if (msg.body.length == 0) return null;
let fname = '';
let idx = 0;
let len = ((msg.body[idx++] & 0xff) << 8) || (msg.body[idx++] & 0xff);
for (let i = 0; i < len && i < msg.body.length; i++) {
fname += String.fromCharCode(msg.body[idx++] & 0xff);
}
// 录音时长
let duration = ((msg.body[idx+1] & 0xff) << 8) || (msg.body[idx+2] & 0xff);
// 声音采样
len = ((msg.body[idx++] & 0xff) << 8) || (msg.body[idx++] & 0xff);
let samples = [];
for (let i = 0; i < len && i < msg.body.length; i++) {
samples.push(msg.body[idx++] & 0xff);
}
return { recording: fname, duration: duration, samples: samples };
});
Jensen.registerHandler(GET_RECORDING_QUALITY, (msg) => {
return { quality: msg.body[0] & 0xff == 0x00 ? 'normal' : 'high' };
});
Jensen.registerHandler(SET_RECORDING_QUALITY, commonMessageParser);
Jensen.registerHandler(SET_AUDIO_INPUT_DEV, commonMessageParser);
Jensen.registerHandler(GET_AUDIO_INPUT_DEV, (msg) => {
let dev = 0x00;
for (let i = 0; i < 4 && i < msg.body.length; i++) {
dev = (msg.body[i] & 0xff) << ((3 - i) * 8);
}
const mic = dev == 0x00 ? 'bt-mic' : 'mic';
return { device: mic };
});
function __is_headset_or_mic(cod)
{
if (typeof cod !== 'number') return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment