Soundcard support added
authorFrancoisGrondin <francois.grondin2@usherbrooke.ca>
Mon, 10 Jul 2017 13:24:00 +0000 (09:24 -0400)
committerFrancoisGrondin <francois.grondin2@usherbrooke.ca>
Mon, 10 Jul 2017 13:24:00 +0000 (09:24 -0400)
src/general/interface.c
src/general/interface.h
src/sink/snk_hops.c
src/sink/snk_pots.c
src/sink/snk_tracks.c
src/source/src_hops.c
src/source/src_hops.h
src/utils/pcm.c
src/utils/pcm.h

index 57fa03a9491cb68726dc3c2e9c6e186cad2aa906..697f52ddabb3285181c87c175a547098f9105e7c 100644 (file)
@@ -30,8 +30,8 @@
         // | Soundcard                                                |
         // +----------------------------------------------------------+
 
+            obj->card = 0;
             obj->device = 0;
-            obj->subdevice = 0;
 
         return obj;
 
@@ -67,8 +67,8 @@
         // | Soundcard                                                |
         // +----------------------------------------------------------+
 
+            obj->card = 0;
             obj->device = 0;
-            obj->subdevice = 0;
 
         return obj;
 
         // | Soundcard                                                |
         // +----------------------------------------------------------+
 
+            obj->card = 0;
             obj->device = 0;
-            obj->subdevice = 0;
 
         return obj;        
 
     }
 
-    interface_obj * interface_construct_soundcard(const unsigned int device, const unsigned int subdevice) {
+    interface_obj * interface_construct_soundcard(const unsigned int card, const unsigned int device) {
 
         interface_obj * obj;
 
         // | Soundcard                                                |
         // +----------------------------------------------------------+
 
+            obj->card = card;
             obj->device = device;
-            obj->subdevice = subdevice;
 
         return obj;
 
 
             if (obj->type == interface_soundcard) {
                 
+                clone->card = obj->card;
                 clone->device = obj->device;
-                clone->subdevice = obj->subdevice;
 
             }
 
 
                 case interface_soundcard:
 
-                    printf("type = soundcard, device = %u, subdevice = %u\n",obj->device,obj->subdevice);
+                    printf("type = soundcard, card = %u, device = %u\n",obj->card,obj->device);
 
                 break;
 
index 8da8b024dd1aed189b1d1df92864d536c8ba5d8c..c63540111ec787c07ac4e680af312f995442f9e4 100644 (file)
@@ -23,8 +23,8 @@
         char * ip;
         unsigned int port;
 
+        unsigned int card;
         unsigned int device;
-        unsigned int subdevice;
 
     } interface_obj;
 
@@ -34,7 +34,7 @@
 
     interface_obj * interface_construct_socket(const char * ip, const unsigned int port);
 
-    interface_obj * interface_construct_soundcard(const unsigned int device, const unsigned int subdevice);
+    interface_obj * interface_construct_soundcard(const unsigned int card, const unsigned int device);
 
     interface_obj * interface_clone(const interface_obj * obj);
 
index 6b05f74d2fc39170b153e8224bfa73c0e09902b1..00c9cf46eaa1c10520698ac0491b4b90720107f7 100644 (file)
         signed short sampleShort;
         signed int sampleInt;
         float sample;
+        unsigned int nBytes;
         int rtnValue;
 
         if (obj->in->timeStamp != 0) {
 
+            switch (obj->format->type) {
+
+                case format_bin08: nBytes = 1; break;
+                case format_bin16: nBytes = 2; break;
+                case format_bin24: nBytes = 3; break;
+                case format_bin32: nBytes = 4; break;
+
+            }
+
             for (iSample = 0; iSample < obj->hopSize; iSample++) {
                 
                 for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
 
                     sample = obj->in->hops->array[iChannel][iSample];
-
-                    switch (obj->format->type) {
-                        
-                        case format_bin08:
-
-                            pcm_normalized2signed08bits(sample, obj->bytes);
-                            fwrite(&(obj->bytes[3]), sizeof(char), 1, obj->fp);
-
-                        break;
-
-                        case format_bin16:
-
-                            pcm_normalized2signed16bits(sample, obj->bytes);
-                            fwrite(&(obj->bytes[2]), sizeof(char), 2, obj->fp);
-
-                        break;
-
-                        case format_bin24:
-
-                            pcm_normalized2signed24bits(sample, obj->bytes);
-                            fwrite(&(obj->bytes[1]), sizeof(char), 3, obj->fp);
-
-                        break;
-                        
-                        case format_bin32:
-                        
-                            pcm_normalized2signed32bits(sample, obj->bytes);
-                            fwrite(&(obj->bytes[0]), sizeof(char), 4, obj->fp);
-                        
-                        break;
-                    }
+                    pcm_normalized2signedXXbits(sample, nBytes, obj->bytes);
+                    fwrite(&(obj->bytes[4-nBytes]), sizeof(char), nBytes, obj->fp);
 
                 }
 
index caa1a98b1b0643313c4935a52d751317fc2500c4..92a560076a445fd64870e2cd2c1d736031907cef 100644 (file)
     int snk_pots_process_file(snk_pots_obj * obj) {
 
         int rtnValue;
-        //unsigned int iPot;
 
         if (obj->in->timeStamp != 0) {
 
 
                     fwrite(obj->in->pots->array, sizeof(float), 4 * obj->in->pots->nPots, obj->fp);
 
-                    /*for (iPot = 0; iPot < obj->in->pots->nPots; iPot++) {
-                        printf("(%f,%f,%f)\n",obj->in->pots->array[iPot*4+0],obj->in->pots->array[iPot*4+1],obj->in->pots->array[iPot*4+2]);
-                    }
-                    printf("\n");*/
-
                 break;
 
             }
index 18c116cf269f902453c8be703209f8c23e72e204..cb1cea06f912c958e0eff1d53c0c64de3c84ff31 100644 (file)
     int snk_tracks_process_file(snk_tracks_obj * obj) {
 
         int rtnValue;
-        //unsigned int iTrack;
 
         if (obj->in->timeStamp != 0) {
 
                     fwrite(obj->in->tracks->ids, sizeof(unsigned long long), obj->in->tracks->nTracks, obj->fp);
                     fwrite(obj->in->tracks->array, sizeof(float), 3 * obj->in->tracks->nTracks, obj->fp);
 
-                    /*for (iTrack = 0; iTrack < obj->nTracks; iTrack++) {
-                        printf("(%03llu): (%f,%f,%f)\n",obj->in->tracks->ids[iTrack],obj->in->tracks->array[iTrack*3+0],obj->in->tracks->array[iTrack*3+1],obj->in->tracks->array[iTrack*3+2]);
-                    }
-                    printf("\n");*/
-
                 break;
 
             }
index 0b75de2f1c8b2120943afaed71af7a58b27d0333..2f8ded5a1c48f23729c4de59e5d32b4bdd342c85 100644 (file)
@@ -4,6 +4,7 @@
     src_hops_obj * src_hops_construct(const src_hops_cfg * src_hops_config, const msg_hops_cfg * msg_hops_config) {
 
         src_hops_obj * obj;
+        unsigned int nBytes;
 
         obj = (src_hops_obj *) malloc(sizeof(src_hops_obj));
 
 
         memset(obj->bytes, 0x00, 4 * sizeof(char));
 
+        switch (obj->format->type) {
+            
+            case format_bin08: nBytes = 1; break;
+            case format_bin16: nBytes = 2; break;
+            case format_bin24: nBytes = 3; break;
+            case format_bin32: nBytes = 4; break;
+            default:
+
+                printf("Invalid format.\n");
+                exit(EXIT_FAILURE);
+
+            break;
+
+        }       
+
+        obj->buffer = (char *) malloc(sizeof(char) * obj->hopSize * obj->nChannels * nBytes);
+
         obj->out = (msg_hops_obj *) NULL;
         
         return obj;
@@ -26,6 +44,7 @@
 
     void src_hops_destroy(src_hops_obj * obj) {
 
+        free((void *) obj->buffer);
         format_destroy(obj->format);
         interface_destroy(obj->interface);
 
 
     void src_hops_open(src_hops_obj * obj) {
 
+
         switch(obj->interface->type) {
 
             case interface_file:
 
-                obj->fp = fopen(obj->interface->fileName, "rb");
+                src_hops_open_file(obj);
 
             break;
 
             case interface_socket:
 
-                printf("Not implemented yet.\n");
-                exit(EXIT_FAILURE);
+                src_hops_open_socket(obj);
 
             break;
 
             case interface_soundcard:
 
-                printf("Not implemented yet.\n");
-                exit(EXIT_FAILURE);
+                src_hops_open_soundcard(obj);
 
             break;
 
 
     }
 
+    void src_hops_open_file(src_hops_obj * obj) {
+
+        obj->fp = fopen(obj->interface->fileName, "rb");
+
+    }
+
+    void src_hops_open_socket(src_hops_obj * obj) {
+
+        printf("Not implemented yet.\n");
+        exit(EXIT_FAILURE);
+
+    }
+
+    void src_hops_open_soundcard(src_hops_obj * obj) {
+
+        snd_pcm_hw_params_t * hw_params;
+        snd_pcm_format_t format;
+        int err;
+        char * tmpStr;
+
+        switch (obj->format->type) {
+            
+            case format_bin08:
+
+                format = SND_PCM_FORMAT_S8;
+
+            break;
+
+            case format_bin16:
+
+                format = SND_PCM_FORMAT_S16_LE;
+
+            break;
+
+            case format_bin24:
+
+                format = SND_PCM_FORMAT_S24_LE;
+
+            break;
+            
+            case format_bin32:
+            
+                format = SND_PCM_FORMAT_S32_LE;
+            
+            break;
+
+            default:
+
+                printf("Invalid format.\n");
+                exit(EXIT_FAILURE);
+
+            break;
+
+        }       
+
+        tmpStr = (char *) malloc(sizeof(char) * 1024);
+        sprintf(tmpStr, "hw:%u,%u", obj->interface->card, obj->interface->device);
+
+        if ((err = snd_pcm_open(&(obj->ch), tmpStr, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
+            printf("Cannot open audio device %s: %s\n",tmpStr, snd_strerror(err));
+            exit(EXIT_FAILURE);
+        }
+
+        free((void *) tmpStr);
+
+        if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
+            printf("Cannot allocate hardware parameter structure: %s\n", snd_strerror(err));
+            exit(EXIT_FAILURE);
+        }
+
+        if ((err = snd_pcm_hw_params_any(obj->ch, hw_params)) < 0) {
+            printf("Cannot initialize hardware parameter structure: %s\n", snd_strerror(err));
+            exit(EXIT_FAILURE);
+        }
+
+        if ((err = snd_pcm_hw_params_set_access(obj->ch, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
+            printf("Cannot set access type: %s\n", snd_strerror(err));
+            exit(EXIT_FAILURE);
+        }
+
+        if ((err = snd_pcm_hw_params_set_format(obj->ch, hw_params, format)) < 0) {
+            printf("Cannot set sample format: %s\n", snd_strerror(err));
+            exit(EXIT_FAILURE);
+        }
+
+        if ((err = snd_pcm_hw_params_set_rate(obj->ch, hw_params, obj->fS, 0)) < 0) {
+            printf("Cannot set sample rate: %s\n", snd_strerror(err));
+            exit(EXIT_FAILURE);
+        }
+
+        if ((err = snd_pcm_hw_params_set_channels(obj->ch, hw_params, obj->nChannels)) < 0) {
+            printf("Cannot set channel count: %s\n", snd_strerror(err));
+            exit(EXIT_FAILURE);
+        }
+
+        if ((err = snd_pcm_hw_params(obj->ch, hw_params)) < 0) {
+            printf("Cannot set parameters: %s\n", snd_strerror(err));
+            exit(EXIT_FAILURE);
+        }
+
+        snd_pcm_hw_params_free(hw_params);
+
+        if ((err = snd_pcm_prepare(obj->ch)) < 0) {
+            printf("Cannot prepare audio interface for use: %s\n", snd_strerror(err));
+            exit(EXIT_FAILURE);
+        }
+
+    }
+
     void src_hops_close(src_hops_obj * obj) {
 
         switch(obj->interface->type) {
 
             case interface_file:
 
-                fclose(obj->fp);
+                src_hops_close_file(obj);
 
             break;
 
             case interface_socket:
 
-                printf("Not implemented yet.\n");
-                exit(EXIT_FAILURE);
+                src_hops_close_socket(obj);
 
             break;
 
             case interface_soundcard:
 
-                printf("Not implemented yet.\n");
-                exit(EXIT_FAILURE);
+                src_hops_close_soundcard(obj);
 
             break;
 
 
     }
 
+    void src_hops_close_file(src_hops_obj * obj) {
+
+        fclose(obj->fp);
+
+    }
+
+    void src_hops_close_socket(src_hops_obj * obj) {
+
+        printf("Not implemented yet.\n");
+        exit(EXIT_FAILURE);
+
+    }
+
+    void src_hops_close_soundcard(src_hops_obj * obj) {
+
+        snd_pcm_close(obj->ch);
+
+    }
+
     int src_hops_process(src_hops_obj * obj) {
 
         int rtnValue;
         signed int sampleInt;
         float sample;
         int rtnValue;
-        int tmp;
-
-        rtnValue = 0;
+        int nBytes;
+        int nSamples;
 
-        for (iSample = 0; iSample < obj->hopSize; iSample++) {
-            
-            for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
-            
-                if (!feof(obj->fp)) {
+        switch (obj->format->type) {
 
-                    switch (obj->format->type) {
-                        
-                        case format_bin08:
+            case format_bin08: nBytes = 1; break;
+            case format_bin16: nBytes = 2; break;
+            case format_bin24: nBytes = 3; break;
+            case format_bin32: nBytes = 4; break;
 
-                            tmp = fread(&(obj->bytes[3]), sizeof(char), 1, obj->fp);
-                            sample = pcm_signed08bits2normalized(obj->bytes);
+        }
 
-                        break;
+        memset(obj->buffer, 0x00, sizeof(char) * nBytes * obj->hopSize * obj->nChannels);
+        nSamples = fread(obj->buffer, sizeof(char) * nBytes, obj->hopSize * obj->nChannels, obj->fp);
 
-                        case format_bin16:
-    
-                            tmp = fread(&(obj->bytes[2]), sizeof(char), 2, obj->fp);
-                            sample = pcm_signed16bits2normalized(obj->bytes);
+        for (iSample = 0; iSample < obj->hopSize; iSample++) {
 
-                        break;
+            for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
 
-                        case format_bin24:
+                memcpy(&(obj->bytes[4-nBytes]),
+                       &(obj->buffer[(iSample * obj->nChannels + iChannel) * nBytes]),
+                       sizeof(char) * nBytes);
 
-                            tmp = fread(&(obj->bytes[1]), sizeof(char), 3, obj->fp);
-                            sample = pcm_signed24bits2normalized(obj->bytes);
+                sample = pcm_signedXXbits2normalized(obj->bytes, nBytes);
 
-                        break;
-                        
-                        case format_bin32:
-                        
-                            tmp = fread(&(obj->bytes[0]), sizeof(char), 4, obj->fp);
-                            sample = pcm_signed32bits2normalized(obj->bytes);
-                        
-                        break;
-                    }
+                obj->out->hops->array[iChannel][iSample] = sample;
 
-                }
-                else {
+            }
 
-                    sample = 0.0f;
-                    rtnValue = -1;
+        }
 
-                }
+        if (nSamples != (obj->nChannels * obj->hopSize)) {
+            
+            rtnValue = -1;    
 
-                obj->out->hops->array[iChannel][iSample] = sample;
+        }
+        else {
 
-            }
+            rtnValue = 0;
 
         }
 
 
     int src_hops_process_soundcard(src_hops_obj * obj) {
 
-        printf("Not implemented\n");
-        exit(EXIT_FAILURE);
+        unsigned int iSample;
+        unsigned int iChannel;
+
+        int err;
+        unsigned int nBytes;
+        float sample;
+
+        int rtnValue;
+
+        switch (obj->format->type) {
+
+            case format_bin08: nBytes = 1; break;
+            case format_bin16: nBytes = 2; break;
+            case format_bin24: nBytes = 3; break;
+            case format_bin32: nBytes = 4; break;
+
+        }
+
+        if (err = snd_pcm_readi(obj->ch, obj->buffer, obj->hopSize) > 0) {
+
+            for (iSample = 0; iSample < obj->hopSize; iSample++) {
+
+                for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
+
+                    memcpy(&(obj->bytes[4-nBytes]),
+                           &(obj->buffer[(iSample * obj->nChannels + iChannel) * nBytes]),
+                           sizeof(char) * nBytes);
+
+                    sample = pcm_signedXXbits2normalized(obj->bytes, nBytes);
+
+                    obj->out->hops->array[iChannel][iSample] = sample;
+
+                }
+
+            }
+
+            rtnValue = 0;
+
+        }
+        else {
+
+            rtnValue = -1;
+
+        }
+
+        return rtnValue;
 
     }
 
index 4c9d534b605658524905bce2b0cbaffa32022aac..28bcc06ae61e9ae335aaf3f2a153dadcaeaadb32 100644 (file)
@@ -4,6 +4,7 @@
     #include <stdlib.h>
     #include <stdio.h>
     #include <string.h>
+    #include <alsa/asoundlib.h>
 
     #include "../general/format.h"
     #include "../general/interface.h"
@@ -24,7 +25,9 @@
         interface_obj * interface;
 
         FILE * fp;
+        snd_pcm_t * ch;
 
+        char * buffer;
         char bytes[4];
 
         msg_hops_obj * out;
 
     void src_hops_open(src_hops_obj * obj);
 
+    void src_hops_open_file(src_hops_obj * obj);
+
+    void src_hops_open_socket(src_hops_obj * obj);
+
+    void src_hops_open_soundcard(src_hops_obj * obj);
+
     void src_hops_close(src_hops_obj * obj);
 
+    void src_hops_close_file(src_hops_obj * obj);
+
+    void src_hops_close_socket(src_hops_obj * obj);
+
+    void src_hops_close_soundcard(src_hops_obj * obj);
+
     int src_hops_process(src_hops_obj * obj);
 
     int src_hops_process_file(src_hops_obj * obj);
index 9c0c6c7cb1a91dd96ff65e56e59386db71e06d1d..89afe1eef237491a5e64c25c2c8924f1fa69d823 100644 (file)
     #include "pcm.h"
 
-    float pcm_signed08bits2normalized(const char * bytes) {
+    float pcm_signedXXbits2normalized(const char * bytes, const unsigned int nBytes) {
 
         int sample32;
         float sampleFloat;
 
-        sample32 = 0;
-        sample32 += ((((signed int) bytes[3]) << 0) & 0xFFFFFFFF);
+        switch(nBytes) {
 
-        sampleFloat = (float) sample32;
+            case 1:
 
-        sampleFloat /= 128.0f;
+                sample32 = 0;
+                sample32 += ((((signed int) bytes[3]) << 0) & 0xFFFFFFFF);
 
-        return sampleFloat;
+                sampleFloat = (float) sample32;
 
-    }
+                sampleFloat /= 128.0f;                
 
-    float pcm_signed16bits2normalized(const char * bytes) {
+            break;
 
-        int sample32;
-        float sampleFloat;
+            case 2:
 
-        sample32 = 0;
-        sample32 += ((((signed int) bytes[2]) << 0) & 0x000000FF);
-        sample32 += ((((signed int) bytes[3]) << 8) & 0xFFFFFF00);
+                sample32 = 0;
+                sample32 += ((((signed int) bytes[2]) << 0) & 0x000000FF);
+                sample32 += ((((signed int) bytes[3]) << 8) & 0xFFFFFF00);
 
-        sampleFloat = (float) sample32;
+                sampleFloat = (float) sample32;
 
-        sampleFloat /= 32768.0f;
+                sampleFloat /= 32768.0f;
 
-        return sampleFloat;
+            break;
 
-    }
+            case 3:
 
-    float pcm_signed24bits2normalized(const char * bytes) {
+                sample32 = 0;
+                sample32 += ((((signed int) bytes[1]) << 0) & 0x000000FF);
+                sample32 += ((((signed int) bytes[2]) << 8) & 0x0000FF00);
+                sample32 += ((((signed int) bytes[3]) << 16) & 0xFFFF0000);
 
-        int sample32;
-        float sampleFloat;
+                sampleFloat = (float) sample32;
 
-        sample32 = 0;
-        sample32 += ((((signed int) bytes[1]) << 0) & 0x000000FF);
-        sample32 += ((((signed int) bytes[2]) << 8) & 0x0000FF00);
-        sample32 += ((((signed int) bytes[3]) << 16) & 0xFFFF0000);
+                sampleFloat /= 8388608.0f;
 
-        sampleFloat = (float) sample32;
+            break;
 
-        sampleFloat /= 8388608.0f;
+            case 4:
 
-        return sampleFloat;
+                sample32 = 0;
+                sample32 += ((((signed int) bytes[0]) << 0) & 0x000000FF);
+                sample32 += ((((signed int) bytes[1]) << 8) & 0x0000FF00);
+                sample32 += ((((signed int) bytes[2]) << 16) & 0x00FF0000);
+                sample32 += ((((signed int) bytes[3]) << 24) & 0xFF000000);
 
-    }
+                sampleFloat = (float) sample32;
 
-    float pcm_signed32bits2normalized(const char * bytes) {
+                sampleFloat /= 2147483648.0f;
 
-        int sample32;
-        float sampleFloat;
-
-        sample32 = 0;
-        sample32 += ((((signed int) bytes[0]) << 0) & 0x000000FF);
-        sample32 += ((((signed int) bytes[1]) << 8) & 0x0000FF00);
-        sample32 += ((((signed int) bytes[2]) << 16) & 0x00FF0000);
-        sample32 += ((((signed int) bytes[3]) << 24) & 0xFF000000);
-
-        sampleFloat = (float) sample32;
+            break;
 
-        sampleFloat /= 2147483648.0f;
+        }
 
         return sampleFloat;
 
     }
 
-    void pcm_normalized2signed08bits(const float sample, char * bytes) {
+    void pcm_normalized2signedXXbits(const float sample, const unsigned int nBytes, char * bytes) {
 
         int sample32;
 
-        sample32 = (signed int) (roundf(sample * 128.0f));
+        switch(nBytes) {
 
-        bytes[0] = 0x00;
-        bytes[1] = 0x00;
-        bytes[2] = 0x00;
-        bytes[3] = (signed char) ((sample32 & 0x000000FF) >> 0);       
+            case 1:
 
-    }
+                sample32 = (signed int) (roundf(sample * 128.0f));
 
-    void pcm_normalized2signed16bits(const float sample, char * bytes) {
+                bytes[0] = 0x00;
+                bytes[1] = 0x00;
+                bytes[2] = 0x00;
+                bytes[3] = (signed char) ((sample32 & 0x000000FF) >> 0);    
 
-        int sample32;
+            break;
 
-        sample32 = (signed int) (roundf(sample * 32768.0f));
+            case 2:
 
-        bytes[0] = 0x00;
-        bytes[1] = 0x00;
-        bytes[2] = (signed char) ((sample32 & 0x000000FF) >> 0);  
-        bytes[3] = (signed char) ((sample32 & 0x0000FF00) >> 8);  
+                sample32 = (signed int) (roundf(sample * 32768.0f));
 
-    }
+                bytes[0] = 0x00;
+                bytes[1] = 0x00;
+                bytes[2] = (signed char) ((sample32 & 0x000000FF) >> 0);  
+                bytes[3] = (signed char) ((sample32 & 0x0000FF00) >> 8);  
 
-    void pcm_normalized2signed24bits(const float sample, char * bytes) {
+            break;
 
-        int sample32;
+            case 3:
 
-        sample32 = (signed int) (roundf(sample * 8388608.0f));
+                sample32 = (signed int) (roundf(sample * 8388608.0f));
 
-        bytes[0] = 0x00;
-        bytes[1] = (signed char) ((sample32 & 0x000000FF) >> 0);  
-        bytes[2] = (signed char) ((sample32 & 0x0000FF00) >> 8);  
-        bytes[3] = (signed char) ((sample32 & 0x00FF0000) >> 16); 
+                bytes[0] = 0x00;
+                bytes[1] = (signed char) ((sample32 & 0x000000FF) >> 0);  
+                bytes[2] = (signed char) ((sample32 & 0x0000FF00) >> 8);  
+                bytes[3] = (signed char) ((sample32 & 0x00FF0000) >> 16); 
 
-    }
+            break;
 
-    void pcm_normalized2signed32bits(const float sample, char * bytes) {
+            case 4:
 
-        int sample32;
+                sample32 = (signed int) (roundf(sample * 2147483648.0f));
+
+                bytes[0] = (signed char) ((sample32 & 0x000000FF) >> 0);
+                bytes[1] = (signed char) ((sample32 & 0x0000FF00) >> 8);  
+                bytes[2] = (signed char) ((sample32 & 0x00FF0000) >> 16);  
+                bytes[3] = (signed char) ((sample32 & 0xFF000000) >> 24); 
 
-        sample32 = (signed int) (roundf(sample * 2147483648.0f));
+            break;
 
-        bytes[0] = (signed char) ((sample32 & 0x000000FF) >> 0);
-        bytes[1] = (signed char) ((sample32 & 0x0000FF00) >> 8);  
-        bytes[2] = (signed char) ((sample32 & 0x00FF0000) >> 16);  
-        bytes[3] = (signed char) ((sample32 & 0xFF000000) >> 24); 
+        }
 
     }
index ab0ac94604a44defad4ca20b2834c12bdcdae69d..df3b23ad779de378f08d3114552c543f10d81623 100644 (file)
@@ -4,20 +4,8 @@
     #include <math.h>
     #include <limits.h>
 
-    float pcm_signed08bits2normalized(const char * bytes);
+    float pcm_signedXXbits2normalized(const char * bytes, const unsigned int nBytes);
 
-    float pcm_signed16bits2normalized(const char * bytes);
-
-    float pcm_signed24bits2normalized(const char * bytes);
-
-    float pcm_signed32bits2normalized(const char * bytes);
-
-    void pcm_normalized2signed08bits(const float sample, char * bytes);
-
-    void pcm_normalized2signed16bits(const float sample, char * bytes);
-
-    void pcm_normalized2signed24bits(const float sample, char * bytes);
-
-    void pcm_normalized2signed32bits(const float sample, char * bytes);
+    void pcm_normalized2signedXXbits(const float sample, const unsigned int nBytes, char * bytes);
 
 #endif
\ No newline at end of file