private Status openStream() { Status status = Status.OK; boolean result = flac.setStreamConfiguration(sc); result = result & flac.setEncodingConfiguration(ec); if (!result) status = Status.INTERNAL_ERROR; else { try { fout = new FLACFileOutputStream(outFile.getPath()); } catch (Exception e) { e.printStackTrace(); } if (status == Status.OK) { flac.setOutputStream(fout); try { flac.openFLACStream(); } catch (IOException e) { status = Status.INTERNAL_ERROR; } } else status = Status.OUTPUT_FILE_ERROR; } return status; }
/** * Encode the given input wav file to an output file. * * @param inputFile Input wav file to encode. * @param outputFile Output file to write FLAC stream to. If file exists, it will be overwritten * without prompting. * @return Status flag for encode */ public javaFlacEncoder.FLAC_FileEncoder.Status encode(File inputFile, File outputFile) { javaFlacEncoder.FLAC_FileEncoder.Status status = javaFlacEncoder.FLAC_FileEncoder.Status.FULL_ENCODE; this.outFile = outputFile; // take file and initial configuration. // open file AudioInputStream sin = null; AudioFormat format = null; // File inputFile = new File("encoderTest.wav"); try { sin = AudioSystem.getAudioInputStream(inputFile); } catch (IOException e) { status = javaFlacEncoder.FLAC_FileEncoder.Status.FILE_IO_ERROR; } catch (UnsupportedAudioFileException e) { status = javaFlacEncoder.FLAC_FileEncoder.Status.UNSUPPORTED_FILE; } finally { if (status != javaFlacEncoder.FLAC_FileEncoder.Status.FULL_ENCODE) return status; } try { format = sin.getFormat(); // sanitize and optimize configurations adjustConfigurations(format); // open stream openStream(); int frameSize = format.getFrameSize(); int sampleSize = format.getSampleSizeInBits(); int bytesPerSample = sampleSize / 8; if (sampleSize % 8 != 0) { // end processing now Exception newEx = new Exception(Status.UNSUPPORTED_SAMPLE_SIZE.name()); throw newEx; } int channels = format.getChannels(); boolean bigEndian = format.isBigEndian(); byte[] samplesIn = new byte[(int) MAX_READ]; int samplesRead; int framesRead; int[] sampleData = new int[MAX_READ * channels / frameSize]; int blockSize = sc.getMaxBlockSize(); int unencodedSamples = 0; int totalSamples = 0; while ((samplesRead = sin.read(samplesIn, 0, MAX_READ)) != -1) { // System.err.println("Read: " + read); framesRead = samplesRead / (frameSize); if (bigEndian) { for (int i = 0; i < framesRead * channels; i++) { int lower8Mask = 255; int temp = 0; int totalTemp = 0; for (int x = bytesPerSample - 1; x >= 0; x++) { int upShift = 8 * x; if (x == 0) // don't mask...we want sign temp = ((samplesIn[bytesPerSample * i + x]) << upShift); else temp = ((samplesIn[bytesPerSample * i + x] & lower8Mask) << upShift); totalTemp = totalTemp | temp; } sampleData[i] = totalTemp; } } else { for (int i = 0; i < framesRead * channels; i++) { int lower8Mask = 255; int temp = 0; int totalTemp = 0; for (int x = 0; x < bytesPerSample; x++) { int upShift = 8 * x; if (x == bytesPerSample - 1) // don't mask...we want // sign temp = ((samplesIn[bytesPerSample * i + x]) << upShift); else temp = ((samplesIn[bytesPerSample * i + x] & lower8Mask) << upShift); totalTemp = totalTemp | temp; } sampleData[i] = totalTemp; } } if (framesRead > 0) { flac.addSamples(sampleData, framesRead); unencodedSamples += framesRead; } // if(unencodedSamples > blockSize*100) { if (useThreads) // Thread.yield();// unencodedSamples -= flac.t_encodeSamples(unencodedSamples, false); else unencodedSamples -= flac.encodeSamples(unencodedSamples, false); totalSamples += unencodedSamples; // unencodedSamples = 0; // } // System.err.println("read : "+ samplesRead); } totalSamples += unencodedSamples; if (useThreads) unencodedSamples -= flac.t_encodeSamples(unencodedSamples, true); else unencodedSamples -= flac.encodeSamples(unencodedSamples, true); // unencodedSamples = 0; lastTotalSamples = totalSamples; } catch (IOException e) { status = javaFlacEncoder.FLAC_FileEncoder.Status.FILE_IO_ERROR; } catch (Exception e) { status = javaFlacEncoder.FLAC_FileEncoder.Status.GENERAL_ERROR; String message = e.getMessage(); if (message == null) { e.printStackTrace(); } else if (message.equals( javaFlacEncoder.FLAC_FileEncoder.Status.UNSUPPORTED_SAMPLE_SIZE.name())) status = javaFlacEncoder.FLAC_FileEncoder.Status.UNSUPPORTED_SAMPLE_SIZE; } // System.err.print("LastTotalSamples: "+lastTotalSamples); return status; }