1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.mozilla.thirdparty.com.google.android.exoplayer2.audio;
17 
18 import org.mozilla.thirdparty.com.google.android.exoplayer2.C;
19 import org.mozilla.thirdparty.com.google.android.exoplayer2.Format;
20 import org.mozilla.thirdparty.com.google.android.exoplayer2.util.Util;
21 
22 /** Utilities for handling WAVE files. */
23 public final class WavUtil {
24 
25   /** Four character code for "RIFF". */
26   public static final int RIFF_FOURCC = 0x52494646;
27   /** Four character code for "WAVE". */
28   public static final int WAVE_FOURCC = 0x57415645;
29   /** Four character code for "fmt ". */
30   public static final int FMT_FOURCC = 0x666d7420;
31   /** Four character code for "data". */
32   public static final int DATA_FOURCC = 0x64617461;
33 
34   /** WAVE type value for integer PCM audio data. */
35   public static final int TYPE_PCM = 0x0001;
36   /** WAVE type value for float PCM audio data. */
37   public static final int TYPE_FLOAT = 0x0003;
38   /** WAVE type value for 8-bit ITU-T G.711 A-law audio data. */
39   public static final int TYPE_ALAW = 0x0006;
40   /** WAVE type value for 8-bit ITU-T G.711 mu-law audio data. */
41   public static final int TYPE_MLAW = 0x0007;
42   /** WAVE type value for IMA ADPCM audio data. */
43   public static final int TYPE_IMA_ADPCM = 0x0011;
44   /** WAVE type value for extended WAVE format. */
45   public static final int TYPE_WAVE_FORMAT_EXTENSIBLE = 0xFFFE;
46 
47   /**
48    * Returns the WAVE format type value for the given {@link C.PcmEncoding}.
49    *
50    * @param pcmEncoding The {@link C.PcmEncoding} value.
51    * @return The corresponding WAVE format type.
52    * @throws IllegalArgumentException If {@code pcmEncoding} is not a {@link C.PcmEncoding}, or if
53    *     it's {@link C#ENCODING_INVALID} or {@link Format#NO_VALUE}.
54    */
getTypeForPcmEncoding(@.PcmEncoding int pcmEncoding)55   public static int getTypeForPcmEncoding(@C.PcmEncoding int pcmEncoding) {
56     switch (pcmEncoding) {
57       case C.ENCODING_PCM_8BIT:
58       case C.ENCODING_PCM_16BIT:
59       case C.ENCODING_PCM_24BIT:
60       case C.ENCODING_PCM_32BIT:
61         return TYPE_PCM;
62       case C.ENCODING_PCM_FLOAT:
63         return TYPE_FLOAT;
64       case C.ENCODING_PCM_16BIT_BIG_ENDIAN: // Not TYPE_PCM, because TYPE_PCM is little endian.
65       case C.ENCODING_INVALID:
66       case Format.NO_VALUE:
67       default:
68         throw new IllegalArgumentException();
69     }
70   }
71 
72   /**
73    * Returns the {@link C.PcmEncoding} for the given WAVE format type value, or {@link
74    * C#ENCODING_INVALID} if the type is not a known PCM type.
75    */
getPcmEncodingForType(int type, int bitsPerSample)76   public static @C.PcmEncoding int getPcmEncodingForType(int type, int bitsPerSample) {
77     switch (type) {
78       case TYPE_PCM:
79       case TYPE_WAVE_FORMAT_EXTENSIBLE:
80         return Util.getPcmEncoding(bitsPerSample);
81       case TYPE_FLOAT:
82         return bitsPerSample == 32 ? C.ENCODING_PCM_FLOAT : C.ENCODING_INVALID;
83       default:
84         return C.ENCODING_INVALID;
85     }
86   }
87 
WavUtil()88   private WavUtil() {
89     // Prevent instantiation.
90   }
91 }
92