Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FLAC_MD5.java « javaFlacEncoder « src - github.com/ClusterM/android-speech-recognition.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1577356ff2f615cfaf9bde8a86fd696534730ac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2010  Preston Lacey http://javaflacencoder.sourceforge.net/
 * All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package javaFlacEncoder;

import java.security.MessageDigest;


/**
 *
 * @author preston
 */
public class FLAC_MD5 {
  private MessageDigest md = null;
  private byte[] _dataMD5 = null;
  
  public FLAC_MD5() throws java.security.NoSuchAlgorithmException {
    md = MessageDigest.getInstance("md5");
  }

  public MessageDigest getMD() {
    return md;
  }

  /**
   * Add samples to the MD5 hash.
   * CURRENTLY ONLY MAY WORK FOR: sample sizes which are divisible by 8. Need
   * to create some audio to test with.
   * @param samples
   * @param count
   * @param channels
   */
  public void addSamplesToMD5(int[] samples, int count, int channels,
      int sampleSize) {
    int bytesPerSample = sampleSize/8;
    if(sampleSize%8 != 0)
      bytesPerSample++;
    if(_dataMD5 == null || _dataMD5.length < count*bytesPerSample*channels) {
      _dataMD5 = new byte[count*bytesPerSample*channels];
    }
    byte[] dataMD5 = _dataMD5;
    splitSamplesToBytes(samples, count*channels, bytesPerSample, dataMD5);
    md.update(dataMD5, 0, count*bytesPerSample*channels);
  }

  /* Split Samples to bytes(for sending to MD5)
   * CURRENTLY ONLY MAY WORK FOR: sample sizes which are divisible by 8. Need
   * to create some audio to test with.*/
  private static final void splitSamplesToBytes(int[] samples, int totalSamples,
    int bytesPerSample, byte[] dataMD5) {
    int destIndexBase = 0;
    int i = 0;
    
    switch(bytesPerSample) {
      case 3:
        for(; i < totalSamples; i++) {
        dataMD5[destIndexBase++] = (byte)(samples[i]);
        dataMD5[destIndexBase++] = (byte)(samples[i] >> 8);
        dataMD5[destIndexBase++] = (byte)(samples[i] >> 16);
        }
        break;
      case 2:
        for(; i < totalSamples; i++) {
        dataMD5[destIndexBase++] = (byte)(samples[i]);
        dataMD5[destIndexBase++] = (byte)(samples[i] >> 8);
        }
        break;
      case 1:
        for(; i < totalSamples; i++) {
        dataMD5[i] = (byte)samples[i];
        }
    }
  }
}