MP3 Player without external hardware MP3 Player without external hardware. A software based MP3 player based on a modified version of libmad. Mono output (at the moment) via AnalogOut. Files are read from an USB drive. This is a demo program, it plays only one file at the moment. Documentation is in "main.cpp" and "config.h"

Dependencies:   mbed

Revision:
2:f28cf0afd021
Parent:
0:7627c79db971
--- a/decoder.cpp	Fri Nov 26 13:05:34 2010 +0000
+++ b/decoder.cpp	Sat Nov 27 17:27:33 2010 +0000
@@ -76,9 +76,6 @@
 
   decoder->options      = 0;
 
-  decoder->async.pid    = 0;
-  decoder->async.in     = -1;
-  decoder->async.out    = -1;
 
   decoder->sync         = 0;
 
@@ -94,202 +91,9 @@
 
 int mad_decoder_finish(struct mad_decoder *decoder)
 {
-# if defined(USE_ASYNC)
-  if (decoder->mode == MAD_DECODER_MODE_ASYNC && decoder->async.pid) {
-    pid_t pid;
-    int status;
-
-    close(decoder->async.in);
-
-    do
-      pid = waitpid(decoder->async.pid, &status, 0);
-    while (pid == -1 && errno == EINTR);
-
-    decoder->mode = -1;
-
-    close(decoder->async.out);
-
-    decoder->async.pid = 0;
-    decoder->async.in  = -1;
-    decoder->async.out = -1;
-
-    if (pid == -1)
-      return -1;
-
-    return (!WIFEXITED(status) || WEXITSTATUS(status)) ? -1 : 0;
-  }
-# endif
-
   return 0;
 }
 
-# if defined(USE_ASYNC)
-static
-enum mad_flow send_io(int fd, void const *data, size_t len)
-{
-  char const *ptr = data;
-  ssize_t count;
-
-  while (len) {
-    do
-      count = write(fd, ptr, len);
-    while (count == -1 && errno == EINTR);
-
-    if (count == -1)
-      return MAD_FLOW_BREAK;
-
-    len -= count;
-    ptr += count;
-  }
-
-  return MAD_FLOW_CONTINUE;
-}
-
-static
-enum mad_flow receive_io(int fd, void *buffer, size_t len)
-{
-  char *ptr = buffer;
-  ssize_t count;
-
-  while (len) {
-    do
-      count = read(fd, ptr, len);
-    while (count == -1 && errno == EINTR);
-
-    if (count == -1)
-      return (errno == EAGAIN) ? MAD_FLOW_IGNORE : MAD_FLOW_BREAK;
-    else if (count == 0)
-      return MAD_FLOW_STOP;
-
-    len -= count;
-    ptr += count;
-  }
-
-  return MAD_FLOW_CONTINUE;
-}
-
-static
-enum mad_flow receive_io_blocking(int fd, void *buffer, size_t len)
-{
-  int flags, blocking;
-  enum mad_flow result;
-
-  flags = fcntl(fd, F_GETFL);
-  if (flags == -1)
-    return MAD_FLOW_BREAK;
-
-  blocking = flags & ~O_NONBLOCK;
-
-  if (blocking != flags &&
-      fcntl(fd, F_SETFL, blocking) == -1)
-    return MAD_FLOW_BREAK;
-
-  result = receive_io(fd, buffer, len);
-
-  if (flags != blocking &&
-      fcntl(fd, F_SETFL, flags) == -1)
-    return MAD_FLOW_BREAK;
-
-  return result;
-}
-
-static
-enum mad_flow send(int fd, void const *message, unsigned int size)
-{
-  enum mad_flow result;
-
-  /* send size */
-
-  result = send_io(fd, &size, sizeof(size));
-
-  /* send message */
-
-  if (result == MAD_FLOW_CONTINUE)
-    result = send_io(fd, message, size);
-
-  return result;
-}
-
-static
-enum mad_flow receive(int fd, void **message, unsigned int *size)
-{
-  enum mad_flow result;
-  unsigned int actual;
-
-  if (*message == 0)
-    *size = 0;
-
-  /* receive size */
-
-  result = receive_io(fd, &actual, sizeof(actual));
-
-  /* receive message */
-
-  if (result == MAD_FLOW_CONTINUE) {
-    if (actual > *size)
-      actual -= *size;
-    else {
-      *size  = actual;
-      actual = 0;
-    }
-
-    if (*size > 0) {
-      if (*message == 0) {
-        *message = malloc(*size);
-        if (*message == 0)
-          return MAD_FLOW_BREAK;
-      }
-
-      result = receive_io_blocking(fd, *message, *size);
-    }
-
-    /* throw away remainder of message */
-
-    while (actual && result == MAD_FLOW_CONTINUE) {
-      char sink[256];
-      unsigned int len;
-
-      len = actual > sizeof(sink) ? sizeof(sink) : actual;
-
-      result = receive_io_blocking(fd, sink, len);
-
-      actual -= len;
-    }
-  }
-
-  return result;
-}
-
-static
-enum mad_flow check_message(struct mad_decoder *decoder)
-{
-  enum mad_flow result;
-  void *message = 0;
-  unsigned int size;
-
-  result = receive(decoder->async.in, &message, &size);
-
-  if (result == MAD_FLOW_CONTINUE) {
-    if (decoder->message_func == 0)
-      size = 0;
-    else {
-      result = decoder->message_func(decoder->cb_data, message, &size);
-
-      if (result == MAD_FLOW_IGNORE ||
-          result == MAD_FLOW_BREAK)
-        size = 0;
-    }
-
-    if (send(decoder->async.out, message, size) != MAD_FLOW_CONTINUE)
-      result = MAD_FLOW_BREAK;
-  }
-
-  if (message)
-    free(message);
-
-  return result;
-}
-# endif
 
 static
 enum mad_flow error_default(void *data, struct mad_stream *stream,
@@ -357,20 +161,6 @@
     }
 
     while (1) {
-# if defined(USE_ASYNC)
-      if (decoder->mode == MAD_DECODER_MODE_ASYNC) {
-        switch (check_message(decoder)) {
-        case MAD_FLOW_IGNORE:
-        case MAD_FLOW_CONTINUE:
-          break;
-        case MAD_FLOW_BREAK:
-          goto fail;
-        case MAD_FLOW_STOP:
-          goto done;
-        }
-      }
-# endif
-
       if (decoder->header_func) {
         if (mad_header_decode(&frame->header, stream) == -1) {
           if (!MAD_RECOVERABLE(stream->error))
@@ -461,69 +251,6 @@
   return result;
 }
 
-# if defined(USE_ASYNC)
-static
-int run_async(struct mad_decoder *decoder)
-{
-  pid_t pid;
-  int ptoc[2], ctop[2], flags;
-
-  if (pipe(ptoc) == -1)
-    return -1;
-
-  if (pipe(ctop) == -1) {
-    close(ptoc[0]);
-    close(ptoc[1]);
-    return -1;
-  }
-
-  flags = fcntl(ptoc[0], F_GETFL);
-  if (flags == -1 ||
-      fcntl(ptoc[0], F_SETFL, flags | O_NONBLOCK) == -1) {
-    close(ctop[0]);
-    close(ctop[1]);
-    close(ptoc[0]);
-    close(ptoc[1]);
-    return -1;
-  }
-
-  pid = fork();
-  if (pid == -1) {
-    close(ctop[0]);
-    close(ctop[1]);
-    close(ptoc[0]);
-    close(ptoc[1]);
-    return -1;
-  }
-
-  decoder->async.pid = pid;
-
-  if (pid) {
-    /* parent */
-
-    close(ptoc[0]);
-    close(ctop[1]);
-
-    decoder->async.in  = ctop[0];
-    decoder->async.out = ptoc[1];
-
-    return 0;
-  }
-
-  /* child */
-
-  close(ptoc[1]);
-  close(ctop[0]);
-
-  decoder->async.in  = ptoc[0];
-  decoder->async.out = ctop[1];
-
-  _exit(run_sync(decoder));
-
-  /* not reached */
-  return -1;
-}
-# endif
 
 /*
  * NAME:        decoder->run()
@@ -540,25 +267,28 @@
     break;
 
   case MAD_DECODER_MODE_ASYNC:
-# if defined(USE_ASYNC)
-    run = run_async;
-# endif
     break;
   }
 
   if (run == 0)
     return -1;
-
+  // static struct mad_sync_s sync_mem;
   decoder->sync = (struct mad_sync_s *)malloc(sizeof(*decoder->sync));
-  decoder->sync->synth = (struct mad_synth *)AHBMEM;   // 12kb in upper memory
+#if defined(TARGET_LPC1768)  
+  decoder->sync->synth = (struct mad_synth *)mad_malloc(sizeof(struct mad_synth));
+#else
+  decoder->sync->synth = (struct mad_synth *)malloc(sizeof(struct mad_synth));
+#endif
   if (decoder->sync == 0)
     return -1;
 
   result = run(decoder);
-
+#if !defined(TARGET_LPC1768)  
+  free(decoder->sync->synth);
+#endif  
   free(decoder->sync);
   decoder->sync = 0;
-
+  reset_ahb_mem();
   return result;
 }
 
@@ -569,14 +299,5 @@
 int mad_decoder_message(struct mad_decoder *decoder,
                         void *message, unsigned int *len)
 {
-# if defined(USE_ASYNC)
-  if (decoder->mode != MAD_DECODER_MODE_ASYNC ||
-      send(decoder->async.out, message, *len) != MAD_FLOW_CONTINUE ||
-      receive(decoder->async.in, &message, len) != MAD_FLOW_CONTINUE)
-    return -1;
-
-  return 0;
-# else
   return -1;
-# endif
 }