Browse Source

优化作品合成播放器解析和播放逻辑

Pq 6 months ago
parent
commit
bfc54d328d

+ 51 - 31
musicMerge/src/main/java/com/cooleshow/musicmerge/player/MergeTrackManager.java

@@ -32,6 +32,9 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
  * Author by pq, Date on 2022/10/26.
  * Author by pq, Date on 2022/10/26.
  */
  */
 public class MergeTrackManager {
 public class MergeTrackManager {
+    public static final int READ_STATUS_END = -1;
+    public static final int READ_STATUS_NOT_START = -2;
+
     public static final int PLAY_COMPLETED = 1001;
     public static final int PLAY_COMPLETED = 1001;
     public static final int MAX_FILE_COUNT = 2;
     public static final int MAX_FILE_COUNT = 2;
 
 
@@ -61,7 +64,7 @@ public class MergeTrackManager {
     private long playProgress = -1;
     private long playProgress = -1;
     private int minBufferSize;
     private int minBufferSize;
     private byte[][] mAllAudioData;
     private byte[][] mAllAudioData;
-    private byte[][] offsetAudioData;
+    //    private byte[][] offsetAudioData;
     public static final int RATE = 44100;
     public static final int RATE = 44100;
 
 
     public static final float MAX_VOLUME = 1f;
     public static final float MAX_VOLUME = 1f;
@@ -307,12 +310,28 @@ public class MergeTrackManager {
     }
     }
 
 
     private int countBytesByPosition(int bytesReadStart, byte[][] allAudioData2) {
     private int countBytesByPosition(int bytesReadStart, byte[][] allAudioData2) {
-        int[] results = new int[offsetAudioData.length];
-        for (int i = 0; i < offsetAudioData.length; i++) {
+        int[] results = new int[mAllAudioData.length];
+        for (int i = 0; i < mAllAudioData.length; i++) {
             int result = 0;
             int result = 0;
             byte[] buffer = new byte[minBufferSize];
             byte[] buffer = new byte[minBufferSize];
-            LOG.i(TAG, "source:" + offsetAudioData[i].length + "--bytesReadStart:" + bytesReadStart);
-            result = splitByteArray(offsetAudioData[i], bytesReadStart, buffer);
+            long timeOffset = positionOffset[i];
+            int startPos = bytesReadStart * minBufferSize;
+
+            LOG.i(TAG, "startPos:" + startPos);
+            LOG.i(TAG, "source:" + mAllAudioData[i].length + "--bytesReadStart:" + bytesReadStart);
+            //希望偏移的位置量
+            int byteOffset = getLengthFromDuration(timeOffset);
+            LOG.i(TAG, "timeOffset:" + timeOffset);
+            LOG.i(TAG, "byteOffset:" + byteOffset);
+            //累加到当前位置处
+            startPos = startPos + byteOffset;
+            LOG.i(TAG, "startPos result:" + startPos);
+            if (startPos >= 0) {
+                int t = splitByteArray(mAllAudioData[i], startPos, buffer);
+                result = t != READ_STATUS_END ? bytesReadStart : READ_STATUS_END;
+            } else {
+                result = bytesReadStart;
+            }
             results[i] = result;
             results[i] = result;
             allAudioData2[i] = buffer;
             allAudioData2[i] = buffer;
         }
         }
@@ -332,18 +351,18 @@ public class MergeTrackManager {
         if (allAudioData == null) {
         if (allAudioData == null) {
             return;
             return;
         }
         }
-        if (offsetAudioData == null) {
-            offsetAudioData = new byte[mAllAudioData.length][];
-        }
-        for (int i = 0; i < allAudioData.length; i++) {
-            byte[] allAudioDatum = allAudioData[i];
-            if (allAudioDatum == null || allAudioDatum.length == 0) {
-                continue;
-            }
-            byte[] afterChangeBytes = countFilePosFromTime(allAudioDatum, positionOffset[i]);
-            LOG.i(TAG, "afterChangeBytes" + i + "-length:" + afterChangeBytes.length);
-            offsetAudioData[i] = afterChangeBytes;
-        }
+//        if (offsetAudioData == null) {
+//            offsetAudioData = new byte[mAllAudioData.length][];
+//        }
+//        for (int i = 0; i < allAudioData.length; i++) {
+//            byte[] allAudioDatum = allAudioData[i];
+//            if (allAudioDatum == null || allAudioDatum.length == 0) {
+//                continue;
+//            }
+//            byte[] afterChangeBytes = countFilePosFromTime(allAudioDatum, positionOffset[i]);
+//            LOG.i(TAG, "afterChangeBytes" + i + "-length:" + afterChangeBytes.length);
+//            offsetAudioData[i] = afterChangeBytes;
+//        }
     }
     }
 
 
     private byte[] countFilePosFromTime(byte[] source, long timeOffset) {
     private byte[] countFilePosFromTime(byte[] source, long timeOffset) {
@@ -351,10 +370,6 @@ public class MergeTrackManager {
         LOG.i(TAG, "source:" + source);
         LOG.i(TAG, "source:" + source);
         if (timeOffset != 0) {
         if (timeOffset != 0) {
             int exceptAudioLength = getLengthFromDuration(timeOffset);
             int exceptAudioLength = getLengthFromDuration(timeOffset);
-            //因为采样率44100 双声道 16位的位深 需要偶数项
-            if (exceptAudioLength % 2 != 0) {
-                exceptAudioLength += 1;
-            }
             LOG.i(TAG, "exceptAudioLength:" + exceptAudioLength);
             LOG.i(TAG, "exceptAudioLength:" + exceptAudioLength);
             byte[] lastLengthFromExcept = getLastLengthFromExcept(source, exceptAudioLength);
             byte[] lastLengthFromExcept = getLastLengthFromExcept(source, exceptAudioLength);
             LOG.i(TAG, "lastLengthFromExcept:" + lastLengthFromExcept.length);
             LOG.i(TAG, "lastLengthFromExcept:" + lastLengthFromExcept.length);
@@ -370,6 +385,10 @@ public class MergeTrackManager {
     private int getLengthFromDuration(long timeOffset) {
     private int getLengthFromDuration(long timeOffset) {
         float d = timeOffset / 1000f;
         float d = timeOffset / 1000f;
         int exceptAudioLength = (int) (d * (RATE * CHANNEL_CONFIG * AUDIO_FORMAT));
         int exceptAudioLength = (int) (d * (RATE * CHANNEL_CONFIG * AUDIO_FORMAT));
+        //因为采样率44100 双声道 16位的位深 需要偶数项
+        if (exceptAudioLength % 2 != 0) {
+            exceptAudioLength += 1;
+        }
         return exceptAudioLength;
         return exceptAudioLength;
     }
     }
 
 
@@ -424,18 +443,18 @@ public class MergeTrackManager {
     }
     }
 
 
     private int splitByteArray(byte[] source, int startIndex, byte[] destination) {
     private int splitByteArray(byte[] source, int startIndex, byte[] destination) {
-        if (startIndex * destination.length >= source.length) {
-            return -1;
+        if (startIndex >= source.length) {
+            return READ_STATUS_END;
         }
         }
-        int remainByteLength = source.length - startIndex * destination.length;
+        int remainByteLength = source.length - startIndex;
         boolean isLast = remainByteLength <= destination.length;
         boolean isLast = remainByteLength <= destination.length;
         if (isLast) {
         if (isLast) {
-            System.arraycopy(source, startIndex * destination.length, destination, 0, remainByteLength);
+            System.arraycopy(source, startIndex, destination, 0, remainByteLength);
         } else {
         } else {
-            System.arraycopy(source, startIndex * destination.length, destination, 0, destination.length);
+            System.arraycopy(source, startIndex, destination, 0, destination.length);
         }
         }
         if (isLast) {
         if (isLast) {
-            return -1;
+            return READ_STATUS_END;
         }
         }
         return startIndex;
         return startIndex;
     }
     }
@@ -609,7 +628,8 @@ public class MergeTrackManager {
         if (ISPLAYSOUND) {
         if (ISPLAYSOUND) {
             return;
             return;
         }
         }
-        if (mAllAudioData == null || offsetAudioData == null) {
+//        if (mAllAudioData == null || offsetAudioData == null) {
+        if (mAllAudioData == null) {
             return;
             return;
         }
         }
         Observable.create(new ObservableOnSubscribe<Boolean>() {
         Observable.create(new ObservableOnSubscribe<Boolean>() {
@@ -655,11 +675,11 @@ public class MergeTrackManager {
         byte[][] allAudioData2 = new byte[mAllAudioData.length][];
         byte[][] allAudioData2 = new byte[mAllAudioData.length][];
         while (ISPLAYSOUND) {
         while (ISPLAYSOUND) {
             int result = countBytesByPosition(bytesReadPos, allAudioData2);
             int result = countBytesByPosition(bytesReadPos, allAudioData2);
-            bytesReadPos = result != -1 ? ++result : -1;
+            bytesReadPos = result != READ_STATUS_END ? ++result : READ_STATUS_END;
             LOG.i(TAG, "bytesRead:" + bytesReadPos);
             LOG.i(TAG, "bytesRead:" + bytesReadPos);
             byte[] audiodata = weightAudioMixer.mixRawAudioBytes(allAudioData2);
             byte[] audiodata = weightAudioMixer.mixRawAudioBytes(allAudioData2);
             mAudioTrack.write(audiodata, 0, minBufferSize);
             mAudioTrack.write(audiodata, 0, minBufferSize);
-            if (bytesReadPos == -1) {
+            if (bytesReadPos == READ_STATUS_END) {
                 LOG.i(TAG, "读取完成:" + getPlayProgress());
                 LOG.i(TAG, "读取完成:" + getPlayProgress());
                 sendPlayCompletedMsg(audiodata.length);
                 sendPlayCompletedMsg(audiodata.length);
                 reset();
                 reset();
@@ -687,7 +707,7 @@ public class MergeTrackManager {
         eventListener = null;
         eventListener = null;
         ISPLAYSOUND = false;
         ISPLAYSOUND = false;
         mAllAudioData = null;
         mAllAudioData = null;
-        offsetAudioData = null;
+//        offsetAudioData = null;
         if (mHandler != null) {
         if (mHandler != null) {
             mHandler.removeCallbacksAndMessages(null);
             mHandler.removeCallbacksAndMessages(null);
         }
         }

+ 2 - 2
musicMerge/src/main/java/com/cooleshow/musicmerge/widget/MusicFrequencyView.java

@@ -123,9 +123,9 @@ public class MusicFrequencyView extends View {
             j++;
             j++;
             model[j] = Math.abs(model[j]);
             model[j] = Math.abs(model[j]);
         }
         }
-        LOG.i(TAG,"model:"+model.length);
+//        LOG.i(TAG,"model:"+model.length);
         float[] results = hananianWindow(model, 2);
         float[] results = hananianWindow(model, 2);
-        LOG.i(TAG,"results:"+results.length);
+//        LOG.i(TAG,"results:"+results.length);
         return results;
         return results;
     }
     }