mirror of
				https://github.com/Tha14/toxic.git
				synced 2025-10-31 03:06:51 +01:00 
			
		
		
		
	| @@ -192,7 +192,8 @@ int start_transmission(ToxWindow *self) | |||||||
|      |      | ||||||
|     set_call(&ASettins.calls[self->call_idx], _True); |     set_call(&ASettins.calls[self->call_idx], _True); | ||||||
|          |          | ||||||
|     if ( open_primary_device(input, &ASettins.calls[self->call_idx].in_idx) != de_None )  |     if ( open_primary_device(input, &ASettins.calls[self->call_idx].in_idx,  | ||||||
|  |             av_DefaultSettings.audio_sample_rate, av_DefaultSettings.audio_frame_duration) != de_None )  | ||||||
|         line_info_add(self, NULL, NULL, NULL, "Failed to open input device!", SYS_MSG, 0, 0); |         line_info_add(self, NULL, NULL, NULL, "Failed to open input device!", SYS_MSG, 0, 0); | ||||||
|      |      | ||||||
|     if ( register_device_callback(self->call_idx, ASettins.calls[self->call_idx].in_idx,  |     if ( register_device_callback(self->call_idx, ASettins.calls[self->call_idx].in_idx,  | ||||||
| @@ -200,7 +201,8 @@ int start_transmission(ToxWindow *self) | |||||||
|         /* Set VAD as true for all; TODO: Make it more dynamic */ |         /* Set VAD as true for all; TODO: Make it more dynamic */ | ||||||
|         line_info_add(self, NULL, NULL, NULL, "Failed to register input handler!", SYS_MSG, 0, 0); |         line_info_add(self, NULL, NULL, NULL, "Failed to register input handler!", SYS_MSG, 0, 0); | ||||||
|      |      | ||||||
|     if ( open_primary_device(output, &ASettins.calls[self->call_idx].out_idx) != de_None ) { |     if ( open_primary_device(output, &ASettins.calls[self->call_idx].out_idx,  | ||||||
|  |             av_DefaultSettings.audio_sample_rate, av_DefaultSettings.audio_frame_duration) != de_None ) { | ||||||
|         line_info_add(self, NULL, NULL, NULL, "Failed to open output device!", SYS_MSG, 0, 0); |         line_info_add(self, NULL, NULL, NULL, "Failed to open output device!", SYS_MSG, 0, 0); | ||||||
|         ASettins.calls[self->call_idx].has_output = 0; |         ASettins.calls[self->call_idx].has_output = 0; | ||||||
|     } |     } | ||||||
| @@ -617,14 +619,16 @@ void cmd_ccur_device(WINDOW * window, ToxWindow * self, Tox *m, int argc, char ( | |||||||
|             if (type == output) { |             if (type == output) { | ||||||
|                 pthread_mutex_lock(&this_call->mutex); |                 pthread_mutex_lock(&this_call->mutex); | ||||||
|                 close_device(output, this_call->out_idx); |                 close_device(output, this_call->out_idx); | ||||||
|                 this_call->has_output = open_device(output, selection, &this_call->out_idx)  |                 this_call->has_output = open_device(output, selection, &this_call->out_idx,  | ||||||
|  |                         av_DefaultSettings.audio_sample_rate, av_DefaultSettings.audio_frame_duration)  | ||||||
|                     == de_None ? 1 : 0; |                     == de_None ? 1 : 0; | ||||||
|                 pthread_mutex_unlock(&this_call->mutex); |                 pthread_mutex_unlock(&this_call->mutex); | ||||||
|             } |             } | ||||||
|             else { |             else { | ||||||
|                 /* TODO: check for failure */ |                 /* TODO: check for failure */ | ||||||
|                 close_device(input, this_call->in_idx); |                 close_device(input, this_call->in_idx); | ||||||
|                 open_device(input, selection, &this_call->in_idx); |                 open_device(input, selection, &this_call->in_idx,  | ||||||
|  |                     av_DefaultSettings.audio_sample_rate, av_DefaultSettings.audio_frame_duration); | ||||||
|                 /* Set VAD as true for all; TODO: Make it more dynamic */ |                 /* Set VAD as true for all; TODO: Make it more dynamic */ | ||||||
|                 register_device_callback(self->call_idx, this_call->in_idx, read_device_callback, &self->call_idx, _True); |                 register_device_callback(self->call_idx, this_call->in_idx, read_device_callback, &self->call_idx, _True); | ||||||
|             } |             } | ||||||
|   | |||||||
| @@ -27,8 +27,6 @@ | |||||||
|  |  | ||||||
| #include "device.h" | #include "device.h" | ||||||
|  |  | ||||||
| #define VAD_THRESHOLD_DEFAULT 40.0 |  | ||||||
|  |  | ||||||
| typedef enum _AudioError { | typedef enum _AudioError { | ||||||
|     ae_None = 0, |     ae_None = 0, | ||||||
|     ae_StartingCaptureDevice = 1 << 0, |     ae_StartingCaptureDevice = 1 << 0, | ||||||
|   | |||||||
| @@ -611,6 +611,7 @@ void chat_onPeerTimeout (ToxWindow *self, ToxAv *av, int call_index) | |||||||
| #endif /* _SOUND_NOTIFY */ | #endif /* _SOUND_NOTIFY */ | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #ifdef _AUDIO | ||||||
| static void init_infobox(ToxWindow *self) | static void init_infobox(ToxWindow *self) | ||||||
| { | { | ||||||
|     ChatContext *ctx = self->chatwin; |     ChatContext *ctx = self->chatwin; | ||||||
| @@ -623,10 +624,11 @@ static void init_infobox(ToxWindow *self) | |||||||
|  |  | ||||||
|     ctx->infobox.win = newwin(INFOBOX_HEIGHT, INFOBOX_WIDTH + 1, 1, x2 - INFOBOX_WIDTH); |     ctx->infobox.win = newwin(INFOBOX_HEIGHT, INFOBOX_WIDTH + 1, 1, x2 - INFOBOX_WIDTH); | ||||||
|     ctx->infobox.starttime = get_unix_time(); |     ctx->infobox.starttime = get_unix_time(); | ||||||
|     ctx->infobox.vad_lvl = VAD_THRESHOLD_DEFAULT; |     ctx->infobox.vad_lvl = user_settings_->VAD_treshold; | ||||||
|     ctx->infobox.active = true; |     ctx->infobox.active = true; | ||||||
|     strcpy(ctx->infobox.timestr, "00"); |     strcpy(ctx->infobox.timestr, "00"); | ||||||
| } | } | ||||||
|  | #endif | ||||||
|  |  | ||||||
| static void kill_infobox(ToxWindow *self) | static void kill_infobox(ToxWindow *self) | ||||||
| { | { | ||||||
|   | |||||||
							
								
								
									
										61
									
								
								src/device.c
									
									
									
									
									
								
							
							
						
						
									
										61
									
								
								src/device.c
									
									
									
									
									
								
							| @@ -20,8 +20,14 @@ | |||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | #include "device.h" | ||||||
|  |  | ||||||
|  | #ifdef _AUDIO | ||||||
| #include "audio_call.h" | #include "audio_call.h" | ||||||
|  | #endif | ||||||
|  |  | ||||||
| #include "line_info.h" | #include "line_info.h" | ||||||
|  | #include "settings.h" | ||||||
|  |  | ||||||
| #ifdef __APPLE__ | #ifdef __APPLE__ | ||||||
| #include <OpenAL/al.h> | #include <OpenAL/al.h> | ||||||
| @@ -37,12 +43,10 @@ | |||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| #include <assert.h> | #include <assert.h> | ||||||
|  |  | ||||||
| #include <tox/toxav.h> | #define OPENAL_BUFS 5 | ||||||
|  |  | ||||||
| #define openal_bufs 5 |  | ||||||
| #define sample_rate 48000 |  | ||||||
| #define inline__ inline __attribute__((always_inline)) | #define inline__ inline __attribute__((always_inline)) | ||||||
| #define frame_size (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000) |  | ||||||
|  | extern struct user_settings *user_settings_; | ||||||
|  |  | ||||||
| typedef struct _Device { | typedef struct _Device { | ||||||
|     ALCdevice  *dhndl;                     /* Handle of device selected/opened */ |     ALCdevice  *dhndl;                     /* Handle of device selected/opened */ | ||||||
| @@ -51,13 +55,17 @@ typedef struct _Device { | |||||||
|     void* cb_data;                         /* Data to be passed to callback */ |     void* cb_data;                         /* Data to be passed to callback */ | ||||||
|     int32_t call_idx;                      /* ToxAv call index */ |     int32_t call_idx;                      /* ToxAv call index */ | ||||||
|      |      | ||||||
|     uint32_t source, buffers[openal_bufs]; /* Playback source/buffers */ |     uint32_t source, buffers[OPENAL_BUFS]; /* Playback source/buffers */ | ||||||
|     size_t ref_count; |     size_t ref_count; | ||||||
|     int32_t selection; |     int32_t selection; | ||||||
|     _Bool enable_VAD; |     _Bool enable_VAD; | ||||||
|     _Bool muted; |     _Bool muted; | ||||||
|     float VAD_treshold;                    /* 40 is usually recommended value */ |  | ||||||
|     pthread_mutex_t mutex[1]; |     pthread_mutex_t mutex[1]; | ||||||
|  |     uint32_t sample_rate;  | ||||||
|  |     uint32_t frame_duration; | ||||||
|  | #ifdef _AUDIO | ||||||
|  |     float VAD_treshold;                    /* 40 is usually recommended value */ | ||||||
|  | #endif | ||||||
| } Device; | } Device; | ||||||
|  |  | ||||||
| const char *ddevice_names[2];              /* Default device */ | const char *ddevice_names[2];              /* Default device */ | ||||||
| @@ -125,7 +133,7 @@ DeviceError init_devices() | |||||||
|     av = av_; |     av = av_; | ||||||
| #endif /* _AUDIO */ | #endif /* _AUDIO */ | ||||||
|      |      | ||||||
|     return (DeviceError) ae_None; |     return (DeviceError) de_None; | ||||||
| } | } | ||||||
|  |  | ||||||
| DeviceError terminate_devices() | DeviceError terminate_devices() | ||||||
| @@ -136,7 +144,7 @@ DeviceError terminate_devices() | |||||||
|      |      | ||||||
|     pthread_mutex_destroy(&mutex); |     pthread_mutex_destroy(&mutex); | ||||||
|      |      | ||||||
|     return (DeviceError) ae_None; |     return (DeviceError) de_None; | ||||||
| } | } | ||||||
|  |  | ||||||
| DeviceError device_mute(DeviceType type, uint32_t device_idx) | DeviceError device_mute(DeviceType type, uint32_t device_idx) | ||||||
| @@ -157,6 +165,7 @@ DeviceError device_mute(DeviceType type, uint32_t device_idx) | |||||||
|     return de_None; |     return de_None; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #ifdef _AUDIO | ||||||
| DeviceError device_set_VAD_treshold(uint32_t device_idx, float value) | DeviceError device_set_VAD_treshold(uint32_t device_idx, float value) | ||||||
| { | { | ||||||
|     if (device_idx >= MAX_DEVICES) return de_InvalidSelection; |     if (device_idx >= MAX_DEVICES) return de_InvalidSelection; | ||||||
| @@ -174,6 +183,8 @@ DeviceError device_set_VAD_treshold(uint32_t device_idx, float value) | |||||||
|     unlock; |     unlock; | ||||||
|     return de_None; |     return de_None; | ||||||
| } | } | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| DeviceError set_primary_device(DeviceType type, int32_t selection) | DeviceError set_primary_device(DeviceType type, int32_t selection) | ||||||
| { | { | ||||||
| @@ -183,28 +194,33 @@ DeviceError set_primary_device(DeviceType type, int32_t selection) | |||||||
|     return de_None; |     return de_None; | ||||||
| } | } | ||||||
|  |  | ||||||
| DeviceError open_primary_device(DeviceType type, uint32_t* device_idx) | DeviceError open_primary_device(DeviceType type, uint32_t* device_idx, uint32_t sample_rate, uint32_t frame_duration) | ||||||
| { | { | ||||||
|     return open_device(type, primary_device[type], device_idx); |     return open_device(type, primary_device[type], device_idx, sample_rate, frame_duration); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // TODO: generate buffers separately | // TODO: generate buffers separately | ||||||
| DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx) | DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx, uint32_t sample_rate, uint32_t frame_duration) | ||||||
| { | { | ||||||
|     if (size[type] <= selection || selection < 0) return de_InvalidSelection; |     if (size[type] <= selection || selection < 0) return de_InvalidSelection; | ||||||
|      |      | ||||||
|     lock; |     lock; | ||||||
|      |      | ||||||
|  |     const uint32_t frame_size = (sample_rate * frame_duration / 1000); | ||||||
|  |      | ||||||
|     uint32_t i; |     uint32_t i; | ||||||
|     for (i = 0; i < MAX_DEVICES && running[type][i] != NULL; i ++); |     for (i = 0; i < MAX_DEVICES && running[type][i] != NULL; i ++); | ||||||
|      |      | ||||||
|     if (i == MAX_DEVICES) { unlock; return de_AllDevicesBusy; } |     if (i == MAX_DEVICES) { unlock; return de_AllDevicesBusy; } | ||||||
|     else *device_idx = i; |     else *device_idx = i; | ||||||
|      |      | ||||||
|     Device* device = running[type][*device_idx] = calloc(1, sizeof(Device));; |     Device* device = running[type][*device_idx] = calloc(1, sizeof(Device)); | ||||||
|     device->selection = selection; |     device->selection = selection; | ||||||
|      |      | ||||||
|  |     device->sample_rate = sample_rate; | ||||||
|  |     device->frame_duration = frame_duration; | ||||||
|  |      | ||||||
|     for (i = 0; i < *device_idx; i ++) { /* Check if any previous has the same selection */ |     for (i = 0; i < *device_idx; i ++) { /* Check if any previous has the same selection */ | ||||||
|         if ( running[type][i]->selection == selection ) { |         if ( running[type][i]->selection == selection ) { | ||||||
|             device->dhndl = running[type][i]->dhndl; |             device->dhndl = running[type][i]->dhndl; | ||||||
| @@ -222,8 +238,10 @@ DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx | |||||||
|      |      | ||||||
|     if (type == input) { |     if (type == input) { | ||||||
|         device->dhndl = alcCaptureOpenDevice(devices_names[type][selection],  |         device->dhndl = alcCaptureOpenDevice(devices_names[type][selection],  | ||||||
|                         av_DefaultSettings.audio_sample_rate, AL_FORMAT_MONO16, frame_size * 2); |                                              sample_rate, AL_FORMAT_MONO16, frame_size * 2); | ||||||
|         device->VAD_treshold = VAD_THRESHOLD_DEFAULT; |     #ifdef _AUDIO | ||||||
|  |         device->VAD_treshold = user_settings_->VAD_treshold; | ||||||
|  |     #endif | ||||||
|     } |     } | ||||||
|     else {  |     else {  | ||||||
|         device->dhndl = alcOpenDevice(devices_names[type][selection]); |         device->dhndl = alcOpenDevice(devices_names[type][selection]); | ||||||
| @@ -237,18 +255,18 @@ DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx | |||||||
|         device->ctx = alcCreateContext(device->dhndl, NULL); |         device->ctx = alcCreateContext(device->dhndl, NULL); | ||||||
|         alcMakeContextCurrent(device->ctx); |         alcMakeContextCurrent(device->ctx); | ||||||
|          |          | ||||||
|         alGenBuffers(openal_bufs, device->buffers); |         alGenBuffers(OPENAL_BUFS, device->buffers); | ||||||
|         alGenSources((uint32_t)1, &device->source); |         alGenSources((uint32_t)1, &device->source); | ||||||
|         alSourcei(device->source, AL_LOOPING, AL_FALSE); |         alSourcei(device->source, AL_LOOPING, AL_FALSE); | ||||||
|          |          | ||||||
|         uint16_t zeros[frame_size]; |         uint16_t zeros[frame_size]; | ||||||
|         memset(zeros, 0, frame_size*2); |         memset(zeros, 0, frame_size*2); | ||||||
|          |          | ||||||
|         for ( i =0; i < openal_bufs; ++i) { |         for ( i =0; i < OPENAL_BUFS; ++i) { | ||||||
|             alBufferData(device->buffers[i], AL_FORMAT_MONO16, zeros, frame_size*2, sample_rate); |             alBufferData(device->buffers[i], AL_FORMAT_MONO16, zeros, frame_size*2, sample_rate); | ||||||
|         } |         } | ||||||
|          |          | ||||||
|         alSourceQueueBuffers(device->source, openal_bufs, device->buffers); |         alSourceQueueBuffers(device->source, OPENAL_BUFS, device->buffers); | ||||||
|         alSourcePlay(device->source); |         alSourcePlay(device->source); | ||||||
|     } |     } | ||||||
|      |      | ||||||
| @@ -294,7 +312,7 @@ DeviceError close_device(DeviceType type, uint32_t device_idx) | |||||||
|             if (alcGetCurrentContext() != device->ctx) alcMakeContextCurrent(device->ctx); |             if (alcGetCurrentContext() != device->ctx) alcMakeContextCurrent(device->ctx); | ||||||
|              |              | ||||||
|             alDeleteSources(1, &device->source); |             alDeleteSources(1, &device->source); | ||||||
|             alDeleteBuffers(openal_bufs, device->buffers); |             alDeleteBuffers(OPENAL_BUFS, device->buffers); | ||||||
|              |              | ||||||
|             if ( !alcCloseDevice(device->dhndl) ) rc = de_AlError; |             if ( !alcCloseDevice(device->dhndl) ) rc = de_AlError; | ||||||
|             alcMakeContextCurrent(NULL); |             alcMakeContextCurrent(NULL); | ||||||
| @@ -354,7 +372,7 @@ inline__ DeviceError write_out(uint32_t device_idx, int16_t* data, uint32_t leng | |||||||
|     } |     } | ||||||
|      |      | ||||||
|      |      | ||||||
|     alBufferData(bufid, AL_FORMAT_MONO16, data, lenght * 2 * channels, av_DefaultSettings.audio_sample_rate); |     alBufferData(bufid, AL_FORMAT_MONO16, data, lenght * 2 * channels, device->sample_rate); | ||||||
|     alSourceQueueBuffers(device->source, 1, &bufid); |     alSourceQueueBuffers(device->source, 1, &bufid); | ||||||
|      |      | ||||||
|     ALint state; |     ALint state; | ||||||
| @@ -376,7 +394,6 @@ void* thread_poll (void* arg) // TODO: maybe use thread for every input source | |||||||
|     uint32_t i; |     uint32_t i; | ||||||
|     int32_t sample = 0; |     int32_t sample = 0; | ||||||
|      |      | ||||||
|     int f_size = frame_size; |  | ||||||
|      |      | ||||||
|     while (thread_running) |     while (thread_running) | ||||||
|     { |     { | ||||||
| @@ -390,6 +407,8 @@ void* thread_poll (void* arg) // TODO: maybe use thread for every input source | |||||||
|                 { |                 { | ||||||
|                     alcGetIntegerv(running[input][i]->dhndl, ALC_CAPTURE_SAMPLES, sizeof(int32_t), &sample); |                     alcGetIntegerv(running[input][i]->dhndl, ALC_CAPTURE_SAMPLES, sizeof(int32_t), &sample); | ||||||
|                      |                      | ||||||
|  |                     int f_size = (running[input][i]->sample_rate * running[input][i]->frame_duration / 1000); | ||||||
|  |                      | ||||||
|                     if (sample < f_size) {  |                     if (sample < f_size) {  | ||||||
|                         unlock; |                         unlock; | ||||||
|                         continue; |                         continue; | ||||||
|   | |||||||
| @@ -71,12 +71,14 @@ void* get_device_callback_data(uint32_t device_idx); | |||||||
| /* toggle device mute */ | /* toggle device mute */ | ||||||
| DeviceError device_mute(DeviceType type, uint32_t device_idx); | DeviceError device_mute(DeviceType type, uint32_t device_idx); | ||||||
|  |  | ||||||
|  | #ifdef _AUDIO | ||||||
| DeviceError device_set_VAD_treshold(uint32_t device_idx, float value); | DeviceError device_set_VAD_treshold(uint32_t device_idx, float value); | ||||||
|  | #endif | ||||||
|  |  | ||||||
| DeviceError set_primary_device(DeviceType type, int32_t selection); | DeviceError set_primary_device(DeviceType type, int32_t selection); | ||||||
| DeviceError open_primary_device(DeviceType type, uint32_t* device_idx); | DeviceError open_primary_device(DeviceType type, uint32_t* device_idx, uint32_t sample_rate, uint32_t frame_duration); | ||||||
| /* Start device */ | /* Start device */ | ||||||
| DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx); | DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx, uint32_t sample_rate, uint32_t frame_duration); | ||||||
| /* Stop device */ | /* Stop device */ | ||||||
| DeviceError close_device(DeviceType type, uint32_t device_idx); | DeviceError close_device(DeviceType type, uint32_t device_idx); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -74,7 +74,7 @@ struct _ActiveSounds { | |||||||
|     uint32_t buffer; |     uint32_t buffer; | ||||||
|     _Bool active; |     _Bool active; | ||||||
|     _Bool looping; |     _Bool looping; | ||||||
| } actives[ACTIVE_SOUNDS_MAX] = {0}; | } actives[ACTIVE_SOUNDS_MAX] = {{0}}; | ||||||
| #endif | #endif | ||||||
| /**********************************************************************************/ | /**********************************************************************************/ | ||||||
| /**********************************************************************************/ | /**********************************************************************************/ | ||||||
| @@ -154,6 +154,7 @@ void* do_playing(void* _p) | |||||||
|         pthread_mutex_unlock(Control.poll_mutex); |         pthread_mutex_unlock(Control.poll_mutex); | ||||||
|         usleep(10000); |         usleep(10000); | ||||||
|     } |     } | ||||||
|  |     pthread_exit(NULL); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -190,7 +191,7 @@ int play_source(uint32_t source, uint32_t buffer, _Bool looping) | |||||||
| int init_notify(int login_cooldown) | int init_notify(int login_cooldown) | ||||||
| { | { | ||||||
| #ifdef _SOUND_NOTIFY | #ifdef _SOUND_NOTIFY | ||||||
|     if (open_primary_device(output, &Control.device_idx) != de_None) |     if (open_primary_device(output, &Control.device_idx, 48000, 20) != de_None) | ||||||
|         return -1; |         return -1; | ||||||
|          |          | ||||||
|     pthread_mutex_init(Control.poll_mutex, NULL); |     pthread_mutex_init(Control.poll_mutex, NULL); | ||||||
| @@ -248,7 +249,7 @@ int play_sound_internal(Notification what, _Bool loop) | |||||||
|     uint32_t source; |     uint32_t source; | ||||||
|     uint32_t buffer; |     uint32_t buffer; | ||||||
|      |      | ||||||
|     alutLoadWAVFile(Control.sounds[what], &format, (void**)&data, &buffer_size, &clockrate, &loop_); |     alutLoadWAVFile((signed char*)Control.sounds[what], &format, (void**)&data, &buffer_size, &clockrate, &loop_); | ||||||
|     alGenSources(1, &source); |     alGenSources(1, &source); | ||||||
|     alGenBuffers(1, &buffer); |     alGenBuffers(1, &buffer); | ||||||
|     alBufferData(buffer, format, data, buffer_size, clockrate); |     alBufferData(buffer, format, data, buffer_size, clockrate); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user