|
| 1 | +// @ts-nocheck |
1 | 2 | // Download the helper library from https://www.twilio.com/docs/node/install
|
2 | 3 | // Find your Account SID and Auth Token at twilio.com/console
|
3 | 4 | // and set the environment variables. See http://twil.io/secure
|
4 | 5 | import dotenv from "dotenv";
|
5 |
| -dotenv.config(); |
6 |
| -const accountSid = process.env.TWILIO_ACCOUNT_SID!; |
7 |
| -const authToken = process.env.TWILIO_AUTH_TOKEN!; |
8 |
| - |
9 |
| -const client = require("twilio")(accountSid, authToken); |
10 |
| - |
11 |
| -export const init = async () => { |
12 |
| - client.calls |
13 |
| - .create({ |
14 |
| - url: "http://demo.twilio.com/docs/voice.xml", |
15 |
| - to: "+916230322486", |
16 |
| - from: "+918580732070", |
17 |
| - }) |
18 |
| - .then((call: any) => console.log(call.sid, call)) |
19 |
| - .catch((err: any) => console.log(accountSid)); |
| 6 | +import { Request, Response } from "express"; |
| 7 | +const fs = require("fs"); |
| 8 | +const path = require("path"); |
| 9 | +const AudioRecorder = require("node-audiorecorder"); |
| 10 | + |
| 11 | +const options = { |
| 12 | + program: `rec`, // Which program to use, either `arecord`, `rec`, or `sox`. |
| 13 | + device: null, // Recording device to use, e.g. `hw:1,0` |
| 14 | + |
| 15 | + bits: 16, // Sample size. (only for `rec` and `sox`) |
| 16 | + channels: 1, // Channel count. |
| 17 | + encoding: `signed-integer`, // Encoding type. (only for `rec` and `sox`) |
| 18 | + format: `S16_LE`, // Encoding type. (only for `arecord`) |
| 19 | + rate: 16000, // Sample rate. |
| 20 | + type: `wav`, // Format type. |
| 21 | + |
| 22 | + // Following options only available when using `rec` or `sox`. |
| 23 | + silence: 2, // Duration of silence in seconds before it stops recording. |
| 24 | + thresholdStart: 0.5, // Silence threshold to start recording. |
| 25 | + thresholdStop: 0.5, // Silence threshold to stop recording. |
| 26 | + keepSilence: true, // Keep the silence in the recording. |
| 27 | +}; |
| 28 | + |
| 29 | +const logger = console; |
| 30 | + |
| 31 | +export const record = async (request: Request, response: Response) => { |
| 32 | + // Create an instance. |
| 33 | + let recorder = new AudioRecorder(options, logger); |
| 34 | + // Constants. |
| 35 | + const DIRECTORY = "examples-recordings"; |
| 36 | + |
| 37 | + // Create path to write recordings to. |
| 38 | + if (!fs.existsSync(DIRECTORY)) { |
| 39 | + fs.mkdirSync(DIRECTORY); |
| 40 | + } |
| 41 | + |
| 42 | + // Create file path with random name. |
| 43 | + const fileName = path.join( |
| 44 | + DIRECTORY, |
| 45 | + Math.random() |
| 46 | + .toString(36) |
| 47 | + .replace(/[^0-9a-zA-Z]+/g, "") |
| 48 | + .concat(".wav") |
| 49 | + ); |
| 50 | + console.log("Writing new recording file at:", fileName); |
| 51 | + // Create write stream. |
| 52 | + const fileStream = fs.createWriteStream(fileName, { encoding: "binary" }); |
| 53 | + // Log information on the following events. |
| 54 | + recorder.on("error", function () { |
| 55 | + console.warn("Recording error."); |
| 56 | + }); |
| 57 | + recorder.on("end", function () { |
| 58 | + console.warn("Recording ended."); |
| 59 | + }); |
| 60 | + |
| 61 | + // Start and write to the file. |
| 62 | + |
| 63 | + recorder.start().stream().pipe(fileStream); |
| 64 | + console.log(recorder.stream()); |
| 65 | + // Keep process alive. |
| 66 | + process.stdin.resume(); |
| 67 | + console.warn("Press ctrl+c to exit."); |
20 | 68 | };
|
0 commit comments