Created
July 14, 2017 07:40
-
-
Save ikumasa/f160abaec52309df6c6869bc5311eec7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.gennai.audio_analyzer.analysis; | |
| import java.io.IOException; | |
| import javax.sound.sampled.LineUnavailableException; | |
| import javax.sound.sampled.UnsupportedAudioFileException; | |
| import org.gennai.audio_analyzer.analysis.AudioTest.Audio; | |
| import org.gennai.audio_analyzer.analysis.audio.AudioSamples; | |
| import org.gennai.audio_analyzer.analysis.config.FingerprintConfig; | |
| import org.gennai.audio_analyzer.analysis.dao.IModelReference; | |
| import org.gennai.audio_analyzer.analysis.dao.data.SoundData; | |
| import org.gennai.audio_analyzer.analysis.inmemory.InMemoryModelService; | |
| import org.gennai.audio_analyzer.analysis.inmemory.RAMStorage; | |
| public class ShazamSample { | |
| private FingerprintConfig fingerprintConfig; | |
| private InMemoryModelService modelService; | |
| private Analyzer analyzer; | |
| private ShazamSample() { | |
| // 設定 | |
| fingerprintConfig = new FingerprintConfig(); | |
| // 4に固定 | |
| fingerprintConfig.getSpectrogramConfig().setLogBins(4); | |
| /* | |
| fingerprintConfig.setNormalizeSignal(false); | |
| fingerprintConfig.getSpectrogramConfig().setWdftSize(2048); | |
| fingerprintConfig.getSpectrogramConfig().setOverlap(64); | |
| */ | |
| RAMStorage storage = new RAMStorage( | |
| fingerprintConfig.getHashingConfig().getNumberOfLSHTables()); | |
| modelService = new InMemoryModelService(storage); | |
| analyzer = new Analyzer(); | |
| } | |
| private void insertHashData(String wavFile) | |
| throws UnsupportedAudioFileException, IOException, LineUnavailableException { | |
| // WAVEファイル読み込み | |
| Audio audio = AudioTest.readWave("../samples4/nom1.wav"); | |
| AudioSamples audioSamples = new AudioSamples(audio.getData(), audio.getOrigin(), | |
| (int) audio.getFormat().getSampleRate()); | |
| // ハッシュを作成 | |
| long[] hashes = analyzer.spectrogram(audioSamples, fingerprintConfig.getSpectrogramConfig()); | |
| // メタデータをモデルに登録 | |
| double length = (double) audio.getData().length / audio.getFormat().getSampleRate() | |
| / audio.getFormat().getChannels(); | |
| IModelReference<?> soundReference = modelService.saveSound(new SoundData(length)); | |
| // ハッシュをモデルに登録 | |
| modelService.insertDataPointsForSound(hashes, soundReference); | |
| } | |
| private void query(String wavFile) | |
| throws UnsupportedAudioFileException, IOException, LineUnavailableException { | |
| // WAVEファイル読み込み | |
| Audio audio = AudioTest.readWave("../samples4/nom1.wav"); | |
| AudioSamples audioSamples = new AudioSamples(audio.getData(), audio.getOrigin(), | |
| (int) audio.getFormat().getSampleRate()); | |
| // ハッシュを作成 | |
| long[] hashes = analyzer.spectrogram(audioSamples, fingerprintConfig.getSpectrogramConfig()); | |
| // モデルを検索 | |
| MatchResult ret = analyzer.query(hashes, modelService); | |
| // ヒットしたか? | |
| if (ret.containsMatches()) { | |
| // ベストマッチ | |
| System.out.printf("best id: %d, score: %d\n", ret.getBestMatch().getSoundRefrence().getId(), | |
| ret.getBestMatch().getMostOffsetCount()); | |
| // マッチリスト | |
| for (MatchEntry entry : ret.getMatchEntries()) { | |
| System.out.printf("id: %d, score: %d\n", entry.getSoundRefrence().getId(), | |
| entry.getMostOffsetCount()); | |
| } | |
| } else { | |
| System.out.println("no match"); | |
| } | |
| } | |
| public static void main(String[] args) | |
| throws UnsupportedAudioFileException, IOException, LineUnavailableException { | |
| ShazamSample shazamSample = new ShazamSample(); | |
| shazamSample.insertHashData("../samples4/nom1.wav"); | |
| shazamSample.query("../samples4/nom1.wav"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment