private void AudioStream_OnBroadcast(object sender, EventArgs <byte[]> e) { switch (state) { case State.WaitLearn: if (IsSilence(e.Value)) { break; } Console.WriteLine("Learning!"); StopAudio(); PlayAudio("intro.wav"); time = 0; spectrum.Clear(); spectrum.AppendSample(e.Value, time); state = State.Learning; break; case State.Learning: if (time++ >= 100) { Console.WriteLine("Done Learning!"); StopAudio(); PlayAudio("ready.mp3"); var fps = spectrum.GetFingerprints(learningAlbumId); database.AddFingerprints(fps); database.Save(); state = State.Idle; break; } else { spectrum.AppendSample(e.Value, time); } break; case State.WaitMatch: if (IsSilence(e.Value)) { break; } Console.WriteLine("Matching!"); PlayAudio("intro.wav"); time = 0; spectrum.Clear(); spectrum.AppendSample(e.Value, time); silent = false; state = State.Matching; break; case State.Matching: if (time++ >= 70) { StopAudio(); // stop intro var mfps = spectrum.GetFingerprints(learningAlbumId); var albumId = database.GetMatch(mfps); if (albumId != null) { Console.WriteLine("Match album " + albumId); PlayAlbum(albumId + ".m3u"); state = State.Playing; } else { Console.WriteLine("No match"); state = State.WaitMatch; } break; } spectrum.AppendSample(e.Value, time); break; case State.Playing: if (silent) { if (!IsSilence(e.Value)) // silent -> not silent { player.SetVolume(1, 1); silent = false; } else // still silent { if (stopwatch.ElapsedMilliseconds - silenceStarted > 5000) { Console.WriteLine("Stopping after silence!"); StopAudio(); state = State.WaitMatch; } } } else { if (IsSilence(e.Value)) // going silent for the first time { silent = true; silenceStarted = stopwatch.ElapsedMilliseconds; } } break; case State.Idle: break; } }