Created
December 15, 2019 10:22
-
-
Save SaadArdati/08992e1df1c1cc06b10bd55326e0f740 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void createWaveForm(byte[] audioBytes) { | |
lines.removeAllElements(); // clear the old vector | |
AudioFormat format = audioInputStream.getFormat(); | |
if (audioBytes == null) { | |
try { | |
audioBytes = new byte[ | |
(int) (audioInputStream.getFrameLength() | |
* format.getFrameSize())]; | |
audioInputStream.read(audioBytes); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
return; | |
} | |
} | |
int[] audioData = null; | |
if (format.getSampleSizeInBits() == 16) { | |
int nlengthInSamples = audioBytes.length / 2; | |
audioData = new int[nlengthInSamples]; | |
for (int i = 0; i < nlengthInSamples; i++) { | |
if (format.isBigEndian()) { | |
/* First byte is MSB (high order) */ | |
int MSB = audioBytes[2 * i]; | |
/* Second byte is LSB (low order) */ | |
int LSB = audioBytes[2 * i + 1]; | |
audioData[i] = MSB << 8 | (255 & LSB); | |
} else { | |
/* First byte is LSB (low order) */ | |
int LSB = audioBytes[2 * i]; | |
/* Second byte is MSB (high order) */ | |
int MSB = audioBytes[2 * i + 1]; | |
audioData[i] = MSB << 8 | (255 & LSB); | |
} | |
} | |
} else if (format.getSampleSizeInBits() == 8) { | |
int nlengthInSamples = audioBytes.length; | |
audioData = new int[nlengthInSamples]; | |
if (format.getEncoding().toString().startsWith("PCM_SIGN")) { | |
for (int i = 0; i < audioBytes.length; i++) { | |
audioData[i] = audioBytes[i]; | |
} | |
} else { | |
for (int i = 0; i < audioBytes.length; i++) { | |
audioData[i] = audioBytes[i] - 128; | |
} | |
} | |
} | |
if (audioData == null) return; | |
int width = 1000; | |
int height = 200; | |
final int bucketCount = width; | |
final int bucketSize = audioData.length / format.getChannels() / bucketCount; | |
final double[] buckets = new double[bucketCount]; | |
for (int bucketIndex = 0, sampleIndex = 0; bucketIndex < bucketCount; bucketIndex++) { | |
double sum = 0; | |
for (int b = 0; b < bucketSize; b++) { | |
for (int c = 0; c < format.getChannels(); c++) { | |
sum += Math.abs(audioData[sampleIndex++]); | |
} | |
} | |
buckets[bucketIndex] = sum / bucketSize; | |
} | |
for (int i = 0; i < buckets.length; i++) { | |
double d = buckets[i]; | |
lines.add(new Point2D(i,d)); | |
} | |
saveToFile(width); | |
} | |
public void saveToFile(int w) { | |
int h = 200; | |
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D g2 = bufferedImage.createGraphics(); | |
createSampleOnGraphicsContext(w, h, g2); | |
g2.dispose(); | |
try { | |
ImageIO.write(bufferedImage, "png", outputImg); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void createSampleOnGraphicsContext(int w, int h, Graphics2D g2) { | |
g2.setComposite(AlphaComposite.Clear); | |
g2.fillRect(0, 0, w, h); | |
g2.setComposite(AlphaComposite.Src); | |
if (audioInputStream != null) { | |
g2.setColor(Color.WHITE); | |
for (Point2D line : lines) { | |
g2.drawLine((int) line.getX(), 100, (int) line.getX(), 100 + (int) line.getY()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment