Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sony.audio;
- import com.sony.domain.SampleRange;
- import com.sony.mpeg4.CAFChannelLabel;
- import com.sony.util.AudioSrcUtil;
- import com.sony.util.AudioUtil;
- import com.sony.util.FileUtil;
- import com.sony.util.Int24;
- import com.sony.util.MainUtil;
- import com.sony.util.NativeLibUtil;
- import com.sony.util.TimeUtil;
- import com.techlogger.dao.cache.AudioIndexCache;
- import org.apache.commons.io.FileUtils;
- import org.junit.Ignore;
- import org.junit.Test;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.nio.FloatBuffer;
- import java.nio.MappedByteBuffer;
- import java.nio.channels.FileChannel;
- import java.util.List;
- import static com.sony.util.AudioSrcUtil.uint16ToFloat;
- import static com.sony.util.AudioSrcUtil.uint24ToFloat;
- import static com.sony.util.TimeUtil.hmsms;
- import static org.junit.Assert.assertArrayEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertTrue;
- @Ignore
- public class AudioIndexFactoryTest {
- static {
- NativeLibUtil.setDefaultLibraryPath();
- MainUtil.initDebugLogger();
- }
- @Test
- public void testGimar() throws IOException {
- File file = new File("/Users/zhukov/Documents/gimar.wav");
- AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
- List<AudioIndexFactory.IndexEntry> fastAudioIndex = audioIndexFactory.makeRawIndex(file);
- System.out.println(fastAudioIndex);
- FastAudioIndex index = new FastAudioIndex(fastAudioIndex, AudioIndexFactory.samplesPerBin);
- AudioIndexCache.write(index, new File("/tmp/idx1"));
- }
- @Test
- public void testReindex24() throws IOException {
- AudioSrc src = AudioSrcUtil.srcFromFile(new File("/Users/zhukov/clients/sony/moneyball_2011_hd_16x9_178_2398_eng_OA9810_JPEG2000_a0.wav"));
- AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
- List<AudioIndexFactory.IndexEntry> indexEntries = audioIndexFactory.makeIndex(src);
- AudioIndexFactory.IndexEntry indexEntry = indexEntries.get(1059);
- System.out.println(indexEntries.size());
- }
- @Test
- public void testReindex() throws IOException {
- AudioSrc src = AudioSrcUtil.srcFromFile(new File("/Volumes/BigfootAudioSync/watchfolder/moneyball_2011_hd_16x9_178_2398_eng_OA9810_JPEG2000/c00.flac"));
- AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
- List<AudioIndexFactory.IndexEntry> indexEntries = audioIndexFactory.makeIndex(src);
- AudioIndexFactory.IndexEntry indexEntry = indexEntries.get(1059);
- System.out.println(indexEntries.size());
- }
- @Test
- public void testGimar16() throws IOException {
- AudioSrc src = AudioSrcUtil.srcFromFile(new File("/Users/zhukov/Documents/gimar16.wav"));
- AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
- List<AudioIndexFactory.IndexEntry> indexEntries = audioIndexFactory.makeIndex(src);
- System.out.println(indexEntries.size());
- }
- public static float[] readAll(AudioSrc src) {
- long length = src.length();
- assertTrue(length < Integer.MAX_VALUE && length > 0);
- int remaining = (int) (length - src.position());
- FloatBuffer samples = FloatBuffer.allocate(remaining);
- for (int i = 0; i < remaining; i++) {
- int nextSample = src.nextSample();
- if (nextSample != -1) {
- samples.put(uint16ToFloat(nextSample));
- } else {
- break;
- }
- }
- assertFalse(samples.hasRemaining());
- return samples.array();
- }
- @Test
- public void testGimar24Index() throws IOException {
- File file = new File("/Users/zhukov/Documents/gimar24.wav");
- AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
- List<AudioIndexFactory.IndexEntry> fastAudioIndex = audioIndexFactory.makeRawIndex(file);
- FastAudioIndex index = new FastAudioIndex(fastAudioIndex, AudioIndexFactory.samplesPerBin);
- AudioIndexCache.write(index, new File("/tmp/idx24"));
- }
- @Test
- public void testGimar24() throws IOException {
- File file = new File("/Users/zhukov/Documents/gimar24.wav");
- AudioSrc src = AudioSrcUtil.srcFromFile(file, CAFChannelLabel.Mono);
- float[] floats = readAll(src);
- src.seek(0);
- int[] ints = AudioUtil.readAll24(src);
- float[] floats24 = new float[ints.length];
- for (int i = 0; i < ints.length; i++) {
- floats24[i] = uint24ToFloat(ints[i]);
- }
- assertArrayEquals(floats, floats24, 0.0001f);
- ByteBuffer allocate = ByteBuffer.allocate(floats.length * 4);
- allocate.asFloatBuffer().put(floats);
- FileUtils.writeByteArrayToFile(new File("/tmp/floats24to16tofloat"), allocate.array());
- allocate.asFloatBuffer().put(floats24);
- FileUtils.writeByteArrayToFile(new File("/tmp/floats24tofloat"), allocate.array());
- // AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
- // List<AudioIndexFactory.IndexEntry> fastAudioIndex = audioIndexFactory.makeRawIndex(file);
- // System.out.println(fastAudioIndex);
- // FastAudioIndex index = new FastAudioIndex(fastAudioIndex, AudioIndexFactory.samplesPerBin);
- // AudioIndexCache.write(index, new File("/tmp/idx1"));
- }
- @Test
- public void testGimar2() throws IOException {
- File f = new File("/Users/zhukov/git/konform/floats");
- MappedByteBuffer map = new FileInputStream(f).getChannel().map(FileChannel.MapMode.READ_ONLY, 0, f.length());
- float[] floats = new float[(int) (f.length() / 4)];
- map.asFloatBuffer().get(floats);
- JTransformsFFT fft = new JTransformsFFT(4096);
- float[] spectrum = fft.spectrum(floats);
- System.out.println(spectrum);
- ByteBuffer allocate = ByteBuffer.allocate(spectrum.length * 4);
- allocate.asFloatBuffer().put(spectrum);
- FileUtils.writeByteArrayToFile(new File("/tmp/spectrum"), allocate.array());
- }
- File wav2 = FileUtil.tildeExpand("~/techlogger-audioproxies/TAXDRI_1AB_ENG_DME_PM/c00.wav");
- File wav1 = FileUtil.tildeExpand("~/Dropbox/testdata-junit/proxies/taxidriver_1976_hd_16x9_178_2398_english_1065_JPEG2000/c06.wav");
- static String toHumanReadableString(SampleRange sampleRange) {
- long start = sampleRange.getStart();
- long end = sampleRange.getEnd();
- long duration = sampleRange.duration();
- return hmsms(duration / 48) + " " + hmsms(start / 48) + " " + hmsms(end / 48);
- }
- static String toHumanReadableString(float[] array) {
- StringBuilder sb = new StringBuilder();
- sb.append('[');
- if (array.length >= 1) {
- sb.append(String.format("%.3f", array[0]));
- for (int i = 1; i < array.length; i++) {
- sb.append(String.format(", %.3f", array[i]));
- }
- }
- sb.append(']');
- return sb.toString();
- }
- @Test
- public void testHarry() throws Exception {
- AudioIndexFactory f = new AudioIndexFactory();
- FastAudioIndex dx = f.makeIndex(wav2);
- FastAudioIndex taxi = f.makeIndex(wav1);
- FastAudioIndex subIndex = dx.subIndex(7458442, 7538690);
- long nearestMatch = taxi.nearestMatch(subIndex);
- System.out.println(TimeUtil.hmsms(nearestMatch / 48));
- }
- @Test
- public void testFind1khz() throws Exception {
- AudioIndexFactory f = new AudioIndexFactory();
- FastAudioIndex taxi = f.makeIndex(wav1);
- File orig = new File("test-data/wav/1000hz.wav");
- FastAudioIndex khz = f.makeIndex(orig);
- long nearestMatch = taxi.nearestMatch(khz);
- System.out.println(TimeUtil.hmsms(nearestMatch / 48));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement