From 034241f6ce910996eba58a11b6a4a7e21da1ac3c Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 6 Feb 2001 08:18:00 +0000 Subject: [PATCH] Fixed up the ripping process as well as progress reporting. Made progress reporting work for the cdparanoia ripper. git-svn-id: https://samskivert.googlecode.com/svn/trunk@37 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/robodj/convert/CDParanoiaRipper.java | 56 +++++++++++++++++-- .../convert/ConversionProgressListener.java | 4 +- .../src/java/robodj/convert/RipTest.java | 22 +++++++- .../src/java/robodj/convert/Ripper.java | 6 +- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/projects/robodj/src/java/robodj/convert/CDParanoiaRipper.java b/projects/robodj/src/java/robodj/convert/CDParanoiaRipper.java index 3a79d6a9..6306dbc5 100644 --- a/projects/robodj/src/java/robodj/convert/CDParanoiaRipper.java +++ b/projects/robodj/src/java/robodj/convert/CDParanoiaRipper.java @@ -1,5 +1,5 @@ // -// $Id: CDParanoiaRipper.java,v 1.4 2000/12/10 07:01:24 mdb Exp $ +// $Id: CDParanoiaRipper.java,v 1.5 2001/02/06 08:18:00 mdb Exp $ package robodj.convert; @@ -106,7 +106,7 @@ public class CDParanoiaRipper implements Ripper } } - public void ripTrack (int index, String target, + public void ripTrack (TrackInfo[] info, int index, String target, ConversionProgressListener listener) throws ConvertException { @@ -116,6 +116,14 @@ public class CDParanoiaRipper implements Ripper cmd.append(" ").append(index); // add track number cmd.append(" ").append(target); // add output file name + // we'll need this for later + RE regex; + try { + regex = new RE("^##: -?\\d+ \\[(\\S+)\\] @ (\\d+)"); + } catch (REException ree) { + throw new ConvertException("Can't compile regexp?! " + ree); + } + try { // fork off a cdparanoia process to read the TOC Runtime rt = Runtime.getRuntime(); @@ -125,9 +133,41 @@ public class CDParanoiaRipper implements Ripper BufferedInputStream bin = new BufferedInputStream(in); DataInputStream din = new DataInputStream(bin); - // read output from the subprocess and chuck it for now - while (din.readLine() != null) { - // la la la + // read output from the subprocess + String out; + + // figure out what our progress boundaries are + long tstart = info[index-1].offset * MAGIC_FACTOR; + long tlength = info[index-1].length * MAGIC_FACTOR; + int lastReported = 0; + + while ((out = din.readLine()) != null) { + // if they don't care about the output then neither do we + if (listener == null) { + continue; + } + + // parse the output + REMatch match = regex.getMatch(out); + if (match != null) { + String action = match.toString(1); + long offset = -1L; + try { + offset = Long.parseLong(match.toString(2)); + } catch (NumberFormatException nfe) { + // System.err.println("Malformed position info: " + out); + continue; + } + + int pctDone = (int)(100 * (offset - tstart) / tlength); + if (pctDone - lastReported >= 1) { + listener.updateProgress(pctDone); + lastReported = pctDone; + } + + } else { + // System.out.println("Couldn't match: " + out); + } } // check the return value of the process @@ -146,9 +186,15 @@ public class CDParanoiaRipper implements Ripper "ripper process to exit."); } + // if everything was successful, report completion + listener.updateProgress(100); + } catch (IOException ioe) { throw new ConvertException( "Error communicating with ripper:\n" + ioe); } } + + // cdparanoia reports length in sectors and progress in + protected static final int MAGIC_FACTOR = 1176; } diff --git a/projects/robodj/src/java/robodj/convert/ConversionProgressListener.java b/projects/robodj/src/java/robodj/convert/ConversionProgressListener.java index db63705a..32af89e4 100644 --- a/projects/robodj/src/java/robodj/convert/ConversionProgressListener.java +++ b/projects/robodj/src/java/robodj/convert/ConversionProgressListener.java @@ -1,5 +1,5 @@ // -// $Id: ConversionProgressListener.java,v 1.1 2000/10/30 22:21:11 mdb Exp $ +// $Id: ConversionProgressListener.java,v 1.2 2001/02/06 08:18:00 mdb Exp $ package robodj.convert; @@ -18,5 +18,5 @@ public interface ConversionProgressListener * herein will be mapped to a global progress indication by the * calling software (in the case of converting an entire CD). */ - public void updateProgress (float percentComplete); + public void updateProgress (int percentComplete); } diff --git a/projects/robodj/src/java/robodj/convert/RipTest.java b/projects/robodj/src/java/robodj/convert/RipTest.java index 01c8dc82..7fdc331d 100644 --- a/projects/robodj/src/java/robodj/convert/RipTest.java +++ b/projects/robodj/src/java/robodj/convert/RipTest.java @@ -1,5 +1,5 @@ // -// $Id: RipTest.java,v 1.2 2000/10/30 22:21:11 mdb Exp $ +// $Id: RipTest.java,v 1.3 2001/02/06 08:18:00 mdb Exp $ package robodj.convert; @@ -79,8 +79,26 @@ public class RipTest System.out.println("Matched no tracks?!"); } else { - // finally try the CDDB lookup + // try the CDDB lookup lookupCDDB("us.cddb.com", info); + + // rip a track, for fun + int track = 2; + System.out.println("Ripping track " + track + + " [" + info[track-1].offset + + ", " + info[track-1].length + "]..."); + + // report our progress + ConversionProgressListener listener = + new ConversionProgressListener() { + public void updateProgress (int percentDone) + { + System.out.println("Percent done: " + + percentDone + "%"); + } + }; + ripper.ripTrack(info, track, "/tmp/track" + track + ".wav", + listener); } } catch (Exception e) { diff --git a/projects/robodj/src/java/robodj/convert/Ripper.java b/projects/robodj/src/java/robodj/convert/Ripper.java index 53027275..055edb8f 100644 --- a/projects/robodj/src/java/robodj/convert/Ripper.java +++ b/projects/robodj/src/java/robodj/convert/Ripper.java @@ -1,5 +1,5 @@ // -// $Id: Ripper.java,v 1.2 2000/10/30 22:21:11 mdb Exp $ +// $Id: Ripper.java,v 1.3 2001/02/06 08:18:00 mdb Exp $ package robodj.convert; @@ -46,6 +46,8 @@ public interface Ripper * notification, it should communicate it to the supplied progress * listener. The track should be ripped in WAV format. * + * @param info the track info as returned by + * getTrackInfo. This is necessary to compute progress. * @param index the track number of the track to rip (starting at 1 * since CDs start counting tracks at 1). * @param target the path to the file into which the track should be @@ -58,7 +60,7 @@ public interface Ripper * trying to rip the specified track (like lack of access to the CDROM * device or any other errors). */ - public void ripTrack (int index, String target, + public void ripTrack (TrackInfo[] info, int index, String target, ConversionProgressListener listener) throws ConvertException; }