Comments (3)
Thanks for taking the time. I can't help on this until September.
Try to add a test class that exercises your code change, and when it's done feel free to raise a pull request.
from musicott.
Found a temporary solution , though we still need to reduce the final amplitudes array size :
//Get the PCM Decoded Audio Input Stream
try (AudioInputStream pcmDecodedInput = AudioSystem.getAudioInputStream(decodedFormat, input)) {
final int BUFFER_SIZE = 4096; //this is actually bytes
System.out.println(available);
//Create a buffer
byte[] buffer = new byte[BUFFER_SIZE];
//Read all the available data on chunks
int counter = 0;
while (pcmDecodedInput.readNBytes(buffer, 0, BUFFER_SIZE) > 0)
for (int i = 0; i < buffer.length - 1; i += 2, counter += 2) {
if (counter == available)
break;
amplitudes[counter] = ( ( buffer[i + 1] << 8 ) | buffer[i] & 0xff ) << 16;
amplitudes[counter] /= 32767;
amplitudes[counter] *= WAVEFORM_HEIGHT_COEFFICIENT;
}
} catch (Exception ex) {
ex.printStackTrace();
}
from musicott.
@octaviospain Just for you i made the below awesome solution . No more too much ram used and now it is super duper fast .
I will do a pull request soon :)
/**
* Get Wav Amplitudes
*
* @param file
* @return
* @throws UnsupportedAudioFileException
* @throws IOException
*/
private int[] getWavAmplitudes(File file) throws UnsupportedAudioFileException , IOException {
//Get Audio input stream
try (AudioInputStream input = AudioSystem.getAudioInputStream(file)) {
AudioFormat baseFormat = input.getFormat();
//Encoding
Encoding encoding = AudioFormat.Encoding.PCM_UNSIGNED;
float sampleRate = baseFormat.getSampleRate();
int numChannels = baseFormat.getChannels();
AudioFormat decodedFormat = new AudioFormat(encoding, sampleRate, 16, numChannels, numChannels * 2, sampleRate, false);
int available = input.available();
//Get the PCM Decoded Audio Input Stream
try (AudioInputStream pcmDecodedInput = AudioSystem.getAudioInputStream(decodedFormat, input)) {
final int BUFFER_SIZE = 4096; //this is actually bytes
//Create a buffer
byte[] buffer = new byte[BUFFER_SIZE];
//Now get the average to a smaller array
int maximumArrayLength = 100000;
int[] finalAmplitudes = new int[maximumArrayLength];
int samplesPerPixel = available / maximumArrayLength;
//Variables to calculate finalAmplitudes array
int currentSampleCounter = 0;
int arrayCellPosition = 0;
float currentCellValue = 0.0f;
//Variables for the loop
int arrayCellValue = 0;
//Read all the available data on chunks
while (pcmDecodedInput.readNBytes(buffer, 0, BUFFER_SIZE) > 0)
for (int i = 0; i < buffer.length - 1; i += 2) {
//Calculate the value
arrayCellValue = (int) ( ( ( ( ( buffer[i + 1] << 8 ) | buffer[i] & 0xff ) << 16 ) / 32767 ) * WAVEFORM_HEIGHT_COEFFICIENT );
//Every time you him [currentSampleCounter=samplesPerPixel]
if (currentSampleCounter != samplesPerPixel) {
++currentSampleCounter;
currentCellValue += Math.abs(arrayCellValue);
} else {
//Avoid ArrayIndexOutOfBoundsException
if (arrayCellPosition != maximumArrayLength)
finalAmplitudes[arrayCellPosition] = finalAmplitudes[arrayCellPosition + 1] = (int) currentCellValue / samplesPerPixel;
//Fix the variables
currentSampleCounter = 0;
currentCellValue = 0;
arrayCellPosition += 2;
}
}
return finalAmplitudes;
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
//You don't want this to reach here...
return new int[1];
}
from musicott.
Related Issues (12)
- hashcode implementation HOT 1
- Missing artifact be.tarsos:transcoder:jar:1.2 , can't be download from Maven HOT 11
- Error while building the project HOT 4
- Any new release or features are planned? HOT 2
- It doesn't import mp3 files. HOT 5
- I want to know why you used Sun's FX module? HOT 2
- Issue Launching Application HOT 6
- Bug: Unable to import files in Linux HOT 5
- Playlists Drag&Drop feature request. Importing big folders make the progress bar stop working HOT 1
- com.worldsworstsoftware unable to find HOT 1
- Fail to build and run HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from musicott.