#ifndef __ODAS_SOURCE_HOPS
#define __ODAS_SOURCE_HOPS
- /**
- * \file src_hops.h
- * \author François Grondin <francois.grondin2@usherbrooke.ca>
- * \version 2.0
- * \date 2018-03-18
- * \copyright
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
+/**
+ * \file src_hops.h
+ * \author François Grondin <francois.grondin2@usherbrooke.ca>
+ * \version 2.0
+ * \date 2018-03-18
+ * \copyright
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <alsa/asoundlib.h>
- #include <pulse/simple.h>
- #include <pulse/error.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <alsa/asoundlib.h>
++#include <pulse/simple.h>
++#include <pulse/error.h>
- #include "../general/format.h"
- #include "../general/interface.h"
- #include "../message/msg_hops.h"
- #include "../signal/hop.h"
- #include "../utils/pcm.h"
+#include "../general/format.h"
+#include "../general/interface.h"
+#include "../message/msg_hops.h"
+#include "../signal/hop.h"
+#include "../utils/pcm.h"
- typedef struct src_hops_obj
- {
- typedef struct src_hops_obj {
++typedef struct src_hops_obj {
- unsigned long long timeStamp;
+ unsigned long long timeStamp;
- unsigned int hopSize;
- unsigned int nChannels;
- unsigned int fS;
+ unsigned int hopSize;
+ unsigned int nChannels;
+ unsigned int fS;
- format_obj * format;
- interface_obj * interface;
+ format_obj *format;
+ interface_obj *interface;
- FILE *fp;
- snd_pcm_t *ch;
- FILE * fp;
- snd_pcm_t * ch;
- pa_simple * pa;
- pa_sample_spec ss;
- pa_channel_map cm;
++ FILE * fp;
++ snd_pcm_t * ch;
++ pa_simple * pa;
++ pa_sample_spec ss;
++ pa_channel_map cm;
+ struct sockaddr_in sserver;
+ int sid;
- char * buffer;
- unsigned int bufferSize;
+ char *buffer;
+ unsigned int bufferSize;
- char bytes[4];
+ char bytes[4];
- msg_hops_obj *out;
- msg_hops_obj * out;
++ msg_hops_obj * out;
- } src_hops_obj;
+} src_hops_obj;
- typedef struct src_hops_cfg
- {
- typedef struct src_hops_cfg {
++typedef struct src_hops_cfg {
- format_obj *format;
- interface_obj *interface;
- format_obj * format;
- interface_obj * interface;
- pa_channel_map * channel_map;
++ format_obj * format;
++ interface_obj * interface;
++ pa_channel_map * channel_map;
- } src_hops_cfg;
+} src_hops_cfg;
- src_hops_obj *src_hops_construct(const src_hops_cfg *src_hops_config, const msg_hops_cfg *msg_hops_config);
- src_hops_obj * src_hops_construct(const src_hops_cfg * src_hops_config, const msg_hops_cfg * msg_hops_config);
++src_hops_obj * src_hops_construct(const src_hops_cfg * src_hops_config, const msg_hops_cfg * msg_hops_config);
+
- void src_hops_destroy(src_hops_obj * obj);
++void src_hops_destroy(src_hops_obj * obj);
+
- void src_hops_connect(src_hops_obj * obj, msg_hops_obj * out);
++void src_hops_connect(src_hops_obj * obj, msg_hops_obj * out);
+
- void src_hops_disconnect(src_hops_obj * obj);
++void src_hops_disconnect(src_hops_obj * obj);
- void src_hops_destroy(src_hops_obj *obj);
- void src_hops_open(src_hops_obj * obj);
++void src_hops_open(src_hops_obj * obj);
- void src_hops_connect(src_hops_obj *obj, msg_hops_obj *out);
- void src_hops_open_interface_file(src_hops_obj * obj);
++void src_hops_open_interface_file(src_hops_obj * obj);
- void src_hops_disconnect(src_hops_obj *obj);
- void src_hops_open_interface_soundcard(src_hops_obj * obj);
++void src_hops_open_interface_soundcard(src_hops_obj * obj);
- void src_hops_open(src_hops_obj *obj);
- void src_hops_open_interface_pulseaudio(src_hops_obj * obj);
++void src_hops_open_interface_pulseaudio(src_hops_obj * obj);
- void src_hops_open_interface_file(src_hops_obj *obj);
- void src_hops_close(src_hops_obj * obj);
++void src_hops_open_interface_socket(src_hops_obj * obj);
- void src_hops_open_interface_soundcard(src_hops_obj *obj);
- void src_hops_close_interface_file(src_hops_obj * obj);
++void src_hops_close(src_hops_obj * obj);
- void src_hops_open_interface_socket(src_hops_obj *obj);
- void src_hops_close_interface_soundcard(src_hops_obj * obj);
++void src_hops_close_interface_file(src_hops_obj * obj);
- void src_hops_close(src_hops_obj *obj);
- void src_hops_close_interface_pulseaudio(src_hops_obj * obj);
++void src_hops_close_interface_soundcard(src_hops_obj * obj);
- void src_hops_close_interface_file(src_hops_obj *obj);
- int src_hops_process(src_hops_obj * obj);
++void src_hops_close_interface_pulseaudio(src_hops_obj * obj);
- void src_hops_close_interface_soundcard(src_hops_obj *obj);
- int src_hops_process_interface_file(src_hops_obj * obj);
++void src_hops_close_interface_socket(src_hops_obj * obj);
- void src_hops_close_interface_socket(src_hops_obj *obj);
- int src_hops_process_interface_soundcard(src_hops_obj * obj);
++int src_hops_process(src_hops_obj * obj);
- int src_hops_process(src_hops_obj *obj);
- int src_hops_process_interface_pulseaudio(src_hops_obj * obj);
++int src_hops_process_interface_file(src_hops_obj * obj);
- int src_hops_process_interface_file(src_hops_obj *obj);
- void src_hops_process_format_binary_int08(src_hops_obj * obj);
++int src_hops_process_interface_soundcard(src_hops_obj * obj);
- int src_hops_process_interface_soundcard(src_hops_obj *obj);
- void src_hops_process_format_binary_int16(src_hops_obj * obj);
++int src_hops_process_interface_socket(src_hops_obj * obj);
- int src_hops_process_interface_socket(src_hops_obj *obj);
- void src_hops_process_format_binary_int24(src_hops_obj * obj);
++int src_hops_process_interface_pulseaudio(src_hops_obj * obj);
- void src_hops_process_format_binary_int08(src_hops_obj *obj);
- void src_hops_process_format_binary_int32(src_hops_obj * obj);
++void src_hops_process_format_binary_int08(src_hops_obj * obj);
- void src_hops_process_format_binary_int16(src_hops_obj *obj);
- src_hops_cfg * src_hops_cfg_construct(void);
++void src_hops_process_format_binary_int16(src_hops_obj * obj);
- void src_hops_process_format_binary_int24(src_hops_obj *obj);
- void src_hops_cfg_destroy(src_hops_cfg * src_hops_config);
++void src_hops_process_format_binary_int24(src_hops_obj * obj);
- void src_hops_process_format_binary_int32(src_hops_obj *obj);
-#endif
++void src_hops_process_format_binary_int32(src_hops_obj * obj);
+
- src_hops_cfg *src_hops_cfg_construct(void);
++src_hops_cfg * src_hops_cfg_construct(void);
+
- void src_hops_cfg_destroy(src_hops_cfg *src_hops_config);
++void src_hops_cfg_destroy(src_hops_cfg * src_hops_config);
+
- #endif
++#endif
-
-
- /**
- * \file src_hops.c
- * \author François Grondin <francois.grondin2@usherbrooke.ca>
- * \version 2.0
- * \date 2018-03-18
- * \copyright
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
- #include <source/src_hops.h>
+/**
- * \file src_hops.c
- * \author François Grondin <francois.grondin2@usherbrooke.ca>
- * \version 2.0
- * \date 2018-03-18
- * \copyright
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
++* \file src_hops.c
++* \author François Grondin <francois.grondin2@usherbrooke.ca>
++* \version 2.0
++* \date 2018-03-18
++* \copyright
++*
++* This program is free software: you can redistribute it and/or modify
++* it under the terms of the GNU General Public License as published by
++* the Free Software Foundation, either version 3 of the License, or
++* (at your option) any later version.
++*
++* This program is distributed in the hope that it will be useful,
++* but WITHOUT ANY WARRANTY; without even the implied warranty of
++* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
++* GNU General Public License for more details.
++*
++* You should have received a copy of the GNU General Public License
++* along with this program. If not, see <http://www.gnu.org/licenses/>.
++*
++*/
+
+#include <source/src_hops.h>
+
- src_hops_obj *src_hops_construct(const src_hops_cfg *src_hops_config, const msg_hops_cfg *msg_hops_config)
- {
++src_hops_obj * src_hops_construct(const src_hops_cfg * src_hops_config, const msg_hops_cfg * msg_hops_config) {
+
- src_hops_obj *obj;
++ src_hops_obj * obj;
+ unsigned int nBytes;
+
- obj = (src_hops_obj *)malloc(sizeof(src_hops_obj));
++ obj = (src_hops_obj *) malloc(sizeof(src_hops_obj));
+
+ obj->timeStamp = 0;
+
+ obj->hopSize = msg_hops_config->hopSize;
+ obj->nChannels = msg_hops_config->nChannels;
+ obj->fS = msg_hops_config->fS;
+
+ obj->format = format_clone(src_hops_config->format);
+ obj->interface = interface_clone(src_hops_config->interface);
++ if (src_hops_config->channel_map != NULL)
++ {
++ // Will not be null if in pulseaudio mode
++ memcpy(&obj->cm, src_hops_config->channel_map, sizeof(pa_channel_map));
++ }
++ else if (obj->interface->type == interface_pulseaudio)
++ {
++ // Can't be null if we are in pulseaudio mode
++ printf("Error: Pulseaudio interface requires channel map.\n");
++ exit(EXIT_FAILURE);
++ }
- src_hops_obj * src_hops_construct(const src_hops_cfg * src_hops_config, const msg_hops_cfg * msg_hops_config) {
+ memset(obj->bytes, 0x00, 4 * sizeof(char));
- src_hops_obj * obj;
- unsigned int nBytes;
+ if (!((obj->interface->type == interface_file ||
- obj->interface->type == interface_soundcard ||
- obj->interface->type == interface_socket) &&
++ obj->interface->type == interface_soundcard ||
++ obj->interface->type == interface_pulseaudio ||
++ obj->interface->type == interface_socket) &&
+ (obj->format->type == format_binary_int08 ||
+ obj->format->type == format_binary_int16 ||
+ obj->format->type == format_binary_int24 ||
- obj->format->type == format_binary_int32)))
- {
++ obj->format->type == format_binary_int32))) {
- obj = (src_hops_obj *) malloc(sizeof(src_hops_obj));
+ printf("Source hops: Invalid interface and/or format.\n");
+ exit(EXIT_FAILURE);
+ }
- obj->buffer = (char *)malloc(sizeof(char) * obj->hopSize * obj->nChannels * 4);
- obj->timeStamp = 0;
++ obj->buffer = (char *) malloc(sizeof(char) * obj->hopSize * obj->nChannels * 4);
+ memset(obj->buffer, 0x00, sizeof(char) * obj->hopSize * obj->nChannels * 4);
+
- switch (obj->format->type)
- {
++ switch (obj->format->type) {
+
+ case format_binary_int08:
+ obj->bufferSize = obj->hopSize * obj->nChannels * 1;
+ break;
+ case format_binary_int16:
+ obj->bufferSize = obj->hopSize * obj->nChannels * 2;
+ break;
+ case format_binary_int24:
+ obj->bufferSize = obj->hopSize * obj->nChannels * 3;
+ break;
+ case format_binary_int32:
+ obj->bufferSize = obj->hopSize * obj->nChannels * 4;
+ break;
+ }
- obj->out = (msg_hops_obj *)NULL;
- obj->hopSize = msg_hops_config->hopSize;
- obj->nChannels = msg_hops_config->nChannels;
- obj->fS = msg_hops_config->fS;
-
- obj->format = format_clone(src_hops_config->format);
- obj->interface = interface_clone(src_hops_config->interface);
- if (src_hops_config->channel_map != NULL)
- {
- // Will not be null if in pulseaudio mode
- memcpy(&obj->cm, src_hops_config->channel_map, sizeof(pa_channel_map));
- }
- else if (obj->interface->type == interface_pulseaudio)
- {
- // Can't be null if we are in pulseaudio mode
- printf("Error: Pulseaudio interface requires channel map.\n");
- exit(EXIT_FAILURE);
- }
++ obj->out = (msg_hops_obj *) NULL;
- memset(obj->bytes, 0x00, 4 * sizeof(char));
-
- if (!(((obj->interface->type == interface_file) && (obj->format->type == format_binary_int08)) ||
- ((obj->interface->type == interface_file) && (obj->format->type == format_binary_int16)) ||
- ((obj->interface->type == interface_file) && (obj->format->type == format_binary_int24)) ||
- ((obj->interface->type == interface_file) && (obj->format->type == format_binary_int32)) ||
- ((obj->interface->type == interface_soundcard) && (obj->format->type == format_binary_int08)) ||
- ((obj->interface->type == interface_soundcard) && (obj->format->type == format_binary_int16)) ||
- ((obj->interface->type == interface_soundcard) && (obj->format->type == format_binary_int24)) ||
- ((obj->interface->type == interface_soundcard) && (obj->format->type == format_binary_int32)) ||
- ((obj->interface->type == interface_pulseaudio) && (obj->format->type == format_binary_int08)) ||
- ((obj->interface->type == interface_pulseaudio) && (obj->format->type == format_binary_int16)) ||
- ((obj->interface->type == interface_pulseaudio) && (obj->format->type == format_binary_int24)) ||
- ((obj->interface->type == interface_pulseaudio) && (obj->format->type == format_binary_int32)))) {
-
- printf("Source hops: Invalid interface and/or format.\n");
- exit(EXIT_FAILURE);
+ return obj;
+}
- void src_hops_destroy(src_hops_obj *obj)
- {
- }
++void src_hops_destroy(src_hops_obj * obj) {
- free((void *)obj->buffer);
- obj->buffer = (char *) malloc(sizeof(char) * obj->hopSize * obj->nChannels * 4);
- memset(obj->buffer, 0x00, sizeof(char) * obj->hopSize * obj->nChannels * 4);
++ free((void *) obj->buffer);
+ format_destroy(obj->format);
+ interface_destroy(obj->interface);
- free((void *)obj);
- switch (obj->format->type) {
++ free((void *) obj);
+}
- void src_hops_connect(src_hops_obj *obj, msg_hops_obj *out)
- {
- case format_binary_int08: obj->bufferSize = obj->hopSize * obj->nChannels * 1; break;
- case format_binary_int16: obj->bufferSize = obj->hopSize * obj->nChannels * 2; break;
- case format_binary_int24: obj->bufferSize = obj->hopSize * obj->nChannels * 3; break;
- case format_binary_int32: obj->bufferSize = obj->hopSize * obj->nChannels * 4; break;
++void src_hops_connect(src_hops_obj * obj, msg_hops_obj * out) {
- }
+ obj->out = out;
+}
- void src_hops_disconnect(src_hops_obj *obj)
- {
- obj->out = (msg_hops_obj *) NULL;
-
- return obj;
++void src_hops_disconnect(src_hops_obj * obj) {
- obj->out = (msg_hops_obj *)NULL;
- }
++ obj->out = (msg_hops_obj *) NULL;
+}
- void src_hops_open(src_hops_obj *obj)
- {
- void src_hops_destroy(src_hops_obj * obj) {
++void src_hops_open(src_hops_obj * obj) {
- switch (obj->interface->type)
- {
- free((void *) obj->buffer);
- format_destroy(obj->format);
- interface_destroy(obj->interface);
++ switch (obj->interface->type) {
- case interface_file:
- free((void *) obj);
++ case interface_file:
- src_hops_open_interface_file(obj);
- }
++ src_hops_open_interface_file(obj);
- void src_hops_connect(src_hops_obj * obj, msg_hops_obj * out) {
+ break;
- case interface_soundcard:
- obj->out = out;
++ case interface_soundcard:
- src_hops_open_interface_soundcard(obj);
- }
++ src_hops_open_interface_soundcard(obj);
- void src_hops_disconnect(src_hops_obj * obj) {
+ break;
- case interface_socket:
- obj->out = (msg_hops_obj *) NULL;
++ case interface_pulseaudio:
- src_hops_open_interface_socket(obj);
- }
++ src_hops_open_interface_pulseaudio(obj);
- void src_hops_open(src_hops_obj * obj) {
+ break;
- default:
- switch(obj->interface->type) {
++ case interface_socket:
- printf("Source hops: Invalid interface type.\n");
- exit(EXIT_FAILURE);
- case interface_file:
++ src_hops_open_interface_socket(obj);
+
- src_hops_open_interface_file(obj);
++ break;
+
- break;
++ default:
+
- case interface_soundcard:
++ printf("Source hops: Invalid interface type.\n");
++ exit(EXIT_FAILURE);
- src_hops_open_interface_soundcard(obj);
+ break;
+
- break;
+ }
+
- case interface_pulseaudio:
+}
- void src_hops_open_interface_file(src_hops_obj *obj)
- {
- src_hops_open_interface_pulseaudio(obj);
++void src_hops_open_interface_file(src_hops_obj * obj) {
- break;
+ obj->fp = fopen(obj->interface->fileName, "rb");
- if (obj->fp == NULL)
- {
- default:
++ if (obj->fp == NULL) {
+ printf("Cannot open file %s\n", obj->interface->fileName);
+ exit(EXIT_FAILURE);
+ }
+
- printf("Source hops: Invalid interface type.\n");
- exit(EXIT_FAILURE);
+}
- void src_hops_open_interface_soundcard(src_hops_obj *obj)
- {
- break;
++void src_hops_open_interface_soundcard(src_hops_obj * obj) {
- snd_pcm_hw_params_t *hw_params;
- }
++ snd_pcm_hw_params_t * hw_params;
+ snd_pcm_format_t format;
+ int err;
- switch (obj->format->type)
- {
- }
++ switch (obj->format->type) {
- case format_binary_int08:
- void src_hops_open_interface_file(src_hops_obj * obj) {
++ case format_binary_int08:
- format = SND_PCM_FORMAT_S8;
- obj->fp = fopen(obj->interface->fileName, "rb");
++ format = SND_PCM_FORMAT_S8;
- if (obj->fp == NULL) {
- printf("Cannot open file %s\n",obj->interface->fileName);
- exit(EXIT_FAILURE);
- }
+ break;
- case format_binary_int16:
- }
++ case format_binary_int16:
- format = SND_PCM_FORMAT_S16_LE;
- void src_hops_open_interface_pulseaudio(src_hops_obj * obj) {
++ format = SND_PCM_FORMAT_S16_LE;
- pa_sample_format_t format;
+ break;
- case format_binary_int24:
- switch (obj->format->type) {
-
- case format_binary_int08:
++ case format_binary_int24:
- format = SND_PCM_FORMAT_S24_LE;
- format = PA_SAMPLE_U8;
++ format = SND_PCM_FORMAT_S24_LE;
- break;
+ break;
- case format_binary_int32:
- case format_binary_int16:
++ case format_binary_int32:
- format = SND_PCM_FORMAT_S32_LE;
- format = PA_SAMPLE_S16LE;
++ format = SND_PCM_FORMAT_S32_LE;
- break;
+ break;
- default:
- case format_binary_int24:
++ default:
- printf("Source hops: Invalid format.\n");
- exit(EXIT_FAILURE);
- format = PA_SAMPLE_S24LE;
++ printf("Source hops: Invalid format.\n");
++ exit(EXIT_FAILURE);
- break;
-
- case format_binary_int32:
-
- format = PA_SAMPLE_S32LE;
-
- break;
+ break;
+
- default:
+ }
- if ((err = snd_pcm_open(&(obj->ch), obj->interface->deviceName, SND_PCM_STREAM_CAPTURE, 0)) < 0)
- {
- printf("Source hops: Invalid format.\n");
- exit(EXIT_FAILURE);
++ if ((err = snd_pcm_open(&(obj->ch), obj->interface->deviceName, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
+ printf("Source hops: Cannot open audio device %s: %s\n", obj->interface->deviceName, snd_strerror(err));
+ exit(EXIT_FAILURE);
+ }
- if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0)
- {
- break;
++ if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
+ printf("Source hops: 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)
- {
- }
++ if ((err = snd_pcm_hw_params_any(obj->ch, hw_params)) < 0) {
+ printf("Source hops: 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)
- {
- obj->ss.format = format;
- obj->ss.rate = obj->fS;
- obj->ss.channels = obj->nChannels;
++ if ((err = snd_pcm_hw_params_set_access(obj->ch, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
+ printf("Source hops: 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)
- {
- int err;
- if (!(obj->pa = pa_simple_new(NULL, "Odas", PA_STREAM_RECORD, obj->interface->deviceName, "record", &obj->ss, &obj->cm, NULL, &err))) {
- printf("Source hops: Cannot open pulseaudio device %s: %s\n", obj->interface->deviceName, pa_strerror(err));
- exit(EXIT_FAILURE);
- }
-
++ if ((err = snd_pcm_hw_params_set_format(obj->ch, hw_params, format)) < 0) {
+ printf("Source hops: 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)
- {
- void src_hops_open_interface_soundcard(src_hops_obj * obj) {
++ if ((err = snd_pcm_hw_params_set_rate(obj->ch, hw_params, obj->fS, 0)) < 0) {
+ printf("Source hops: 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)
- {
- snd_pcm_hw_params_t * hw_params;
- snd_pcm_format_t format;
- int err;
++ if ((err = snd_pcm_hw_params_set_channels(obj->ch, hw_params, obj->nChannels)) < 0) {
+ printf("Source hops: Cannot set channel count: %s\n", snd_strerror(err));
+ exit(EXIT_FAILURE);
+ }
- if ((err = snd_pcm_hw_params(obj->ch, hw_params)) < 0)
- {
- switch (obj->format->type) {
-
- case format_binary_int08:
++ if ((err = snd_pcm_hw_params(obj->ch, hw_params)) < 0) {
+ printf("Source hops: Cannot set parameters: %s\n", snd_strerror(err));
+ exit(EXIT_FAILURE);
+ }
- format = SND_PCM_FORMAT_S8;
+ snd_pcm_hw_params_free(hw_params);
- if ((err = snd_pcm_prepare(obj->ch)) < 0)
- {
- break;
++ if ((err = snd_pcm_prepare(obj->ch)) < 0) {
+ printf("Source hops: Cannot prepare audio interface for use: %s\n", snd_strerror(err));
+ exit(EXIT_FAILURE);
+ }
+
- case format_binary_int16:
++}
+
- format = SND_PCM_FORMAT_S16_LE;
++void src_hops_open_interface_pulseaudio(src_hops_obj * obj) {
+
- break;
++ pa_sample_format_t format;
+
- case format_binary_int24:
++ switch (obj->format->type) {
+
- format = SND_PCM_FORMAT_S24_LE;
++ case format_binary_int08:
+
- break;
-
- case format_binary_int32:
-
- format = SND_PCM_FORMAT_S32_LE;
-
- break;
++ format = PA_SAMPLE_U8;
+
- default:
++ break;
+
- printf("Source hops: Invalid format.\n");
- exit(EXIT_FAILURE);
++ case format_binary_int16:
+
- break;
++ format = PA_SAMPLE_S16LE;
+
- }
++ break;
+
++ case format_binary_int24:
+
- if ((err = snd_pcm_open(&(obj->ch), obj->interface->deviceName, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
- printf("Source hops: Cannot open audio device %s: %s\n",obj->interface->deviceName, snd_strerror(err));
- exit(EXIT_FAILURE);
- }
++ format = PA_SAMPLE_S24LE;
+
- if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
- printf("Source hops: Cannot allocate hardware parameter structure: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
++ break;
+
- if ((err = snd_pcm_hw_params_any(obj->ch, hw_params)) < 0) {
- printf("Source hops: Cannot initialize hardware parameter structure: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
++ case format_binary_int32:
+
- if ((err = snd_pcm_hw_params_set_access(obj->ch, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
- printf("Source hops: Cannot set access type: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
++ format = PA_SAMPLE_S32LE;
+
- if ((err = snd_pcm_hw_params_set_format(obj->ch, hw_params, format)) < 0) {
- printf("Source hops: Cannot set sample format: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
++ break;
+
- if ((err = snd_pcm_hw_params_set_rate(obj->ch, hw_params, obj->fS, 0)) < 0) {
- printf("Source hops: Cannot set sample rate: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
++ default:
+
- if ((err = snd_pcm_hw_params_set_channels(obj->ch, hw_params, obj->nChannels)) < 0) {
- printf("Source hops: Cannot set channel count: %s\n", snd_strerror(err));
++ printf("Source hops: Invalid format.\n");
+ exit(EXIT_FAILURE);
- }
+
- if ((err = snd_pcm_hw_params(obj->ch, hw_params)) < 0) {
- printf("Source hops: Cannot set parameters: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
++ break;
+
- snd_pcm_hw_params_free(hw_params);
++ }
+
- if ((err = snd_pcm_prepare(obj->ch)) < 0) {
- printf("Source hops: Cannot prepare audio interface for use: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
++ obj->ss.format = format;
++ obj->ss.rate = obj->fS;
++ obj->ss.channels = obj->nChannels;
+
++ int err;
++ if (!(obj->pa = pa_simple_new(NULL, "Odas", PA_STREAM_RECORD, obj->interface->deviceName, "record", &obj->ss, &obj->cm, NULL, &err))) {
++ printf("Source hops: Cannot open pulseaudio device %s: %s\n", obj->interface->deviceName, pa_strerror(err));
++ exit(EXIT_FAILURE);
+ }
+
- void src_hops_close(src_hops_obj * obj) {
+}
- void src_hops_open_interface_socket(src_hops_obj *obj)
- {
- switch(obj->interface->type) {
++void src_hops_open_interface_socket(src_hops_obj * obj) {
- case interface_file:
+ memset(&(obj->sserver), 0x00, sizeof(struct sockaddr_in));
- src_hops_close_interface_file(obj);
+ obj->sserver.sin_family = AF_INET;
+ obj->sserver.sin_addr.s_addr = inet_addr(obj->interface->ip);
+ obj->sserver.sin_port = htons(obj->interface->port);
+ obj->sid = socket(AF_INET, SOCK_STREAM, 0);
- if ((connect(obj->sid, (struct sockaddr *)&(obj->sserver), sizeof(obj->sserver))) < 0)
- {
-
- break;
++ if ((connect(obj->sid, (struct sockaddr *) &(obj->sserver), sizeof(obj->sserver))) < 0) {
+ printf("Source hops: Cannot connect to server\n");
+ exit(EXIT_FAILURE);
+ }
+}
- void src_hops_close(src_hops_obj *obj)
- {
- case interface_soundcard:
++void src_hops_close(src_hops_obj * obj) {
- switch (obj->interface->type)
- {
- src_hops_close_interface_soundcard(obj);
++ switch (obj->interface->type) {
- case interface_file:
- break;
++ case interface_file:
- src_hops_close_interface_file(obj);
- case interface_pulseaudio:
++ src_hops_close_interface_file(obj);
- src_hops_close_interface_pulseaudio(obj);
+ break;
- case interface_soundcard:
- break;
++ case interface_soundcard:
- src_hops_close_interface_soundcard(obj);
- default:
++ src_hops_close_interface_soundcard(obj);
- printf("Source hops: Invalid interface type.\n");
- exit(EXIT_FAILURE);
+ break;
- case interface_socket:
- break;
++ case interface_pulseaudio:
- src_hops_close_interface_socket(obj);
- }
++ src_hops_close_interface_pulseaudio(obj);
- }
+ break;
- default:
- void src_hops_close_interface_file(src_hops_obj * obj) {
++ case interface_socket:
- printf("Source hops: Invalid interface type.\n");
- exit(EXIT_FAILURE);
- fclose(obj->fp);
++ src_hops_close_interface_socket(obj);
+
- }
++ break;
+
- void src_hops_close_interface_soundcard(src_hops_obj * obj) {
++ default:
+
- snd_pcm_close(obj->ch);
++ printf("Source hops: Invalid interface type.\n");
++ exit(EXIT_FAILURE);
+ break;
}
+}
- void src_hops_close_interface_file(src_hops_obj *obj)
- {
- void src_hops_close_interface_pulseaudio(src_hops_obj * obj) {
++void src_hops_close_interface_file(src_hops_obj * obj) {
- if (obj->pa != NULL)
- pa_simple_free(obj->pa);
+ fclose(obj->fp);
+
- }
+}
- void src_hops_close_interface_soundcard(src_hops_obj *obj)
- {
- int src_hops_process(src_hops_obj * obj) {
++void src_hops_close_interface_soundcard(src_hops_obj * obj) {
- int rtnValue;
+ snd_pcm_close(obj->ch);
+
- switch(obj->format->type) {
++}
++
++void src_hops_close_interface_pulseaudio(src_hops_obj * obj) {
+
- case format_binary_int08:
++ if (obj->pa != NULL)
++ pa_simple_free(obj->pa);
+
- src_hops_process_format_binary_int08(obj);
+}
- void src_hops_close_interface_socket(src_hops_obj *obj)
- {
- break;
++void src_hops_close_interface_socket(src_hops_obj * obj) {
++
+ close(obj->sid);
+
- case format_binary_int16:
+}
- int src_hops_process(src_hops_obj *obj)
- {
- src_hops_process_format_binary_int16(obj);
++int src_hops_process(src_hops_obj * obj) {
- break;
+ int rtnValue;
- switch (obj->format->type)
- {
- case format_binary_int24:
++ switch (obj->format->type) {
- case format_binary_int08:
- src_hops_process_format_binary_int24(obj);
++ case format_binary_int08:
- src_hops_process_format_binary_int08(obj);
- break;
++ src_hops_process_format_binary_int08(obj);
- case format_binary_int32:
+ break;
- case format_binary_int16:
- src_hops_process_format_binary_int32(obj);
++ case format_binary_int16:
- src_hops_process_format_binary_int16(obj);
- break;
++ src_hops_process_format_binary_int16(obj);
- default:
+ break;
- case format_binary_int24:
- printf("Source hops: Invalid format type.\n");
- exit(EXIT_FAILURE);
++ case format_binary_int24:
- src_hops_process_format_binary_int24(obj);
- break;
++ src_hops_process_format_binary_int24(obj);
- }
+ break;
- case format_binary_int32:
- switch(obj->interface->type) {
++ case format_binary_int32:
- src_hops_process_format_binary_int32(obj);
- case interface_file:
++ src_hops_process_format_binary_int32(obj);
- rtnValue = src_hops_process_interface_file(obj);
+ break;
- default:
- break;
++ default:
- printf("Source hops: Invalid format type.\n");
- exit(EXIT_FAILURE);
- case interface_soundcard:
++ printf("Source hops: Invalid format type.\n");
++ exit(EXIT_FAILURE);
- rtnValue = src_hops_process_interface_soundcard(obj);
+ break;
+ }
- switch (obj->interface->type)
- {
- break;
++ switch (obj->interface->type) {
- case interface_file:
- case interface_pulseaudio:
++ case interface_file:
- rtnValue = src_hops_process_interface_file(obj);
- rtnValue = src_hops_process_interface_pulseaudio(obj);
++ rtnValue = src_hops_process_interface_file(obj);
- break;
+ break;
- case interface_soundcard:
- default:
++ case interface_soundcard:
- rtnValue = src_hops_process_interface_soundcard(obj);
- printf("Source hops: Invalid interface type.\n");
- exit(EXIT_FAILURE);
++ rtnValue = src_hops_process_interface_soundcard(obj);
- break;
+ break;
- case interface_socket:
- }
++ case interface_pulseaudio:
- rtnValue = src_hops_process_interface_socket(obj);
- obj->timeStamp++;
- obj->out->timeStamp = obj->timeStamp;
++ rtnValue = src_hops_process_interface_pulseaudio(obj);
- return rtnValue;
+ break;
- default:
- }
++ case interface_socket:
- printf("Source hops: Invalid interface type.\n");
- exit(EXIT_FAILURE);
- int src_hops_process_interface_file(src_hops_obj * obj) {
++ rtnValue = src_hops_process_interface_socket(obj);
+
- unsigned int nBytesTotal;
- int rtnValue;
++ break;
+
- nBytesTotal = fread(obj->buffer, sizeof(char), obj->bufferSize, obj->fp);
++ default:
+
- if (nBytesTotal == obj->bufferSize) {
-
- rtnValue = 0;
++ printf("Source hops: Invalid interface type.\n");
++ exit(EXIT_FAILURE);
- }
- else {
+ break;
+ }
- rtnValue = -1;
+ obj->timeStamp++;
+ obj->out->timeStamp = obj->timeStamp;
- }
+ return rtnValue;
+}
- int src_hops_process_interface_file(src_hops_obj *obj)
- {
- return rtnValue;
++int src_hops_process_interface_file(src_hops_obj * obj) {
- }
+ unsigned int nBytesTotal;
+ int rtnValue;
- int src_hops_process_interface_soundcard(src_hops_obj * obj) {
+ nBytesTotal = fread(obj->buffer, sizeof(char), obj->bufferSize, obj->fp);
- if (nBytesTotal == obj->bufferSize)
- {
- unsigned int nSamples;
- int rtnValue;
- int err;
++ if (nBytesTotal == obj->bufferSize) {
- if (err = snd_pcm_readi(obj->ch, obj->buffer, obj->hopSize) > 0) {
-
- rtnValue = 0;
+ rtnValue = 0;
++
+ }
- else
- {
++ else {
- }
- else {
+ rtnValue = -1;
++
+ }
- rtnValue = -1;
+ return rtnValue;
+}
- int src_hops_process_interface_soundcard(src_hops_obj *obj)
- {
- }
++int src_hops_process_interface_soundcard(src_hops_obj * obj) {
- return rtnValue;
+ unsigned int nSamples;
+ int rtnValue;
+ int err;
- if (err = snd_pcm_readi(obj->ch, obj->buffer, obj->hopSize) > 0)
- {
- }
++ if (err = snd_pcm_readi(obj->ch, obj->buffer, obj->hopSize) > 0) {
- int src_hops_process_interface_pulseaudio(src_hops_obj * obj) {
+ rtnValue = 0;
++
+ }
- else
- {
++ else {
- int rtnValue;
- int err;
+ rtnValue = -1;
++
+ }
- if (pa_simple_read(obj->pa, obj->buffer, obj->bufferSize, &err) < 0) {
-
- rtnValue = -1;
+ return rtnValue;
+}
- int src_hops_process_interface_socket(src_hops_obj *obj)
- {
- }
- else {
++int src_hops_process_interface_pulseaudio(src_hops_obj * obj) {
+
- rtnValue = 0;
++ int rtnValue;
++ int err;
+
- }
++ if (pa_simple_read(obj->pa, obj->buffer, obj->bufferSize, &err) < 0) {
+
- return rtnValue;
++ rtnValue = -1;
+
+ }
++ else {
+
- void src_hops_process_format_binary_int08(src_hops_obj * obj) {
++ rtnValue = 0;
+
- unsigned int iSample;
- unsigned int iChannel;
- unsigned int nBytes;
- float sample;
-
- nBytes = 1;
++ }
+
- for (iSample = 0; iSample < obj->hopSize; iSample++) {
++ return rtnValue;
+
- 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);
++int src_hops_process_interface_socket(src_hops_obj * obj) {
++
+ unsigned int nBytesTotal;
+ int rtnValue;
- obj->out->hops->array[iChannel][iSample] = sample;
+ // ssize_t recv(int sockfd, void *buf, size_t len, int flags);
+ nBytesTotal = recv(obj->sid, obj->buffer, obj->bufferSize, MSG_WAITALL);
- if (nBytesTotal == obj->bufferSize)
- {
- }
-
- }
++ if (nBytesTotal == obj->bufferSize) {
+ rtnValue = 0;
++
}
- else
- {
++ else {
- void src_hops_process_format_binary_int16(src_hops_obj * obj) {
+ rtnValue = -1;
++
+ }
- unsigned int iSample;
- unsigned int iChannel;
- unsigned int nBytes;
- float sample;
+ return rtnValue;
+}
- void src_hops_process_format_binary_int08(src_hops_obj *obj)
- {
- nBytes = 2;
++void src_hops_process_format_binary_int08(src_hops_obj * obj) {
- for (iSample = 0; iSample < obj->hopSize; iSample++) {
+ unsigned int iSample;
+ unsigned int iChannel;
+ unsigned int nBytes;
+ float sample;
- for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
+ nBytes = 1;
- for (iSample = 0; iSample < obj->hopSize; iSample++)
- {
- memcpy(&(obj->bytes[4-nBytes]),
- &(obj->buffer[(iSample * obj->nChannels + iChannel) * nBytes]),
- sizeof(char) * nBytes);
++ for (iSample = 0; iSample < obj->hopSize; iSample++) {
- for (iChannel = 0; iChannel < obj->nChannels; iChannel++)
- {
- sample = pcm_signedXXbits2normalized(obj->bytes, nBytes);
++ for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
- obj->out->hops->array[iChannel][iSample] = sample;
+ 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;
++
}
-
++
}
++
+}
- void src_hops_process_format_binary_int16(src_hops_obj *obj)
- {
- void src_hops_process_format_binary_int24(src_hops_obj * obj) {
++void src_hops_process_format_binary_int16(src_hops_obj * obj) {
- unsigned int iSample;
- unsigned int iChannel;
- unsigned int nBytes;
- float sample;
+ unsigned int iSample;
+ unsigned int iChannel;
+ unsigned int nBytes;
+ float sample;
- nBytes = 3;
+ nBytes = 2;
- for (iSample = 0; iSample < obj->hopSize; iSample++)
- {
- for (iSample = 0; iSample < obj->hopSize; iSample++) {
++ for (iSample = 0; iSample < obj->hopSize; iSample++) {
- for (iChannel = 0; iChannel < obj->nChannels; iChannel++)
- {
- for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
++ for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
- memcpy(&(obj->bytes[4-nBytes]),
- &(obj->buffer[(iSample * obj->nChannels + iChannel) * nBytes]),
- sizeof(char) * nBytes);
+ memcpy(&(obj->bytes[4 - nBytes]),
+ &(obj->buffer[(iSample * obj->nChannels + iChannel) * nBytes]),
+ sizeof(char) * nBytes);
- sample = pcm_signedXXbits2normalized(obj->bytes, nBytes);
+ sample = pcm_signedXXbits2normalized(obj->bytes, nBytes);
- obj->out->hops->array[iChannel][iSample] = sample;
+ obj->out->hops->array[iChannel][iSample] = sample;
++
+ }
++
+ }
++
+}
- void src_hops_process_format_binary_int24(src_hops_obj *obj)
- {
- }
++void src_hops_process_format_binary_int24(src_hops_obj * obj) {
- }
+ unsigned int iSample;
+ unsigned int iChannel;
+ unsigned int nBytes;
+ float sample;
- }
+ nBytes = 3;
- for (iSample = 0; iSample < obj->hopSize; iSample++)
- {
- void src_hops_process_format_binary_int32(src_hops_obj * obj) {
++ for (iSample = 0; iSample < obj->hopSize; iSample++) {
- for (iChannel = 0; iChannel < obj->nChannels; iChannel++)
- {
- unsigned int iSample;
- unsigned int iChannel;
- unsigned int nBytes;
- float sample;
++ for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
- nBytes = 4;
+ memcpy(&(obj->bytes[4 - nBytes]),
+ &(obj->buffer[(iSample * obj->nChannels + iChannel) * nBytes]),
+ sizeof(char) * nBytes);
- for (iSample = 0; iSample < obj->hopSize; iSample++) {
+ sample = pcm_signedXXbits2normalized(obj->bytes, nBytes);
- for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
+ obj->out->hops->array[iChannel][iSample] = sample;
++
+ }
++
+ }
++
+}
- void src_hops_process_format_binary_int32(src_hops_obj *obj)
- {
- memcpy(&(obj->bytes[4-nBytes]),
- &(obj->buffer[(iSample * obj->nChannels + iChannel) * nBytes]),
- sizeof(char) * nBytes);
++void src_hops_process_format_binary_int32(src_hops_obj * obj) {
- sample = pcm_signedXXbits2normalized(obj->bytes, nBytes);
+ unsigned int iSample;
+ unsigned int iChannel;
+ unsigned int nBytes;
+ float sample;
- obj->out->hops->array[iChannel][iSample] = sample;
+ nBytes = 4;
- for (iSample = 0; iSample < obj->hopSize; iSample++)
- {
- }
++ for (iSample = 0; iSample < obj->hopSize; iSample++) {
- for (iChannel = 0; iChannel < obj->nChannels; iChannel++)
- {
- }
++ for (iChannel = 0; iChannel < obj->nChannels; iChannel++) {
- }
+ memcpy(&(obj->bytes[4 - nBytes]),
+ &(obj->buffer[(iSample * obj->nChannels + iChannel) * nBytes]),
+ sizeof(char) * nBytes);
- src_hops_cfg * src_hops_cfg_construct(void) {
+ sample = pcm_signedXXbits2normalized(obj->bytes, nBytes);
- src_hops_cfg * cfg;
+ obj->out->hops->array[iChannel][iSample] = sample;
++
+ }
++
+ }
+
- cfg = (src_hops_cfg *) malloc(sizeof(src_hops_cfg));
+}
- src_hops_cfg *src_hops_cfg_construct(void)
- {
- cfg->format = (format_obj *) NULL;
- cfg->interface = (interface_obj *) NULL;
++src_hops_cfg * src_hops_cfg_construct(void) {
- src_hops_cfg *cfg;
- return cfg;
++ src_hops_cfg * cfg;
- cfg = (src_hops_cfg *)malloc(sizeof(src_hops_cfg));
- }
++ cfg = (src_hops_cfg *) malloc(sizeof(src_hops_cfg));
- cfg->format = (format_obj *)NULL;
- cfg->interface = (interface_obj *)NULL;
- void src_hops_cfg_destroy(src_hops_cfg * src_hops_config) {
++ cfg->format = (format_obj *) NULL;
++ cfg->interface = (interface_obj *) NULL;
+
+ return cfg;
+}
- void src_hops_cfg_destroy(src_hops_cfg *src_hops_config)
- {
- if (src_hops_config->format != NULL) {
- format_destroy(src_hops_config->format);
- }
- if (src_hops_config->interface != NULL) {
- interface_destroy(src_hops_config->interface);
- }
- if (src_hops_config->channel_map != NULL) {
- free((void *) src_hops_config->channel_map);
- }
- if (src_hops_config->format != NULL)
- {
- free((void *) src_hops_config);
++void src_hops_cfg_destroy(src_hops_cfg * src_hops_config) {
+
++ if (src_hops_config->format != NULL) {
+ format_destroy(src_hops_config->format);
}
- if (src_hops_config->interface != NULL)
- {
++ if (src_hops_config->interface != NULL) {
+ interface_destroy(src_hops_config->interface);
+ }
++ if (src_hops_config->channel_map != NULL) {
++ free((void *) src_hops_config->channel_map);
++ }
+
- free((void *)src_hops_config);
++ free((void *) src_hops_config);
++
+}