/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code 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 General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; /** * @test * @bug 8146239 */ public final class RecognizeAuFloat { //sox -c 2 -n -b 32 -e floating-point -r 32000 --comment '' tmp.au synth 0.01 sine 1-90000 sine 90000-1 //sndfile-convert tmp.au data.au static byte[] data = new byte[]{ 46, 115, 110, 100, 0, 0, 0, 24, 0, 0, 10, 0, 0, 0, 0, 6, 0, 0, 125, 0, 0, 0, 0, 2, 55, 30, 0, 0, -66, 83, 22, 72, 57, 20, 0, 0, -66, -22, -69, 4, 57, -100, -96, 0, 62, -119, 4, -42, 57, -15, -80, 0, -67, -102, -15, -24, 58, 39, -84, 0, -68, 77, 29, 64, 58, 88, -104, 0, 61, 39, -13, -64, 58, -122, -16, 0, -69, -74, -27, -128, 58, -94, -26, 0, -66, 42, 16, 116, 58, -63, 38, 0, 63, 5, 27, -95, 58, -32, -6, 0, -65, 25, 104, -34, 59, 1, -98, 0, -66, -67, -43, 98, 59, 19, -85, 0, 62, -37, 121, -120, 59, 39, 4, 0, 63, 21, 30, 50, 59, 59, 110, 0, 63, 65, 109, 87, 59, 81, 60, 0, 63, 11, 74, 112, 59, 104, 67, 0, 62, -36, 123, -48, 59, -128, 100, 0, -66, -50, -18, -128, 59, -115, 88, -128, -65, 20, -62, 90, 59, -101, 27, -128, -67, -122, 39, -88, 59, -87, -89, -128, 63, 81, 60, -32, 59, -71, 18, -128, -65, 52, -14, 17, 59, -55, 95, -128, 62, 91, 110, -100, 59, -38, -97, 0, 62, 36, 28, -120, 59, -20, -38, 0, -66, -97, 28, -24, 60, 0, 15, 0, 62, -95, 14, 112, 60, 10, 60, -128, -66, -125, -38, 104, 60, 20, -5, 0, 62, 64, -85, -72, 60, 32, 83, -64, -66, 0, -88, -124, 60, 44, 75, -128, 61, -104, -68, -104, 60, 56, -19, 64, -67, 11, -80, 16, 60, 70, 62, 0, 59, 12, 0, 0, 60, 84, 73, 64, 60, -72, 108, -128, 60, 99, 21, 0, -67, 6, -91, 32, 60, 114, -83, -128, 61, 110, -11, 32, 60, -127, -116, -64, -67, 73, -87, -64, 60, -118, 51, 64, 61, -71, 28, -96, 60, -109, 77, -32, -67, -66, 26, -72, 60, -100, -29, -96, 61, -101, 24, -112, 60, -90, -7, 32, -67, 110, -4, -64, 60, -79, -107, -96, 61, -85, -41, 112, 60, -68, -66, 64, -67, -117, 85, 8, 60, -56, 122, -96, 61, 119, 33, -128, 60, -44, -48, -32, -67, -123, -116, 104, 60, -31, -56, -64, 60, -101, -65, -128, 60, -17, 105, 96, 60, 71, 29, -128, 60, -3, -70, -32, -67, -121, -72, 32, 61, 6, 98, -80, 62, 17, 12, -52, 61, 14, 72, -96, -66, 98, -7, 120, 61, 22, -109, -48, 62, -89, -123, -114, 61, 31, 72, -64, -66, -46, -27, -126, 61, 40, 108, 112, 62, -24, 13, -98, 61, 50, 3, -64, -66, -49, -111, -118, 61, 60, 20, 32, 62, 65, -50, 0, 61, 70, -94, -48, 62, 7, -53, -92, 61, 81, -75, -80, -65, 15, 22, 51, 61, 93, 82, -112, 63, 75, 122, 6, 61, 105, 127, -64, -65, 39, -103, 108, 61, 118, 67, -128, 61, 102, -22, 32, 61, -127, -46, 80, 63, 3, -8, 3, 61, -120, -44, -16, -65, 13, 31, -78, 61, -112, 45, 88, -66, 26, 95, 88, 61, -105, -33, 40, 63, 56, 1, -51, 61, -97, -18, 104, -66, -87, 96, -96, 61, -88, 94, -24, -65, 14, -12, 116, 61, -79, 53, 8, 62, -28, 8, -68, 61, -70, 116, -16, 62, -11, -35, 8, 61, -60, 35, 48, -65, 7, -110, 47, 61, -50, 68, 96, -65, 4, 62, -13, 61, -40, -35, 48, 62, -54, -54, 70, 61, -29, -14, -64, 63, 28, -19, -10, 61, -17, -119, -80, -66, 74, 117, -88, 61, -5, -89, -24, -65, 55, 82, 2, 62, 4, 40, -40, -66, 119, -21, -24, 62, 10, -58, -4, 62, -8, 108, 110, 62, 17, -80, 40, 63, 38, 48, 49, 62, 24, -24, -112, -67, 110, -90, 32, 62, 32, 113, 92, -65, 29, -111, -11, 62, 40, 79, -92, -65, 31, 111, 119, 62, 48, -125, -68, -67, -99, -66, -80, 62, 57, 19, -60, 62, -13, -69, 100, 62, 65, -1, 16, 63, 53, 109, -123, 62, 75, 76, -64, 62, -112, 45, 8, 62, 84, -5, 36, -66, -117, -58, -90, 62, 95, 18, 24, -65, 43, -15, -111, 62, 105, -113, 36, -65, 37, -126, 17, 62, 116, 122, 72, -66, -71, 7, 124, 62, 127, -48, -48, 61, -80, 58, 104, 62, -123, -52, -22, 63, 10, -42, -96, 62, -117, -23, -88, 63, 53, 17, -15, 62, -110, 64, -8, 63, 4, -33, -105, 62, -104, -46, -42, 62, 14, -16, 16, 62, -97, -97, 98, -66, -120, -100, 48, 62, -90, -88, 106, -65, 16, -55, 65, 62, -83, -22, -104, -65, 50, 111, 94, 62, -75, 106, 126, -65, 43, -76, -4, 62, -67, 31, -16, -65, 5, 60, 84, 62, -59, 19, 34, -66, -101, 116, -118, 62, -51, 53, -64, -67, -114, -112, 112, 62, -43, -108, 62, 62, 124, 61, 4, 62, -34, 25, 4, 63, 12, -46, 66, 62, -26, -44, -28, 63, 46, -89, -62, 62, -17, -86, 94, 63, 49, -118, -31, 62, -8, -83, -6, 63, 18, 74, -70, 63, 0, -35, 49, 62, -57, -13, -10, 63, 5, 115, 21, 62, 39, 28, -108, 63, 10, 2, -109, -67, 91, 2, -48, 63, 14, -106, 126, -66, -125, -114, 6, 63, 19, 22, 108, -66, -43, -29, 52, 63, 23, -118, -117, -65, 11, 119, -38, 63, 27, -39, -51, -65, 32, -43, 72, 63, 32, 6, -41, -65, 46, 64, -64, 63, 35, -6, 65, -65, 51, -67, -118, 63, 39, -83, -78, -65, 51, -12, -67, 63, 43, 14, 38, -65, 47, -33, -17, 63, 46, 8, 121, -65, 41, 19, 46, 63, 48, -111, 23, -65, 32, -60, -18, 63, 50, -124, 76, -65, 23, -122, -109, 63, 51, -31, 5, -65, 14, -121, 38, 63, 52, 111, -48, -65, 5, -96, -7, 63, 52, 60, -30, -66, -5, -52, -60, 63, 50, -5, 32, -66, -19, -110, -102, 63, 48, -58, 58, -66, -30, 112, -76, 63, 45, 59, -112, -66, -40, -12, 36, 63, 40, -121, -76, -66, -46, -85, 120, 63, 34, 53, -105, -66, -50, 29, 12, 63, 26, -125, 46, -66, -52, -124, 104, 63, 16, -17, -11, -66, -52, -114, 12, 63, 5, -54, -26, -66, -49, 34, 40, 62, -15, 39, -124, -66, -45, 38, -34, 62, -45, 83, 6, -66, -39, 53, 112, 62, -79, 58, -88, -66, -32, 113, 10, 62, -117, -71, 118, -66, -23, 44, -72, 62, 68, -124, 88, -66, -14, -61, 36, 61, -41, 35, -48, -66, -3, 76, -16, 60, 73, 21, 64, -65, 4, 41, 121, -67, -84, -32, 56, -65, 9, -32, 97, -66, 59, -16, -68, -65, 15, -95, -73, -66, -112, -19, 104, -65, 21, 83, -106, -66, -62, 84, 28, -65, 26, -38, 66, -66, -15, 61, -70, -65, 32, 18, -104, -65, 13, 91, 117, -65, 36, -21, 35, -65, 31, 24, -23, -65, 41, 61, 53, -65, 43, -67, -83, -65, 44, -1, 47, -65, 51, 118, 20, -65, 48, 11, 126, -65, 51, -106, -70, -65, 50, 94, -68, -65, 45, 55, 12, -65, 51, -40, 0, -65, 29, 54, 57, -65, 52, 121, 12, -65, 6, 103, 50, -65, 52, 40, 15, -66, -54, 55, 44, -65, 50, -21, 51, -66, 126, 66, -44, -65, 48, -79, 26, -67, 117, 95, -48, -65, 45, -125, -61, 61, -106, 52, -96, -65, 41, 90, -97, 61, -57, 113, -120, -65, 36, 66, -15, 62, 12, -23, 24, -65, 30, 60, -115, 62, 48, 125, 20, -65, 23, 87, 95, 62, 90, -59, 68, -65, 15, -102, -111, 62, -127, -1, 112, -65, 7, 24, 12, 62, -104, 94, -106, -66, -5, -71, -60, 62, -82, -25, -76, -66, -25, -8, -128, 62, -58, 53, -84, -66, -45, 14, -22, 62, -35, -81, -96, -66, -67, 36, -120, 62, -11, 6, -66, -66, -90, 97, 4, 63, 6, 3, 127, -66, -114, -21, -84, 63, 16, -20, 40, -66, 109, -36, 56, 63, 27, 40, 41, -66, 61, 28, 72, 63, 36, 33, -103, -66, 11, -23, 72, 63, 43, -83, 65, -67, -75, 19, 40, 63, 49, 27, 71, -67, 37, 37, 48, 63, 52, 32, -92, 59, -13, 9, 0, 63, 51, -1, -36, 61, 95, 94, -112, 63, 48, 88, -114, 61, -50, 123, -8, 63, 40, 111, -36, 62, 21, -110, 52, 63, 27, -12, -26, 62, 66, -90, -48, 63, 10, 76, 35, 62, 110, 78, 0, 62, -26, -13, -12, 62, -116, 49, -14, 62, -82, 100, -112, 62, -96, 99, 6, 62, 88, -35, 60, 62, -77, -83, 18, 61, -122, 105, 8, 62, -58, 4, 54, -67, -72, 110, -32, 62, -41, 95, -40, -66, -128, -100, 68, 62, -25, -72, -8, -66, -50, -95, -10, 62, -9, 11, 32, -65, 10, 90, -74, 63, 2, -87, -56, -65, 36, 41, 100, 63, 9, 72, -63, -65, 51, 48, -12, 63, 15, 98, -30, -65, 49, 114, 53, 63, 20, -7, 86, -65, 31, -51, 3, 63, 26, 13, -7, -66, -14, -93, 4, 63, 30, -93, 56, -66, -118, -69, 76, 63, 34, -68, 16, -70, -82, 44, 0, 63, 38, 91, -23, 62, 94, 113, -52, 63, 41, -122, -105, 62, -89, -122, -42, 63, 44, 64, 56, 62, -28, 76, -10, 63, 46, -115, 50, 63, 12, 66, 105, 63, 48, 114, 28, 63, 34, 21, 2, 63, 49, -13, -73, 63, 48, 66, -126, 63, 51, 22, -34, 63, 52, 69, -91, 63, 51, -32, 123, 63, 44, 33, -96, 63, 52, 85, -126, 63, 21, 55, -35, 63, 52, 122, -32, 62, -33, -42, 72, 63, 52, 85, 123, 62, 113, 54, 96, 63, 51, -22, 37, -69, 84, -27, 0, 63, 51, 61, -98, -66, -125, -34, -80, 63, 50, 84, -124, -66, -7, -59, 56, 63, 49, 51, 90, -65, 38, -69, 12, 63, 47, -34, 124, -65, 52, 120, -13, 63, 46, 90, 32, -65, 28, -48, -75, 63, 44, -86, 84, -66, -62, -122, 2, 63, 42, -46, -4, -68, -66, 18, -64, 63, 40, -41, -49, 62, -125, 39, -32, 63, 38, -68, 89, 62, -23, 125, -60, 63, 36, -125, -7, 63, 32, 56, 64, 63, 34, 49, -32, 63, 51, -95, -58, 63, 31, -55, 21, 63, 41, -75, 70, 63, 29, 76, 114, 62, -7, -33, -98, 63, 26, -66, -91, 62, 65, -30, 76, 63, 24, 34, 52, -66, 48, 71, -116, 63, 21, 121, 124, -65, 2, -67, 114, 63, 18, -58, -80, -65, 49, 29, -36, 63, 16, 11, -32, -65, 34, -68, -19, 63, 13, 74, -12, -66, -104, 24, -26, 63, 10, -123, -76, 62, 51, 28, -80, 63, 7, -67, -61, 63, 2, 89, -28, 63, 4, -12, -89, 63, 47, -73, -62, 63, 2, 43, -61, 63, 39, 28, 20, 62, -2, -56, -64, 62, -77, 8, -104, 62, -7, 63, 86, -67, -19, -18, -96, 62, -13, -67, 110, -65, 14, -31, -41, 62, -18, 68, -10, -65, 50, -108, 124, 62, -24, -41, -72, -66, -51, -101, 100, 62, -29, 119, 74, 62, 81, -23, -92, 62, -34, 37, 28, 63, 35, 19, -90, 62, -40, -30, 118, 63, 42, 75, 0, 62, -45, -80, -126, 62, -101, 23, -2, 62, -50, -112, 60, -66, -92, 113, 88, 62, -55, -126, -110, -65, 48, 57, -75, 62, -60, -120, 62, -66, -18, 109, -36, 62, -65, -95, -2, 62, -122, 46, -94, 62, -70, -48, 80, 63, 43, -78, 92, 62, -74, 19, -56, 63, 8, -56, 59, 62, -79, 108, -82, -66, 53, 9, -12, 62, -84, -37, 124, -65, 45, -38, -43, 62, -88, 96, 68, -66, -64, 66, -114, 62, -93, -5, 104, 62, -5, -24, -122, 62, -97, -84, -46, 63, 37, -56, 106, 62, -101, 116, -52, -69, -51, 74, 0, 62, -105, 83, 38, -65, 52, 39, 12, 62, -109, 72, 12, -66, 105, -56, -88, 62, -113, 83, 68, 63, 34, 28, -23, 62, -117, 116, -54, 62, -41, 27, -96, 62, -121, -84, 124, -65, 6, 56, 36, 62, -125, -6, 18, -66, -24, -78, 120, 62, -128, 93, -98, 63, 26, -115, 55, 62, 121, -84, -36, 62, -72, -90, 88, 62, 114, -54, 0, -65, 24, -101, -10, 62, 108, 16, 20, -66, 52, -39, -8, 62, 101, -127, 68, 63, 69, 45, 6, 62, 95, 25, 120, -66, -68, 112, 34, 62, 88, -36, 112, -66, -63, -116, 94, 62, 82, -60, 72, 63, 28, 61, 0, 62, 76, -42, -116, -66, 27, 72, 116, 62, 71, 11, 104, -66, -7, -1, -124, 62, 65, 106, 68, 63, 84, 48, 84, 62, 59, -23, 120, -65, 42, 107, 50, 62, 54, -110, 16, 62, -125, -115, -8, 62, 49, 89, 0, 62, 57, -24, -4, 62, 44, 72, 60, -66, -35, -117, 4, 62, 39, 84, 100, 63, 1, -103, 34, 62, 34, -121, 12, -66, -17, 116, 4, 62, 29, -42, 0, 62, -65, -115, 24, 62, 25, 72, -72, -66, 120, -42, -28, 62, 20, -40, 64, 62, 26, 47, 8, 62, 16, -121, -112, -67, 77, -2, 32, 62, 12, 85, -124, 59, 64, 93, 0, 62, 8, 61, -16, 61, 73, 86, -80, 62, 4, 72, 80, -67, -102, 14, 0, 62, 0, 102, 100, 61, -76, -35, 112, 61, -7, 86, 72, -67, 121, 104, -96, 61, -15, -9, 56, 61, 123, -75, 16, 61, -22, -15, 64, -67, 115, -85, 64, 61, -29, -15, 8, 61, -125, -102, 40, 61, -35, 86, -16, -68, -17, 52, 32, 61, -42, -80, -128, 61, 30, -101, 112, 61, -48, 124, -16, 60, 14, -37, 0, 61, -54, 44, 88, -67, 38, 97, 112, 61, -60, 89, 8, 61, 79, -37, -96, 61, -66, 91, -64, -67, 76, 109, 80, 61, -72, -31, 72, 61, -73, -94, -88, 61, -77, 54, 72, -67, -4, 114, -24, 61, -82, 12, 32, 62, 5, 28, 48, 61, -88, -77, -8, -66, 37, 7, 20, 61, -93, -48, 88, 62, 36, -101, 8, 61, -98, -52, -16, -67, -40, 80, 112, 61, -102, 37, 64, -67, -91, -60, 104, 61, -107, 121, -88, 62, -47, 16, 34, 61, -111, 2, -72, -65, 69, 50, -82, 61, -116, -78, -96, 63, 20, -70, 1, 61, -120, 97, 56, 62, -127, -67, -36, 61, -124, 112, 96, -65, 33, -9, -43, 61, -128, 57, -16, -66, -7, -70, 72, 61, 121, 86, -32, 62, 41, -78, 116, 61, 113, 13, -80, 63, 18, -75, -76, 61, 106, -72, 112, 63, 48, 90, 88, 61, 98, -123, 48, 63, 52, 13, -69, 61, 92, -10, 16, 63, 45, 31, -18, 61, 84, -47, 48, 62, -32, 12, 26, 61, 80, 0, 48, -66, 38, -81, 88, 61, 71, -23, -16, -65, 63, -24, 61, 61, 67, -57, 0, 62, -106, -40, 110, 61, 59, -55, 48, 62, 116, 6, 44, 61, 56, 58, 80, -66, -67, 31, 42, 61, 48, 106, 64, 62, -88, 121, 104, 61, 45, 73, 16, -66, -115, -11, -110, 61, 37, -53, 48, 62, -128, -110, -42, 61, 34, -33, -80, -66, 121, 84, -44, 61, 27, -17, 48, 62, 79, -38, 28, 61, 24, -30, -16, 61, 91, 108, 16, 61, 18, -23, 32, -65, 38, 12, 43, 61, 15, 26, -64, 62, 59, -92, -104, 61, 11, 11, 96, 62, -37, 45, 18, 61, 4, -81, -48, 61, 74, 69, -16, 61, 6, -67, 80, }; public static void main(final String[] args) throws Exception { // Audio data should contain the same format as a command line above test(new ByteArrayInputStream(data)); // Checks that the data will be preserved when we save audio data to the // file and read it again final File file = File.createTempFile("sound", "test"); file.deleteOnExit(); try (AudioInputStream ais = AudioSystem.getAudioInputStream( new ByteArrayInputStream(data))) { AudioSystem.write(ais, AudioFileFormat.Type.AU, file); } try (InputStream fis = new FileInputStream(file)) { if (!Arrays.equals(data, fis.readAllBytes())) { throw new RuntimeException("Wrong data in the file: "); } } } /** * Tests that audio data is the same format as a command line above. */ private static void test(InputStream is) throws UnsupportedAudioFileException, IOException { final AudioFileFormat aff = AudioSystem.getAudioFileFormat(is); final int byteLength = aff.getByteLength(); if (byteLength != data.length) { throw new RuntimeException("Wrong byteLength: " + byteLength); } final AudioFileFormat.Type type = aff.getType(); if (!type.equals(AudioFileFormat.Type.AU)) { throw new RuntimeException("Wrong type: " + type); } final int frameLength = aff.getFrameLength(); if (frameLength != 0.01f * aff.getFormat().getFrameRate()) { throw new RuntimeException("Wrong frameLength: " + frameLength); } checkAudioFormat(aff.getFormat()); try (AudioInputStream ais = AudioSystem.getAudioInputStream(is)) { final AudioFormat format = ais.getFormat(); if (ais.getFrameLength() != 0.01f * format.getFrameRate()) { throw new RuntimeException( "Wrong frameLength: " + ais.getFrameLength()); } checkAudioFormat(format); } } private static void checkAudioFormat(final AudioFormat audioFormat) { final int channels = audioFormat.getChannels(); if (channels != 2) { throw new RuntimeException("Wrong channels: " + channels); } final int sizeInBits = audioFormat.getSampleSizeInBits(); if (sizeInBits != 32) { throw new RuntimeException("Wrong sizeInBits: " + sizeInBits); } final int frameSize = audioFormat.getFrameSize(); if (frameSize != sizeInBits * channels / 8) { throw new RuntimeException("Wrong frameSize: " + frameSize); } final AudioFormat.Encoding encoding = audioFormat.getEncoding(); if (!encoding.equals(AudioFormat.Encoding.PCM_FLOAT)) { throw new RuntimeException("Wrong encoding: " + encoding); } final float frameRate = audioFormat.getFrameRate(); if (frameRate != 32000) { throw new RuntimeException("Wrong frameRate: " + frameRate); } final float sampleRate = audioFormat.getSampleRate(); if (sampleRate != 32000) { throw new RuntimeException("Wrong sampleRate: " + sampleRate); } } }