Skip to content

Instantly share code, notes, and snippets.

@JeffBrand JeffBrand/run.cs
Last active Nov 6, 2018

What would you like to do?
Azure Function for Transcribing Speech
#r "Microsoft.WindowsAzure.Storage"
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.CognitiveServices.SpeechRecognition;
public class ReportItem{
public string id {get;set;}
public string audioFile {get;set;}
public string audioText {get;set;}
public class Report
public string id {get;set;}
public string title {get;set;}
public List<ReportItem> items {get; set;}
public static async Task<HttpResponseMessage> Run(Report report, IAsyncCollector<object> outputDocument, TraceWriter log)
log.Info($"C# HTTP trigger function processing a request...");
//var json = await req.Content.ReadAsStringAsync();
//var report = Newtonsoft.Json.JsonConvert.DeserializeObject<report>(json);
if (report?.items?.Count > 0) {
log.Info($"{report.items.Count} items found.");
var storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(AzureStorageAccount.ConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(AzureStorageAccount.ContainerName);
var audioItems = from i in report.items where !string.IsNullOrWhiteSpace(i.audioFile) select i;
foreach(var item in audioItems)
log.Info($"Processing {}...");
var audioHandler = new AudioHandler();
item.audioText = await audioHandler.ProcessBlob(container, item.audioFile, log);
log.Info($"[{item.audioFile}] transcribed: {item.audioText}");
await outputDocument.AddAsync(report);
return new HttpResponseMessage(HttpStatusCode.OK);
return new HttpResponseMessage(HttpStatusCode.BadRequest);
public abstract class AzureStorageAccount
public static string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=<your-account-name>;AccountKey=<your-account-key>";
public static string ContainerName = "upload";
public class AudioHandler {
TaskCompletionSource<string> _tcs;
static DataRecognitionClient _dataClient;
static AudioHandler()
_dataClient = SpeechRecognitionServiceFactory.CreateDataClient(
public AudioHandler()
_dataClient.OnResponseReceived += responseHandler;
private void responseHandler(object sender, SpeechResponseEventArgs args){
if (args.PhraseResponse.Results.Length == 0)
_tcs.SetResult("ERROR: Bad audio");
var client = sender as DataRecognitionClient;
client.OnResponseReceived -= responseHandler;
public Task<string> ProcessBlob(Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container, string blobName, TraceWriter log)
_tcs = new TaskCompletionSource<string>();
var mem = new System.IO.MemoryStream();
log.Info("Ready to read blob");
var blockBlob = container.GetBlockBlobReference(blobName);
log.Info("Blob read - size=" + mem.Length);
mem.Position = 0;
int bytesRead = 0;
byte[] buffer = new byte[1024];
bytesRead = mem.Read(buffer, 0, buffer.Length);
_dataClient.SendAudio(buffer, bytesRead);
while (bytesRead > 0);
log.Info("Done Reading bytes");
return _tcs.Task;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.