From c1e5c8c1ef41ddb47bbe4c8545e29b0681df4247 Mon Sep 17 00:00:00 2001 From: FrancoisGrondin Date: Mon, 10 Jul 2017 09:24:00 -0400 Subject: [PATCH] Soundcard support added --- src/general/interface.c | 14 +- src/general/interface.h | 4 +- src/sink/snk_hops.c | 43 ++----- src/sink/snk_pots.c | 6 - src/sink/snk_tracks.c | 6 - src/source/src_hops.c | 275 ++++++++++++++++++++++++++++++++-------- src/source/src_hops.h | 15 +++ src/utils/pcm.c | 135 ++++++++++---------- src/utils/pcm.h | 16 +-- 9 files changed, 328 insertions(+), 186 deletions(-) diff --git a/src/general/interface.c b/src/general/interface.c index 57fa03a..697f52d 100644 --- a/src/general/interface.c +++ b/src/general/interface.c @@ -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; @@ -104,14 +104,14 @@ // | 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; @@ -140,8 +140,8 @@ // | Soundcard | // +----------------------------------------------------------+ + obj->card = card; obj->device = device; - obj->subdevice = subdevice; return obj; @@ -188,8 +188,8 @@ if (obj->type == interface_soundcard) { + clone->card = obj->card; clone->device = obj->device; - clone->subdevice = obj->subdevice; } @@ -235,7 +235,7 @@ 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; diff --git a/src/general/interface.h b/src/general/interface.h index 8da8b02..c635401 100644 --- a/src/general/interface.h +++ b/src/general/interface.h @@ -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); diff --git a/src/sink/snk_hops.c b/src/sink/snk_hops.c index 6b05f74..00c9cf4 100644 --- a/src/sink/snk_hops.c +++ b/src/sink/snk_hops.c @@ -160,46 +160,27 @@ 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); } diff --git a/src/sink/snk_pots.c b/src/sink/snk_pots.c index caa1a98..92a5600 100644 --- a/src/sink/snk_pots.c +++ b/src/sink/snk_pots.c @@ -132,7 +132,6 @@ int snk_pots_process_file(snk_pots_obj * obj) { int rtnValue; - //unsigned int iPot; if (obj->in->timeStamp != 0) { @@ -142,11 +141,6 @@ 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; } diff --git a/src/sink/snk_tracks.c b/src/sink/snk_tracks.c index 18c116c..cb1cea0 100644 --- a/src/sink/snk_tracks.c +++ b/src/sink/snk_tracks.c @@ -132,7 +132,6 @@ int snk_tracks_process_file(snk_tracks_obj * obj) { int rtnValue; - //unsigned int iTrack; if (obj->in->timeStamp != 0) { @@ -143,11 +142,6 @@ 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; } diff --git a/src/source/src_hops.c b/src/source/src_hops.c index 0b75de2..2f8ded5 100644 --- a/src/source/src_hops.c +++ b/src/source/src_hops.c @@ -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)); @@ -18,6 +19,23 @@ 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); @@ -47,25 +66,24 @@ 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; @@ -80,27 +98,134 @@ } + 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; @@ -115,6 +240,25 @@ } + 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; @@ -164,58 +308,45 @@ 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; } @@ -232,8 +363,52 @@ 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; } diff --git a/src/source/src_hops.h b/src/source/src_hops.h index 4c9d534..28bcc06 100644 --- a/src/source/src_hops.h +++ b/src/source/src_hops.h @@ -4,6 +4,7 @@ #include #include #include + #include #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; @@ -49,8 +52,20 @@ 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); diff --git a/src/utils/pcm.c b/src/utils/pcm.c index 9c0c6c7..89afe1e 100644 --- a/src/utils/pcm.c +++ b/src/utils/pcm.c @@ -1,123 +1,118 @@ #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); + } } diff --git a/src/utils/pcm.h b/src/utils/pcm.h index ab0ac94..df3b23a 100644 --- a/src/utils/pcm.h +++ b/src/utils/pcm.h @@ -4,20 +4,8 @@ #include #include - 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 -- 2.34.1