Advertisement
Guest User

Untitled

a guest
May 24th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.97 KB | None | 0 0
  1. package com.sony.audio;
  2.  
  3. import com.sony.domain.SampleRange;
  4. import com.sony.mpeg4.CAFChannelLabel;
  5. import com.sony.util.AudioSrcUtil;
  6. import com.sony.util.AudioUtil;
  7. import com.sony.util.FileUtil;
  8. import com.sony.util.Int24;
  9. import com.sony.util.MainUtil;
  10. import com.sony.util.NativeLibUtil;
  11. import com.sony.util.TimeUtil;
  12. import com.techlogger.dao.cache.AudioIndexCache;
  13. import org.apache.commons.io.FileUtils;
  14. import org.junit.Ignore;
  15. import org.junit.Test;
  16.  
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.nio.ByteBuffer;
  21. import java.nio.FloatBuffer;
  22. import java.nio.MappedByteBuffer;
  23. import java.nio.channels.FileChannel;
  24. import java.util.List;
  25.  
  26. import static com.sony.util.AudioSrcUtil.uint16ToFloat;
  27. import static com.sony.util.AudioSrcUtil.uint24ToFloat;
  28. import static com.sony.util.TimeUtil.hmsms;
  29. import static org.junit.Assert.assertArrayEquals;
  30. import static org.junit.Assert.assertFalse;
  31. import static org.junit.Assert.assertTrue;
  32.  
  33. @Ignore
  34. public class AudioIndexFactoryTest {
  35. static {
  36. NativeLibUtil.setDefaultLibraryPath();
  37. MainUtil.initDebugLogger();
  38. }
  39.  
  40. @Test
  41. public void testGimar() throws IOException {
  42. File file = new File("/Users/zhukov/Documents/gimar.wav");
  43. AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
  44. List<AudioIndexFactory.IndexEntry> fastAudioIndex = audioIndexFactory.makeRawIndex(file);
  45. System.out.println(fastAudioIndex);
  46. FastAudioIndex index = new FastAudioIndex(fastAudioIndex, AudioIndexFactory.samplesPerBin);
  47. AudioIndexCache.write(index, new File("/tmp/idx1"));
  48. }
  49.  
  50. @Test
  51. public void testReindex24() throws IOException {
  52. AudioSrc src = AudioSrcUtil.srcFromFile(new File("/Users/zhukov/clients/sony/moneyball_2011_hd_16x9_178_2398_eng_OA9810_JPEG2000_a0.wav"));
  53. AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
  54. List<AudioIndexFactory.IndexEntry> indexEntries = audioIndexFactory.makeIndex(src);
  55. AudioIndexFactory.IndexEntry indexEntry = indexEntries.get(1059);
  56. System.out.println(indexEntries.size());
  57. }
  58.  
  59. @Test
  60. public void testReindex() throws IOException {
  61. AudioSrc src = AudioSrcUtil.srcFromFile(new File("/Volumes/BigfootAudioSync/watchfolder/moneyball_2011_hd_16x9_178_2398_eng_OA9810_JPEG2000/c00.flac"));
  62. AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
  63.  
  64. List<AudioIndexFactory.IndexEntry> indexEntries = audioIndexFactory.makeIndex(src);
  65. AudioIndexFactory.IndexEntry indexEntry = indexEntries.get(1059);
  66. System.out.println(indexEntries.size());
  67. }
  68.  
  69. @Test
  70. public void testGimar16() throws IOException {
  71. AudioSrc src = AudioSrcUtil.srcFromFile(new File("/Users/zhukov/Documents/gimar16.wav"));
  72. AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
  73.  
  74. List<AudioIndexFactory.IndexEntry> indexEntries = audioIndexFactory.makeIndex(src);
  75. System.out.println(indexEntries.size());
  76.  
  77. }
  78.  
  79. public static float[] readAll(AudioSrc src) {
  80. long length = src.length();
  81. assertTrue(length < Integer.MAX_VALUE && length > 0);
  82. int remaining = (int) (length - src.position());
  83. FloatBuffer samples = FloatBuffer.allocate(remaining);
  84. for (int i = 0; i < remaining; i++) {
  85. int nextSample = src.nextSample();
  86. if (nextSample != -1) {
  87. samples.put(uint16ToFloat(nextSample));
  88. } else {
  89. break;
  90. }
  91. }
  92. assertFalse(samples.hasRemaining());
  93. return samples.array();
  94. }
  95.  
  96. @Test
  97. public void testGimar24Index() throws IOException {
  98. File file = new File("/Users/zhukov/Documents/gimar24.wav");
  99. AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
  100. List<AudioIndexFactory.IndexEntry> fastAudioIndex = audioIndexFactory.makeRawIndex(file);
  101. FastAudioIndex index = new FastAudioIndex(fastAudioIndex, AudioIndexFactory.samplesPerBin);
  102. AudioIndexCache.write(index, new File("/tmp/idx24"));
  103.  
  104. }
  105.  
  106. @Test
  107. public void testGimar24() throws IOException {
  108. File file = new File("/Users/zhukov/Documents/gimar24.wav");
  109. AudioSrc src = AudioSrcUtil.srcFromFile(file, CAFChannelLabel.Mono);
  110. float[] floats = readAll(src);
  111. src.seek(0);
  112. int[] ints = AudioUtil.readAll24(src);
  113. float[] floats24 = new float[ints.length];
  114. for (int i = 0; i < ints.length; i++) {
  115. floats24[i] = uint24ToFloat(ints[i]);
  116. }
  117. assertArrayEquals(floats, floats24, 0.0001f);
  118. ByteBuffer allocate = ByteBuffer.allocate(floats.length * 4);
  119. allocate.asFloatBuffer().put(floats);
  120. FileUtils.writeByteArrayToFile(new File("/tmp/floats24to16tofloat"), allocate.array());
  121.  
  122. allocate.asFloatBuffer().put(floats24);
  123. FileUtils.writeByteArrayToFile(new File("/tmp/floats24tofloat"), allocate.array());
  124.  
  125.  
  126. // AudioIndexFactory audioIndexFactory = new AudioIndexFactory();
  127. // List<AudioIndexFactory.IndexEntry> fastAudioIndex = audioIndexFactory.makeRawIndex(file);
  128. // System.out.println(fastAudioIndex);
  129. // FastAudioIndex index = new FastAudioIndex(fastAudioIndex, AudioIndexFactory.samplesPerBin);
  130. // AudioIndexCache.write(index, new File("/tmp/idx1"));
  131.  
  132. }
  133.  
  134. @Test
  135. public void testGimar2() throws IOException {
  136. File f = new File("/Users/zhukov/git/konform/floats");
  137. MappedByteBuffer map = new FileInputStream(f).getChannel().map(FileChannel.MapMode.READ_ONLY, 0, f.length());
  138. float[] floats = new float[(int) (f.length() / 4)];
  139. map.asFloatBuffer().get(floats);
  140. JTransformsFFT fft = new JTransformsFFT(4096);
  141. float[] spectrum = fft.spectrum(floats);
  142. System.out.println(spectrum);
  143. ByteBuffer allocate = ByteBuffer.allocate(spectrum.length * 4);
  144. allocate.asFloatBuffer().put(spectrum);
  145. FileUtils.writeByteArrayToFile(new File("/tmp/spectrum"), allocate.array());
  146. }
  147.  
  148.  
  149. File wav2 = FileUtil.tildeExpand("~/techlogger-audioproxies/TAXDRI_1AB_ENG_DME_PM/c00.wav");
  150. File wav1 = FileUtil.tildeExpand("~/Dropbox/testdata-junit/proxies/taxidriver_1976_hd_16x9_178_2398_english_1065_JPEG2000/c06.wav");
  151.  
  152. static String toHumanReadableString(SampleRange sampleRange) {
  153. long start = sampleRange.getStart();
  154. long end = sampleRange.getEnd();
  155. long duration = sampleRange.duration();
  156. return hmsms(duration / 48) + " " + hmsms(start / 48) + " " + hmsms(end / 48);
  157. }
  158.  
  159. static String toHumanReadableString(float[] array) {
  160. StringBuilder sb = new StringBuilder();
  161. sb.append('[');
  162. if (array.length >= 1) {
  163. sb.append(String.format("%.3f", array[0]));
  164. for (int i = 1; i < array.length; i++) {
  165. sb.append(String.format(", %.3f", array[i]));
  166. }
  167. }
  168. sb.append(']');
  169. return sb.toString();
  170. }
  171.  
  172. @Test
  173. public void testHarry() throws Exception {
  174. AudioIndexFactory f = new AudioIndexFactory();
  175. FastAudioIndex dx = f.makeIndex(wav2);
  176. FastAudioIndex taxi = f.makeIndex(wav1);
  177. FastAudioIndex subIndex = dx.subIndex(7458442, 7538690);
  178. long nearestMatch = taxi.nearestMatch(subIndex);
  179. System.out.println(TimeUtil.hmsms(nearestMatch / 48));
  180.  
  181. }
  182.  
  183. @Test
  184. public void testFind1khz() throws Exception {
  185. AudioIndexFactory f = new AudioIndexFactory();
  186. FastAudioIndex taxi = f.makeIndex(wav1);
  187. File orig = new File("test-data/wav/1000hz.wav");
  188. FastAudioIndex khz = f.makeIndex(orig);
  189. long nearestMatch = taxi.nearestMatch(khz);
  190. System.out.println(TimeUtil.hmsms(nearestMatch / 48));
  191.  
  192. }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement