OSDN Git Service

Add sf_writef_int to tiny libsndfile
authorGlenn Kasten <gkasten@google.com>
Thu, 18 Jun 2015 22:38:15 +0000 (15:38 -0700)
committerGlenn Kasten <gkasten@google.com>
Fri, 19 Jun 2015 20:12:40 +0000 (13:12 -0700)
Change-Id: I301781c079b0c6501a940dd7f6b782dd876d2899

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

index 74850ab..e24632b 100644 (file)
@@ -69,6 +69,7 @@ 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);
 sf_count_t sf_writef_float(SNDFILE *handle, const float *ptr, sf_count_t desired);
+sf_count_t sf_writef_int(SNDFILE *handle, const int *ptr, sf_count_t desired);
 
 __END_DECLS
 
index e35c75d..ca03a72 100644 (file)
@@ -303,7 +303,7 @@ static SNDFILE *sf_open_write(const char *path, SF_INFO *info)
             (info->channels > 0 && info->channels <= 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 +343,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;
@@ -626,3 +629,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;
+}