/** * This function goes through all samples of the .WAV song and multiplies the values by the given * gainFactor in order to achieve the volume changing * * @param gainFactor - the value which is multiplied with the sample value to get new sample value */ public void changeVolume(double gainFactor) { int sample = 0; for (int i = wavTagReader.getFirstSampleIndex(); i < rawData.length - 2; i = i + 2) { sample = (short) rawData[i] + (short) ((rawData[i + 1] << 8) & 0xFF00); sample *= gainFactor; rawData[i] = (byte) (sample & 0xFF); rawData[i + 1] = (byte) ((sample >>> 8) & 0xFF); } }
/** * This function is responsible for muting the fragment of the currently edited .WAV song. * * @param startSecond - the start time from which the song is cutted * @param endSecond - the end time to which the song is cutted */ public void muteSong(int startSecond, int endSecond) { int startIndex = wavTagReader.getFirstSampleIndex() + startSecond * wavTagReader.sampleRate * wavTagReader.numOfChannels * wavTagReader.bitsPerSample / 8; int endIndex = wavTagReader.getFirstSampleIndex() + endSecond * wavTagReader.sampleRate * wavTagReader.numOfChannels * wavTagReader.bitsPerSample / 8; for (int i = startIndex; i < endIndex; i++) rawData[i] = 0; }
/** * This function loads the .WAV song which is to be edited into the RAM memory * * @param fileToOpen - the file to be edited. */ public void loadSong(File fileToOpen) { file = fileToOpen; int fileLength = (int) file.length(); rawData = new byte[fileLength]; try { fileInputStream = new FileInputStream(file); dataIn = new BufferedInputStream(fileInputStream); for (int i = 0; i < fileLength; ++i) rawData[i] = (byte) dataIn.read(); wavTagReader = new WavTagReader(this.rawData); wavTagReader.readHeader(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * This function cuts the fragment of song, and puts it in another byte array. It assigns also the * header of the edited song to the cut fragment * * @param startSecond - the start second to cut * @param endSecond - the end second to cut * @return reference to the array with cut fragment. */ public byte[] cutSong(int startSecond, int endSecond) { int startIndex = wavTagReader.getFirstSampleIndex() + startSecond * wavTagReader.sampleRate * wavTagReader.numOfChannels * wavTagReader.bitsPerSample / 8; int endIndex = wavTagReader.getFirstSampleIndex() + endSecond * wavTagReader.sampleRate * wavTagReader.numOfChannels * wavTagReader.bitsPerSample / 8; byte cutSong[] = new byte[wavTagReader.getFirstSampleIndex() + (endIndex - startIndex)]; /// Copy the song header except for data field size for (int i = 0; i < wavTagReader.getFirstSampleIndex() - 4; i++) { cutSong[i] = rawData[i]; } /// Set the new data size int samplesSize = endIndex - startIndex; int firstSampleIndex = wavTagReader.getFirstSampleIndex(); for (int i = 0; i < 4; i++) { cutSong[firstSampleIndex - 4 + i] = (byte) ((samplesSize >>> (i * 8)) & 0xFF); } int byteIndex = startIndex; /// Copy the data for (int i = wavTagReader.getFirstSampleIndex(); i < cutSong.length; i++) { cutSong[i] = rawData[byteIndex++]; } return cutSong; }