// | Soundcard |
// +----------------------------------------------------------+
+ obj->card = 0;
obj->device = 0;
- obj->subdevice = 0;
return obj;
// | 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;
char * ip;
unsigned int port;
+ unsigned int card;
unsigned int device;
- unsigned int subdevice;
} interface_obj;
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);
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);
}
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;
}
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;
}
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;
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;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+ #include <alsa/asoundlib.h>
#include "../general/format.h"
#include "../general/interface.h"
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);
#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);
+ }
}
#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