0

I have found this nice project https://github.com/AlekseyMartynov/shazam-for-real written in C# based on NAudio.

Basically, it listens to mic input and it searches in Shazam DB for matching songs. I am trying hard to adjust it to use wav files (about 5-10 seconds of recording) as input but I am not able to. I am new to audio programming.

 static ShazamResult CaptureAndTag() {
    var analysis = new Analysis();
    var finder = new LandmarkFinder(analysis);

    using(var capture = new WasapiCapture()) {
        var captureBuf = new BufferedWaveProvider(capture.WaveFormat) { ReadFully = false };

        capture.DataAvailable += (s, e) => {
            captureBuf.AddSamples(e.Buffer, 0, e.BytesRecorded);
        };

        capture.StartRecording();

        using(var resampler = new MediaFoundationResampler(captureBuf, new WaveFormat(Analysis.SAMPLE_RATE, 16, 1))) {
            var sampleProvider = resampler.ToSampleProvider();
            var retryMs = 3000;
            var tagId = Guid.NewGuid().ToString();

            while(true) {
                while(captureBuf.BufferedDuration.TotalSeconds < 1)
                    Thread.Sleep(100);

                analysis.ReadChunk(sampleProvider);

                if(analysis.StripeCount > 2 * LandmarkFinder.RADIUS_TIME)
                    finder.Find(analysis.StripeCount - LandmarkFinder.RADIUS_TIME - 1);

                if(analysis.ProcessedMs >= retryMs) {
                    //new Painter(analysis, finder).Paint("c:/temp/spectro.png");
                    //new Synthback(analysis, finder).Synth("c:/temp/synthback.raw");

                    var sigBytes = Sig.Write(Analysis.SAMPLE_RATE, analysis.ProcessedSamples, finder);
                    var result = ShazamApi.SendRequest(tagId, analysis.ProcessedMs, sigBytes).GetAwaiter().GetResult();
                    if(result.Success)
                        return result;

                    retryMs = result.RetryMs;
                    if(retryMs == 0)
                        return result;
                }
            }
        }
    }
}

Above is the main code where the magic happens. I have tried to get the audio part of the wav file and shovel it into captureBuf (very naive, I know) and removed the loop while(true) but it failed. Am I going the wrong way? First issue I noticed is that even if my wav file is 5 seconds long, the value of

analysis.ProcessedMs >= retryMs

is always 8ms. Clearly, I have no idea what I am doing.

Any good piece of advice on how to proceed? Thanks!

Nik Dell
  • 53
  • 7
  • Please check my comment here - https://github.com/AlekseyMartynov/shazam-for-real/issues/6#issuecomment-1682429717 – amartynov Aug 17 '23 at 14:56

0 Answers0