Вопрос

I have code that decodes an MP3 and populates an array with all the 'values'. My question is: what are those values? Are they frequencies? Are they amplitudes? This is the code:

File file = new File(song.getFilepath());
                if (file.exists()) {
                    AudioInputStream in = AudioSystem.getAudioInputStream(file);
                    AudioInputStream din = null;
                    AudioFormat baseFormat = in.getFormat();
                    AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                            baseFormat.getSampleRate(),
                            16,
                            baseFormat.getChannels(),
                            baseFormat.getChannels() * 2,
                            baseFormat.getSampleRate(),
                            false);
                    din = AudioSystem.getAudioInputStream(decodedFormat, in);
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    byte[] data = new byte[4096];
                    SourceDataLine line = getLine(decodedFormat);

                    int nBytesRead = 0, nBytesWritten = 0;
                    while (nBytesRead != -1) {
                        nBytesRead = din.read(data, 0, data.length);
                        if (nBytesRead != -1) {
                            nBytesWritten = line.write(data, 0, nBytesRead);
                            out.write(data, 0, nBytesRead);
                        }
                    }
                    byte[] audio = out.toByteArray();
                    System.err.println(audio.length);
                    for (byte b : audio) {
                        System.err.println(b);
                    }
                }

I get about 40,000,000 numbers (length of byte array) for a 3 minute song, but I have no idea what they are?

Это было полезно?

Решение

They are amplitudes. Usually, each amplitude is 16 bits (2 bytes, range from -32768 to 32767), and there are two channels (left and right). In this case, one sound sample spans four bytes. Example: 100, 0, 2, 1 means left amplitude is 100 (of 32767) and right is 258.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top