I am trying to convert my API to accept an audio file instead of a string but after looking into it I can not find a suitable example on how to do so.
At the moment the Speech-To-Text service is run locally but I want to move this to the server. The API call to the wit.ai services I already made. The thing left is to make the API accept a audio file (audio will always be .wav)
If anyone has some suggestions let me know I am stuck on this
[Produces("application/json")]
[Route("api")]
public class CommandApiController : Controller
{
// constructor
[HttpPost]
public async Task<IActionResult> ProcessCommandAsync([FromBody]string command)
{
// Testing SpeechToText method
string path = @"C:\Users\rickk\Desktop\SmartSpeaker\agenda.wav";
// Overridden for now
command = await CovnvertSpeechToTextApiCall(new ByteArrayContent(System.IO.File.ReadAllBytes(path)));
// Logic
}
async Task<string> CovnvertSpeechToTextApiCall(ByteArrayContent content)
{
var httpClient = new HttpClient();
content.Headers.ContentType = MediaTypeHeaderValue.Parse("audio/wav");
// Wit.ai server token
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
var httpResponseMessage = await httpClient.PostAsync("https://api.wit.ai/speech", content);
if (httpResponseMessage.IsSuccessStatusCode)
{
var response = await httpResponseMessage.Content.ReadAsStringAsync();
var modeldata = Newtonsoft.Json.JsonConvert.DeserializeObject<Model.DeserializedJsonDataModel>(response);
return modeldata._text;
}
else
{
return null;
}
}
}
Thanks for the comment.
Not exactly what I was looking for but that is my fault for not explaining it correctly. Here is what I have found as a solution.
[Produces("application/json")]
[Route("api/audio")]
[HttpPost]
public async Task<IActionResult> ProcessCommandAsync([FromForm]IFormFile command)
{
if(command.ContentType != "audio/wav" && command.ContentType != "audio/wave" || command.Length < 1)
{
return BadRequest();
}
var text = await CovnvertSpeechToTextApiCall(ConvertToByteArrayContent(command));
return Ok(FormulateResponse(text));
}
private ByteArrayContent ConvertToByteArrayContent(IFormFile audofile)
{
byte[] data;
using (var br = new BinaryReader(audofile.OpenReadStream()))
{
data = br.ReadBytes((int) audofile.OpenReadStream().Length);
}
return new ByteArrayContent(data);
}