chiark / gitweb /
A batch of copyright date updates.
[disorder] / plugins / tracklength.c
index a31792ecade3dd24dbad3c440c03f464c49de844..7f0866ed8aa1e9d7c714c982d3714cda73335b47 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2005, 2007 Richard Kettlewell
+ * Copyright (C) 2004, 2005, 2007, 2008, 2010 Richard Kettlewell
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * Currently implements MP3, OGG, FLAC and WAV.
  */
-
-#include <config.h>
-
-#include <string.h>
-#include <stdio.h>
-#include <math.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <errno.h>
-
-#include <vorbis/vorbisfile.h>
-#include <mad.h>
-#include <FLAC/stream_decoder.h>
-
-
-#include <disorder.h>
-
-#include "madshim.h"
-#include "wav.h"
-
-static void *mmap_file(const char *path, size_t *lengthp) {
-  int fd;
-  void *base;
-  struct stat sb;
-  
-  if((fd = open(path, O_RDONLY)) < 0) {
-    disorder_error(errno, "error opening %s", path);
-    return 0;
-  }
-  if(fstat(fd, &sb) < 0) {
-    disorder_error(errno, "error calling stat on %s", path);
-    goto error;
-  }
-  if(sb.st_size == 0)                  /* can't map 0-length files */
-    goto error;
-  if((base = mmap(0, sb.st_size, PROT_READ,
-                 MAP_SHARED, fd, 0)) == (void *)-1) {
-    disorder_error(errno, "error calling mmap on %s", path);
-    goto error;
-  }
-  *lengthp = sb.st_size;
-  close(fd);
-  return base;
-error:
-  close(fd);
-  return 0;
-}
-
-static long tl_mp3(const char *path) {
-  size_t length;
-  void *base;
-  buffer b;
-
-  if(!(base = mmap_file(path, &length))) return -1;
-  b.duration = mad_timer_zero;
-  scan_mp3(base, length, &b);
-  munmap(base, length);
-  return b.duration.seconds + !!b.duration.fraction;
-}
-
-static long tl_ogg(const char *path) {
-  OggVorbis_File vf;
-  FILE *fp = 0;
-  double length;
-
-  if(!path) goto error;
-  if(!(fp = fopen(path, "rb"))) goto error;
-  if(ov_open(fp, &vf, 0, 0)) goto error;
-  fp = 0;
-  length = ov_time_total(&vf, -1);
-  ov_clear(&vf);
-  return ceil(length);
-error:
-  if(fp) fclose(fp);
-  return -1;
-}
-
-static long tl_wav(const char *path) {
-  struct wavfile f[1];
-  int err, sample_frame_size;
-  long duration;
-
-  if((err = wav_init(f, path))) {
-    disorder_error(err, "error opening %s", path); 
-    return -1;
-  }
-  sample_frame_size = (f->bits + 7) / 8 * f->channels;
-  if(sample_frame_size) {
-    const long long n_samples = f->datasize / sample_frame_size;
-    duration = (n_samples + f->rate - 1) / f->rate;
-  } else
-    duration = -1;
-  wav_destroy(f);
-  return duration;
-}
-
-/* libFLAC's "simplified" interface is rather heavyweight... */
-
-struct flac_state {
-  long duration;
-  const char *path;
-};
-
-static void flac_metadata(const FLAC__StreamDecoder attribute((unused)) *decoder,
-                         const FLAC__StreamMetadata *metadata,
-                         void *client_data) {
-  struct flac_state *const state = client_data;
-  const FLAC__StreamMetadata_StreamInfo *const stream_info
-    = &metadata->data.stream_info;
-
-  if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
-    /* FLAC uses 0 to mean unknown and conveniently so do we */
-    state->duration = (stream_info->total_samples
-                      + stream_info->sample_rate - 1)
-           / stream_info->sample_rate;
-}
-
-static void flac_error(const FLAC__StreamDecoder attribute((unused)) *decoder,
-                      FLAC__StreamDecoderErrorStatus status,
-                      void *client_data) {
-  const struct flac_state *const state = client_data;
-
-  disorder_error(0, "error decoding %s: %s", state->path,
-                FLAC__StreamDecoderErrorStatusString[status]);
-}
-
-static FLAC__StreamDecoderWriteStatus flac_write
-    (const FLAC__StreamDecoder attribute((unused)) *decoder,
-     const FLAC__Frame attribute((unused)) *frame,
-     const FLAC__int32 attribute((unused)) *const buffer_[],
-     void attribute((unused)) *client_data) {
-  const struct flac_state *const state = client_data;
-
-  if(state->duration >= 0)
-    return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
-  else
-    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
-}
-
-static long tl_flac(const char *path) {
-  FLAC__StreamDecoder *sd = 0;
-  FLAC__StreamDecoderInitStatus is;
-  struct flac_state state[1];
-
-  state->duration = -1;                        /* error */
-  state->path = path;
-  if(!(sd = FLAC__stream_decoder_new())) {
-    disorder_error(0, "FLAC__stream_decoder_new failed");
-    goto fail;
-  }
-  if((is = FLAC__stream_decoder_init_file(sd, path, flac_write, flac_metadata,
-                                         flac_error, state))) {
-    disorder_error(0, "FLAC__stream_decoder_init_file %s: %s",
-                  path, FLAC__StreamDecoderInitStatusString[is]);
-    goto fail;
-  }
-  FLAC__stream_decoder_process_until_end_of_metadata(sd);
-fail:
-  if(sd)
-    FLAC__stream_decoder_delete(sd);
-  return state->duration;
-}
+#include "tracklength.h"
 
 static const struct {
   const char *ext;