|
@@ -6,6 +6,7 @@ import be.tarsos.dsp.AudioProcessor;
|
|
|
import be.tarsos.dsp.SilenceDetector;
|
|
|
import be.tarsos.dsp.beatroot.BeatRootOnsetEventHandler;
|
|
|
import be.tarsos.dsp.io.PipedAudioStream;
|
|
|
+import be.tarsos.dsp.io.TarsosDSPAudioFormat;
|
|
|
import be.tarsos.dsp.io.TarsosDSPAudioInputStream;
|
|
|
import be.tarsos.dsp.io.jvm.AudioDispatcherFactory;
|
|
|
import be.tarsos.dsp.onsets.ComplexOnsetDetector;
|
|
@@ -15,6 +16,7 @@ import com.ym.mec.biz.dal.dao.SysMusicScoreDao;
|
|
|
import com.ym.mec.biz.dal.entity.SysMusicScore;
|
|
|
import com.ym.mec.biz.service.SoundService;
|
|
|
import com.ym.mec.common.exception.BizException;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -24,9 +26,13 @@ import javax.sound.sampled.AudioFileFormat;
|
|
|
import javax.sound.sampled.AudioFormat;
|
|
|
import javax.sound.sampled.AudioSystem;
|
|
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
@@ -53,7 +59,7 @@ public class SoundServiceImpl implements SoundService {
|
|
|
List<Double> times = new ArrayList<>();
|
|
|
int size = 256;
|
|
|
int overlap = 128;
|
|
|
- AudioDispatcher dispatcher = StringUtils.isBlank(url)?getFromByteArray(bytes, size, overlap):getFromUtl(url, size, overlap);
|
|
|
+ AudioDispatcher dispatcher = StringUtils.isBlank(url)?getFromByteArray(bytes, size, overlap):getFromFile(url, size, overlap);
|
|
|
|
|
|
ComplexOnsetDetector detector = new ComplexOnsetDetector(size);
|
|
|
BeatRootOnsetEventHandler handler = new BeatRootOnsetEventHandler();
|
|
@@ -83,7 +89,7 @@ public class SoundServiceImpl implements SoundService {
|
|
|
int size = 2048;
|
|
|
int overlap = 0;
|
|
|
final SilenceDetector silenceDetecor = new SilenceDetector();
|
|
|
- AudioDispatcher dispatcher = StringUtils.isBlank(url)?getFromByteArray(bytes, size, overlap):getFromUtl(url, size, overlap);
|
|
|
+ AudioDispatcher dispatcher = StringUtils.isBlank(url)?getFromByteArray(bytes, size, overlap):getFromFile(url, size, overlap);
|
|
|
dispatcher.addAudioProcessor(silenceDetecor);
|
|
|
dispatcher.addAudioProcessor(new AudioProcessor() {
|
|
|
@Override
|
|
@@ -106,8 +112,11 @@ public class SoundServiceImpl implements SoundService {
|
|
|
return dispatcher;
|
|
|
}
|
|
|
|
|
|
- private AudioDispatcher getFromUtl(String url, int size, int overlap) throws UnsupportedAudioFileException, IOException {
|
|
|
- AudioDispatcher dispatcher = AudioDispatcherFactory.fromURL(new URL(url), size, overlap);
|
|
|
+ private AudioDispatcher getFromFile(String url, int size, int overlap) throws UnsupportedAudioFileException, IOException {
|
|
|
+ PipedAudioStream file = new PipedAudioStream(url);
|
|
|
+ TarsosDSPAudioInputStream stream = file.getMonoStream(44100,0);
|
|
|
+
|
|
|
+ AudioDispatcher dispatcher = new AudioDispatcher(stream, size, overlap);
|
|
|
return dispatcher;
|
|
|
}
|
|
|
|
|
@@ -119,15 +128,18 @@ public class SoundServiceImpl implements SoundService {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- AudioFileFormat sourceFile = AudioSystem.getAudioFileFormat(new URL(sysMusicScore.getUrl()));
|
|
|
- double l_s = sourceFile.getFrameLength()/sourceFile.getFormat().getFrameRate();
|
|
|
- System.out.printf("源长度:%.2f \n", l_s);
|
|
|
- AudioFileFormat recordFile = AudioSystem.getAudioFileFormat(record.getInputStream());
|
|
|
- double r_s = sourceFile.getFrameLength()/sourceFile.getFormat().getFrameRate();
|
|
|
- System.out.printf("录音长度:%.2f \n", r_s);
|
|
|
+ URL url = new URL(sysMusicScore.getUrl());
|
|
|
+ String filePath = FileUtils.getTempDirectoryPath()+ System.currentTimeMillis() + ".mp3";
|
|
|
+ File f = new File(filePath);
|
|
|
+ FileUtils.copyURLToFile(url, f);
|
|
|
+
|
|
|
+// AudioSystem.getAudioFileFormat(loadedFile);
|
|
|
+
|
|
|
+ double l_s = 0;
|
|
|
+ double l_r = 0;
|
|
|
|
|
|
//相似度
|
|
|
- List<Double> pitchs_s = soundPressureLevelExtractor(null, sysMusicScore.getUrl());
|
|
|
+ List<Double> pitchs_s = soundPressureLevelExtractor(null, filePath);
|
|
|
List<Double> pitchs_r = soundPressureLevelExtractor(record.getBytes(), null);
|
|
|
|
|
|
int maxLength = pitchs_s.size();
|
|
@@ -151,7 +163,7 @@ public class SoundServiceImpl implements SoundService {
|
|
|
System.out.printf("音准:%.2f \r\n", intonation);
|
|
|
|
|
|
//节奏
|
|
|
- List<Double> times_s = beatExtractor(null, sysMusicScore.getUrl());
|
|
|
+ List<Double> times_s = beatExtractor(null, filePath);
|
|
|
List<Double> times_r = beatExtractor(record.getBytes(), null);
|
|
|
|
|
|
float sameTimes = 0;
|