Skip to content

Commit 470dc82

Browse files
author
ftreml
committed
start/end parameters for audio file conversion
1 parent fd6d91b commit 470dc82

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ This project is standing on the shoulders of giants.
144144

145145
## Changelog
146146

147+
### 2020-03-05
148+
149+
* Optional _start_/_end_ parameters for audio file conversion to trim an audio file by time codes formatted as mm:ss (_01:32_)
150+
147151
### 2020-02-22
148152

149153
* Additional endpoint to calculate the Word Error Rate (Levenshtein Distance) between two texts

frontend/src/convert/sox.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ const { spawn } = require('child_process')
44
const uuidv1 = require('uuid/v1')
55
const debug = require('debug')('botium-speech-processing-convert-sox')
66

7-
const runsox = (cmdLine, inputBuffer) => {
7+
const runsox = (cmdLine, { inputBuffer, start, end }) => {
88
return new Promise((resolve, reject) => {
99
const output = `/tmp/${uuidv1()}.wav`
10-
11-
const cmdLineSox = Mustache.render(cmdLine, { output })
10+
11+
let cmdLineSox = Mustache.render(cmdLine, { output })
12+
if (start && end) {
13+
cmdLineSox = `${cmdLineSox} trim ${start} ${end}`
14+
} else if (start && !end) {
15+
cmdLineSox = `${cmdLineSox} trim ${start}`
16+
} else if (!start && end) {
17+
cmdLineSox = `${cmdLineSox} trim 0 ${end}`
18+
}
1219
debug(`cmdLineSox: ${cmdLineSox}`)
1320
const cmdLineSoxParts = cmdLineSox.split(' ')
1421
const sox = spawn(cmdLineSoxParts[0], cmdLineSoxParts.slice(1))

frontend/src/routes.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const mkdirp = require('mkdirp')
44
const crypto = require('crypto')
55
const express = require('express')
66
const sanitize = require('sanitize-filename')
7-
const { runsox } = require('./convert/sox.js')
7+
const { runsox } = require('./convert/sox')
88
const { wer } = require('./utils')
99
const debug = require('debug')('botium-speech-processing-routes')
1010

@@ -234,6 +234,18 @@ router.get('/api/tts/:language', async (req, res, next) => {
234234
* required: true
235235
* schema:
236236
* type: string
237+
* - name: start
238+
* description: Start Timecode within audio stream (01:32)
239+
* in: query
240+
* schema:
241+
* type: string
242+
* pattern: '^([0-5][0-9]):([0-5][0-9])$'
243+
* - name: end
244+
* description: End Timecode within audio stream (02:48)
245+
* in: query
246+
* schema:
247+
* type: string
248+
* pattern: '^([0-5][0-9]):([0-5][0-9])$'
237249
* requestBody:
238250
* description: Audio file
239251
* content:
@@ -264,7 +276,7 @@ router.post('/api/convert/:profile', async (req, res, next) => {
264276
}
265277

266278
try {
267-
const outputBuffer = await runsox(process.env[envVarSox], req.body)
279+
const outputBuffer = await runsox(process.env[envVarSox], { inputBuffer: req.body, start: req.query.start, end: req.query.end })
268280
res.writeHead(200, {
269281
'Content-disposition': `attachment; filename="${process.env[envVarOutput]}"`,
270282
'Content-Length': outputBuffer.length

frontend/src/swagger.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,24 @@
184184
"schema": {
185185
"type": "string"
186186
}
187+
},
188+
{
189+
"name": "start",
190+
"description": "Start Timecode within audio stream (01:32)",
191+
"in": "query",
192+
"schema": {
193+
"type": "string",
194+
"pattern": "^([0-5][0-9]):([0-5][0-9])$"
195+
}
196+
},
197+
{
198+
"name": "end",
199+
"description": "End Timecode within audio stream (02:48)",
200+
"in": "query",
201+
"schema": {
202+
"type": "string",
203+
"pattern": "^([0-5][0-9]):([0-5][0-9])$"
204+
}
187205
}
188206
],
189207
"requestBody": {

0 commit comments

Comments
 (0)