refactor tests for readability + linting fixes
This commit is contained in:
parent
9bbabc5038
commit
57fce6d6cc
12 changed files with 123 additions and 318 deletions
95
tests/audio.js
Normal file
95
tests/audio.js
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
|
||||
const speech = require('@google-cloud/speech');
|
||||
const portAudio = require('naudiodon');
|
||||
// const rs = fs.createReadStream('rawAudio.wav');
|
||||
|
||||
// Creates a client
|
||||
const client = new speech.SpeechClient();
|
||||
|
||||
const encoding = 'LINEAR16';
|
||||
const sampleRateHertz = 16000;
|
||||
const languageCode = 'en-US';
|
||||
|
||||
const config = {
|
||||
encoding: encoding,
|
||||
sampleRateHertz: sampleRateHertz,
|
||||
languageCode: languageCode,
|
||||
};
|
||||
|
||||
/**
|
||||
* Note that transcription is limited to 60 seconds audio.
|
||||
* Use a GCS file for audio longer than 1 minute.
|
||||
*/
|
||||
let audio;
|
||||
|
||||
async function transcribeSpeech (audio) {
|
||||
const request = {
|
||||
config: config,
|
||||
audio: audio,
|
||||
};
|
||||
|
||||
// Detects speech in the audio file. This creates a recognition job that you
|
||||
// can wait for now, or get its result later.
|
||||
const [operation] = await client.longRunningRecognize(request);
|
||||
|
||||
// Get a Promise representation of the final result of the job
|
||||
const [response] = await operation.promise();
|
||||
|
||||
const transcription = response.results
|
||||
.map(result => result.alternatives[0].transcript)
|
||||
.join('\n');
|
||||
console.log(`Transcription: ${transcription}`);
|
||||
}
|
||||
|
||||
let audioInput = [];
|
||||
// Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
|
||||
const ia = new portAudio.AudioIO({
|
||||
inOptions: {
|
||||
channelCount: 1,
|
||||
sampleFormat: 16,
|
||||
sampleRate: 16000,
|
||||
deviceId: -1,
|
||||
closeOnError: false,
|
||||
}
|
||||
});
|
||||
ia.setEncoding('base64');
|
||||
ia.start();
|
||||
ia.on('error', (e) => {
|
||||
console.log('error recording audio', + e);
|
||||
});
|
||||
ia.on('data', (chunk) => {
|
||||
audioInput.push(chunk);
|
||||
console.log('Got %d characters of string data:', chunk.length);
|
||||
});
|
||||
|
||||
async function processAudio() {
|
||||
audio = '';
|
||||
audioInput.forEach(s => {
|
||||
audio += s;
|
||||
})
|
||||
const buffer = Buffer.from(audio, 'base64');
|
||||
audioInput = [];
|
||||
await transcribeSpeech({
|
||||
content: buffer
|
||||
});
|
||||
}
|
||||
|
||||
setTimeout(async () => {
|
||||
ia.pause();
|
||||
await processAudio();
|
||||
// ia.resume();
|
||||
}, 3000);
|
||||
|
||||
setTimeout(() => {
|
||||
ia.resume();
|
||||
}, 4000)
|
||||
|
||||
setTimeout(async () => {
|
||||
ia.pause();
|
||||
await processAudio();
|
||||
// ia.resume();
|
||||
}, 8000);
|
||||
|
||||
setTimeout(() => {
|
||||
ia.pause();
|
||||
}, 9000)
|
||||
20
tests/server.js
Normal file
20
tests/server.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
const WebSocket = require("ws");
|
||||
|
||||
const wss = new WebSocket.Server({
|
||||
port: 8081
|
||||
});
|
||||
|
||||
wss.on("connection", function connection(ws, req) {
|
||||
ws.on("message", function incoming(message) {
|
||||
|
||||
console.log(message)
|
||||
if (message === 'no-token' || message === 'undefined') {
|
||||
console.log('no token to authenticate');
|
||||
ws.send('locked');
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
const ip = req.socket.remoteAddress;
|
||||
console.log("received connection from", ip);
|
||||
});
|
||||
59
tests/ws.py
Normal file
59
tests/ws.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
# WS server example
|
||||
|
||||
import asyncio
|
||||
import websockets
|
||||
|
||||
# Imports the Google Cloud client library
|
||||
from google.cloud import speech
|
||||
|
||||
# Instantiates a client
|
||||
client = speech.SpeechClient()
|
||||
|
||||
speechtotext_client = speech.SpeechClient()
|
||||
|
||||
config = speech.RecognitionConfig(
|
||||
audio_channel_count=1,
|
||||
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
|
||||
sample_rate_hertz=16000,
|
||||
language_code="en-US",
|
||||
)
|
||||
|
||||
# Transcribe an audio file.
|
||||
def transcribe(content):
|
||||
|
||||
print(len(content))
|
||||
audio = speech.RecognitionAudio(content=content)
|
||||
|
||||
# Detects speech in the audio file
|
||||
response = speechtotext_client.recognize(config=config, audio=audio)
|
||||
input(f"Response: {response}")
|
||||
|
||||
for result in response.results:
|
||||
return result.alternatives[0].transcript
|
||||
|
||||
|
||||
async def session(websocket, path):
|
||||
buf = []
|
||||
|
||||
chunk = await websocket.recv()
|
||||
input(type(chunk))
|
||||
transcribe(chunk)
|
||||
# while True:
|
||||
# chunk = await websocket.recv_frame()
|
||||
|
||||
# if chunk == "stop":
|
||||
# break
|
||||
|
||||
# buf.append(chunk)
|
||||
|
||||
# input(f"Buffer sample: {buf[0]}")
|
||||
# audio = b"".join(buf)
|
||||
|
||||
# transcribe(audio)
|
||||
|
||||
|
||||
start_server = websockets.serve(session, "localhost", 8080)
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
asyncio.get_event_loop().run_forever()
|
||||
Loading…
Reference in a new issue