OSDN Git Service

Snap for 5706862 from e907286277a038c0bed1916c5dfcb56a15c58349 to qt-c2f2-release
[android-x86/system-media.git] / audio_utils / tinysndfile.c
index e35c75d..e94bb65 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <system/audio.h>
 #include <audio_utils/sndfile.h>
 #include <audio_utils/primitives.h>
 #ifdef HAVE_STDERR
@@ -181,8 +182,7 @@ static SNDFILE *sf_open_read(const char *path, SF_INFO *info)
                 fseek(stream, (long) (chunkSize - minSize), SEEK_CUR);
             }
             unsigned channels = little2u(&fmt[2]);
-            // FIXME FCC_8
-            if (channels != 1 && channels != 2 && channels != 4 && channels != 6 && channels != 8) {
+            if ((channels < 1) || (channels > FCC_8)) {
 #ifdef HAVE_STDERR
                 fprintf(stderr, "unsupported channels %u\n", channels);
 #endif
@@ -299,11 +299,10 @@ static SNDFILE *sf_open_write(const char *path, SF_INFO *info)
     int sub = info->format & SF_FORMAT_SUBMASK;
     if (!(
             (info->samplerate > 0) &&
-            // FIXME FCC_8
-            (info->channels > 0 && info->channels <= 8) &&
+            (info->channels > 0 && info->channels <= FCC_8) &&
             ((info->format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) &&
             (sub == SF_FORMAT_PCM_16 || sub == SF_FORMAT_PCM_U8 || sub == SF_FORMAT_FLOAT ||
-                sub == SF_FORMAT_PCM_24)
+                sub == SF_FORMAT_PCM_24 || sub == SF_FORMAT_PCM_32)
           )) {
         return NULL;
     }
@@ -343,6 +342,9 @@ static SNDFILE *sf_open_write(const char *path, SF_INFO *info)
     case SF_FORMAT_PCM_24:
         bitsPerSample = 24;
         break;
+    case SF_FORMAT_PCM_32:
+        bitsPerSample = 32;
+        break;
     default:    // not reachable
         bitsPerSample = 0;
         break;
@@ -487,11 +489,8 @@ sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desiredFrames)
     handle->remaining -= actualFrames;
     switch (format) {
     case SF_FORMAT_PCM_U8:
-#if 0
-        // TODO - implement
         memcpy_to_float_from_u8(ptr, (const unsigned char *) temp,
                 actualFrames * handle->info.channels);
-#endif
         free(temp);
         break;
     case SF_FORMAT_PCM_16:
@@ -538,11 +537,8 @@ sf_count_t sf_readf_int(SNDFILE *handle, int *ptr, sf_count_t desiredFrames)
     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:
@@ -626,3 +622,21 @@ sf_count_t sf_writef_float(SNDFILE *handle, const float *ptr, sf_count_t desired
     handle->remaining += actualFrames;
     return actualFrames;
 }
+
+sf_count_t sf_writef_int(SNDFILE *handle, const int *ptr, sf_count_t desiredFrames)
+{
+    if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)
+        return 0;
+    size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
+    size_t actualBytes = 0;
+    switch (handle->info.format & SF_FORMAT_SUBMASK) {
+    case SF_FORMAT_PCM_32:
+        actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream);
+        break;
+    default:    // transcoding from other formats not yet implemented
+        break;
+    }
+    size_t actualFrames = actualBytes / handle->bytesPerFrame;
+    handle->remaining += actualFrames;
+    return actualFrames;
+}