private ArrayList<Integer> findMinMax(int beginFrame, int endFrame) { // System.err.println(beginFrame + " " + endFrame); // System.err.println(data); ArrayList<Integer> minMax = new ArrayList<Integer>(); int length = endFrame - beginFrame + 1; float maxValue = myHelper.getMaxSampleValue(); float max = (-1) * maxValue; ; float min = maxValue; for (int sampleIndex = beginFrame; sampleIndex < endFrame; sampleIndex++) { int sampleValue = data.get(sampleIndex); if (max < sampleValue) { max = sampleValue; } if (min > sampleValue) { min = sampleValue; } } minMax.add((int) min); minMax.add((int) max); return minMax; }
private ArrayList<SoundSample> getSampleAsArrayList(Selection selection, Component c) { ArrayList<SoundSample> points = new ArrayList<SoundSample>(); // System.err.println(c.getHeight()); int sampleBeginIndex = selection.getBegin(); int sampleEndIndex = selection.getEnd(); if (!checkRange(sampleBeginIndex, sampleEndIndex)) { System.err.println( "The range of sample (" + sampleBeginIndex + " , " + sampleEndIndex + ") you are trying to visualize is invalid"); } else { int selectionLength = sampleEndIndex - sampleBeginIndex + 1; float maxValue = myHelper.getMaxSampleValue(); samplePerPixel = selectionLength * 1.0 / frameWidth; if (DEBUG) System.err.println("getSamples: sample per pixel " + samplePerPixel); if (selectionLength < frameWidth) { // if every sample can be displayed (i.e at least 1 pixel per sample) if (DEBUG) System.err.println("getSamples: Everything can be displayed " + selectionLength); int ratio = (int) Math.floor(frameWidth / selectionLength); if (DEBUG) System.err.println("getSamples: ratio " + ratio); int occupyPixels = ratio * selectionLength; selectionLength = selectionLength + (frameWidth - occupyPixels) / ratio; // fill up the display space with a few more samples if (DEBUG) System.err.println("getSamples: selection length " + selectionLength); if (selectionLength + sampleBeginIndex - 1 > data.size()) { selectionLength -= selectionLength + sampleBeginIndex - 1 - data.size(); } if (DEBUG) System.err.println("getSamples: Final selection length " + selectionLength); for (int i = 0; i < selectionLength; i++) { float sampleValue = data.get(sampleBeginIndex + i - 1); float y = ((float) Math.floor(c.getHeight() / 2) * (1 - sampleValue / maxValue)); points.add( new SoundSample(new Point2D.Float(i * ratio, y), sampleValue, sampleBeginIndex + i)); } } else if (selectionLength / 2 < frameWidth) { // we can sample at Nyquist rate // System.err.println("Sample begin " + sampleBeginIndex ); int start = sampleBeginIndex; for (int pixel = 0; pixel < frameWidth; pixel++) { // System.err.println("Sample index " + (start+ samplePerPixel * pixel) ); int sampleValue = data.get((int) Math.floor(start + samplePerPixel * pixel)); // int sampleValue = findMax(start, end); float y = ((float) Math.floor(c.getHeight() / 2) - (sampleValue * ((float) Math.floor(c.getHeight() / 2) / maxValue))); points.add(new SoundSample(new Point2D.Float(pixel, y), sampleValue, pixel)); } } else { // show general contour int start = sampleBeginIndex; int end = (int) (sampleBeginIndex + this.samplePerPixel); int baseLine = c.getHeight() / 2; for (int pixel = 0; pixel < frameWidth; pixel++) { ArrayList<Integer> minMax = findMinMax(start, end); // System.err.println(minMax); float y = baseLine - (minMax.get(0) * 0.9f / maxValue * baseLine); points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(0), pixel)); y = baseLine - (minMax.get(0) * 0.7f / maxValue * baseLine); points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(0) / 2, pixel)); // System.err.print("Min y: " + y + " "); y = baseLine - (minMax.get(1) * 0.9f / maxValue * baseLine); points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(1) / 2, pixel)); y = baseLine - (minMax.get(1) * 0.7f / maxValue * baseLine); points.add(new SoundSample(new Point2D.Float(pixel, y), minMax.get(1), pixel)); // if (y > c.getHeight()) // System.err.println("not right"); start = end + 1; end = (int) (end + samplePerPixel); } } } return points; }