]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
mux multi-channel audio files to mono to support playing stereo sound files
authorAnthony Minessale <anthony.minessale@gmail.com>
Thu, 15 Jan 2009 17:47:21 +0000 (17:47 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Thu, 15 Jan 2009 17:47:21 +0000 (17:47 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11226 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_resample.h
src/switch_core_file.c
src/switch_resample.c

index 86c4345be57f7ebe6c3a543e993b68138aa36e69..8bd3543160f15779e34058bd065ce7e9b5d32a33 100644 (file)
@@ -168,6 +168,8 @@ SWITCH_DECLARE(void) switch_change_sln_volume(int16_t *data, uint32_t samples, i
 
 SWITCH_DECLARE(uint32_t) switch_merge_sln(int16_t *data, uint32_t samples, int16_t *other_data, uint32_t other_samples);
 
+SWITCH_DECLARE(void) switch_mux_channels(int16_t *data, uint32_t samples, uint32_t channels);
+
 SWITCH_END_EXTERN_C
 #endif
 /* For Emacs:
index f11b0a8df8ce3eed746f6e6c89ddc94690976204..852f3528429611265635bcb329a3adab13e8a298 100644 (file)
@@ -120,7 +120,11 @@ SWITCH_DECLARE(switch_status_t) switch_core_perform_file_open(const char *file,
        if (fh->pre_buffer_datalen) {
                //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Prebuffering %d bytes\n", (int)fh->pre_buffer_datalen);
                switch_buffer_create_dynamic(&fh->pre_buffer, fh->pre_buffer_datalen * fh->channels, fh->pre_buffer_datalen * fh->channels / 2, 0);
-               fh->pre_buffer_data = switch_core_alloc(fh->memory_pool, fh->pre_buffer_datalen);
+               fh->pre_buffer_data = switch_core_alloc(fh->memory_pool, fh->pre_buffer_datalen * fh->channels);
+       }
+
+       if (fh->channels > 1) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "File has %d channels, muxing to mono will occur.\n", fh->channels);
        }
 
        switch_set_flag(fh, SWITCH_FILE_OPEN);
@@ -164,6 +168,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_read(switch_file_handle_t *fh,
                                if ((status = fh->file_interface->file_read(fh, fh->pre_buffer_data, &rlen)) != SWITCH_STATUS_SUCCESS || !rlen) {
                                        switch_set_flag(fh, SWITCH_FILE_BUFFER_DONE);
                                } else {
+                                       if (fh->channels > 1) {
+                                               switch_mux_channels((int16_t *)fh->pre_buffer_data, rlen, fh->channels);
+                                       }
                                        switch_buffer_write(fh->pre_buffer, fh->pre_buffer_data, asis ? rlen : rlen * 2);
                                }
                        }
@@ -185,6 +192,11 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_read(switch_file_handle_t *fh,
                        switch_set_flag(fh, SWITCH_FILE_DONE);
                        goto top;
                }
+
+               if (fh->channels > 1) {
+                       switch_mux_channels((int16_t *)data, *len, fh->channels);
+               }
+
        }
 
 
index 0abb3ec63c13482d6e59f20e0114368ef1e4c01c..478df61ca3153ae8faac9b13f983b6592802606b 100644 (file)
@@ -244,6 +244,27 @@ SWITCH_DECLARE(uint32_t) switch_merge_sln(int16_t *data, uint32_t samples, int16
        return x;
 }
 
+SWITCH_DECLARE(void) switch_mux_channels(int16_t *data, uint32_t samples, uint32_t channels)
+{
+       int16_t *buf;
+       switch_size_t len = samples * sizeof(int16_t);
+       uint32_t i = 0, j = 0, k = 0;
+
+       switch_zmalloc(buf, len);
+       
+       for(i = 0; i < samples; i++) {
+               for (j = 0; j < channels; j++) {
+                       int32_t z = buf[i] + data[k++];
+                       switch_normalize_to_16bit(z);
+                       buf[i] = (int16_t) z;
+               }
+       }
+
+       memcpy(data, buf, len);
+       free(buf);
+
+}
+
 SWITCH_DECLARE(void) switch_change_sln_volume(int16_t *data, uint32_t samples, int32_t vol)
 {
        double newrate = 0;