I have an AudioFormat object (returned from audioInputStream.getFormat()
). I have a timestamp (in milliseconds) that I want to start reading from the corresponding wav file from. How do I determine how many bytes to read/skip from the AudioInputStream in order to get to the appropriate timeStamp?
I get confused with frame rate (and how it does or doesn't relate to sample rate).
This is what I have so far... startPos is the timestamp in milliseconds.
float skipTotalFrames = audioInputStream.getFormat().getFrameRate() * (startPos / 1000);
long byteStartPos = (long) (audioInputStream.getFormat().getFrameSize() * skipTotalFrames);
Where am I off at?
Edit: My code did in fact work, I just had other errors.
WAV format typically starts with a 44 byte header followed by audio samples taken 44,100 times per second (sample rate) where each sample is 16 bit signed integer little endian (bit depth) ... bit rate is calculated by multiplying those two fundamental factors : (sample rate) * (bit depth) ... this is mono so if stereo those samples are interleaved
Looking at the API you reference, first probe property : vbr (variable bit rate) If its true the calculation you desire would be unattainable. For WAV it should always a constant bit rate (IE. false). Then retrieve property : bitrate
bitrate = (sample_rate) * (bit_depth) * (number_of_channels) === bits per second
For argument sake lets say your ...
sample_rate = 44100; // 44.1 kHz which is typical
bit_depth = 16; // also typical
number_of_channels = 2; // mono --> 1 stereo --> 2
look_ahead_milli_sec = 1500; // you are given this in milliseconds
bit_rate = sample_rate * bit_depth * number_of_channels;
bit_rate = 44100 * 16 * 2;
bitrate = 1411200; // based on above calculation
bytes_per_second = bitrate / 8; // bits to bytes per sec
bytes_per_second = 1411200 / 8; // bits to bytes per sec
bytes_per_second = 176400; // bytes per sec
look_ahead_in_bytes = (bytes_per_second / 1000) * look_ahead_milli_sec;
look_ahead_in_bytes = (176400 / 1000) * 1500;
look_ahead_in_bytes = 264600;