Skip to content

Instantly share code, notes, and snippets.

@AddictedCS
Created May 27, 2020 11:18
Show Gist options
  • Save AddictedCS/8ce2ec2614f1f7ef70b68dbf6b38e331 to your computer and use it in GitHub Desktop.
Save AddictedCS/8ce2ec2614f1f7ef70b68dbf6b38e331 to your computer and use it in GitHub Desktop.
Realtime Command usage for SoundFingerprinting
public class RealtimeCommandSample
{
public async Task Run(IModelService modelService, CancellationToken cancellationToken)
{
var collection = new BlockingCollection<AudioSamples>();
var producerTask = Task.Run(() => GetRealtimeData(collection, cancellationToken), cancellationToken);
var consumerTask = QueryRealtimeData(modelService, collection, 5d, cancellationToken);
var finished = await Task.WhenAny(producerTask, consumerTask);
await finished.ConfigureAwait(false);
}
public void GetRealtimeData(BlockingCollection<AudioSamples> producer, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
// get realtime data in chunks
// each chunk has to contain 10240 samples, at 5512Hz, meaning each sample has to be 1.857 seconds length
// source of the realtime data is application dependent
float[] audioSamples = new float[10240];
var chunk = new AudioSamples(audioSamples, "", 5512);
producer.Add(chunk, cancellationToken);
}
producer.CompleteAdding();
}
public Task QueryRealtimeData(
IModelService modelService,
BlockingCollection<AudioSamples> source,
double lengthOfASuccessfulMatch,
CancellationToken cancellationToken)
{
var realtimeQuery = QueryCommandBuilder.Instance.BuildRealtimeQueryCommand()
.From(source)
.WithRealtimeQueryConfig(config =>
{
config.ResultEntryFilter = new QueryMatchLengthFilter(lengthOfASuccessfulMatch);
config.SuccessCallback = entry =>
{
Console.WriteLine($"Got successful match for {entry.Track.Id}");
};
return config;
})
.UsingServices(modelService)
.Query(cancellationToken);
return realtimeQuery;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment