OSDN Git Service

Squashed commit of the following:
authorGlenn Kasten <gkasten@google.com>
Thu, 5 Jun 2014 03:30:23 +0000 (20:30 -0700)
committerGlenn Kasten <gkasten@google.com>
Thu, 5 Jun 2014 03:35:18 +0000 (03:35 +0000)
commit b80e690164b152a2fc8ae55c549cf6082c29719a
Author: Andy Hung <hunga@google.com>
Date:   Thu May 22 18:26:07 2014 -0700

    Add sf_readf_int to read files as 32 bit PCM int

    Change-Id: Ic98cca69926309b1a8f77cc702bc54aba20e3996

commit a0d0fb2d89f5b22cf82dc5a4da52e4bbc3aaffb6
Author: Andy Hung <hunga@google.com>
Date:   Thu May 22 18:17:53 2014 -0700

    Update sf_readf_float to handle 16 and 32 bit PCM

    Change-Id: I0f74ca686f1a86e724057bc483924cea13577ef0

Change-Id: Ie18b611b3025e9b0c566c024b774f2cb2c49ceda

audio_utils/include/audio_utils/sndfile.h
audio_utils/tinysndfile.c

index cf715b4..8bd9fd9 100644 (file)
@@ -63,6 +63,7 @@ void sf_close(SNDFILE *handle);
 // Read interleaved frames and return actual number of frames read
 sf_count_t sf_readf_short(SNDFILE *handle, short *ptr, sf_count_t desired);
 sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desired);
+sf_count_t sf_readf_int(SNDFILE *handle, int *ptr, sf_count_t desired);
 
 // Write interleaved frames and return actual number of frames written
 sf_count_t sf_writef_short(SNDFILE *handle, const short *ptr, sf_count_t desired);
index 98709a6..e95bde5 100644 (file)
@@ -399,25 +399,32 @@ sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desiredFrames)
     // does not check for numeric overflow
     size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
     size_t actualBytes;
+    void *temp = NULL;
     unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
-    actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
+    if (format == SF_FORMAT_PCM_16 || format == SF_FORMAT_PCM_U8) {
+        temp = malloc(desiredBytes);
+        actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream);
+    } else {
+        actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
+    }
     size_t actualFrames = actualBytes / handle->bytesPerFrame;
     handle->remaining -= actualFrames;
     switch (format) {
-#if 0
     case SF_FORMAT_PCM_U8:
-        memcpy_to_float_from_u8(ptr, (const unsigned char *) ptr,
+#if 0
+        // TODO - implement
+        memcpy_to_float_from_u8(ptr, (const unsigned char *) temp,
                 actualFrames * handle->info.channels);
-        break;
 #endif
+        free(temp);
+        break;
     case SF_FORMAT_PCM_16:
-        memcpy_to_float_from_i16(ptr, (const short *) ptr, actualFrames * handle->info.channels);
+        memcpy_to_float_from_i16(ptr, (const short *) temp, actualFrames * handle->info.channels);
+        free(temp);
         break;
-#if 0
     case SF_FORMAT_PCM_32:
         memcpy_to_float_from_i32(ptr, (const int *) ptr, actualFrames * handle->info.channels);
         break;
-#endif
     case SF_FORMAT_FLOAT:
         break;
     default:
@@ -427,6 +434,53 @@ sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desiredFrames)
     return actualFrames;
 }
 
+sf_count_t sf_readf_int(SNDFILE *handle, int *ptr, sf_count_t desiredFrames)
+{
+    if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining ||
+            desiredFrames <= 0) {
+        return 0;
+    }
+    if (handle->remaining < (size_t) desiredFrames) {
+        desiredFrames = handle->remaining;
+    }
+    // does not check for numeric overflow
+    size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
+    void *temp = NULL;
+    unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
+    size_t actualBytes;
+    if (format == SF_FORMAT_PCM_16 || format == SF_FORMAT_PCM_U8) {
+        temp = malloc(desiredBytes);
+        actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream);
+    } else {
+        actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
+    }
+    size_t actualFrames = actualBytes / handle->bytesPerFrame;
+    handle->remaining -= actualFrames;
+    switch (format) {
+    case SF_FORMAT_PCM_U8:
+#if 0
+        // TODO - implement
+        memcpy_to_i32_from_u8(ptr, (const unsigned char *) temp,
+                actualFrames * handle->info.channels);
+#endif
+        free(temp);
+        break;
+    case SF_FORMAT_PCM_16:
+        memcpy_to_i32_from_i16(ptr, (const short *) temp, actualFrames * handle->info.channels);
+        free(temp);
+        break;
+    case SF_FORMAT_PCM_32:
+        break;
+    case SF_FORMAT_FLOAT:
+        memcpy_to_i32_from_float(ptr, (const float *) ptr, actualFrames * handle->info.channels);
+        break;
+    default:
+        memset(ptr, 0, actualFrames * handle->info.channels * sizeof(int));
+        break;
+    }
+    return actualFrames;
+}
+
 sf_count_t sf_writef_short(SNDFILE *handle, const short *ptr, sf_count_t desiredFrames)
 {
     if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)