Browse Source

added end sound func pointer

Iver 2 days ago
parent
commit
52ab40ea62
7 changed files with 50 additions and 23 deletions
  1. BIN
      builds/libyuzuparse.so
  2. BIN
      builds/main.bin
  3. 1 2
      changelog.txt
  4. 10 1
      src/headers/YZ_functions.h
  5. 2 0
      src/headers/YZ_types.h
  6. 12 3
      src/launch program/main.c
  7. 25 17
      src/main/main.c

BIN
builds/libyuzuparse.so


BIN
builds/main.bin


+ 1 - 2
changelog.txt

@@ -1,2 +1 @@
--made callback exit safer
--added dynamically playback, can now change speed (can be positive and negative) and can loop audio
+-added function pointer that you can pass that runs at the end of a sound being played

+ 10 - 1
src/headers/YZ_functions.h

@@ -3,10 +3,19 @@
 
 
 #include "YZ_types.h"
 #include "YZ_types.h"
 
 
+// loads an audio file into a playable format
 YZ_audio_stream* YZ_load_audio_file(char* filename, unsigned char debug_mode);
 YZ_audio_stream* YZ_load_audio_file(char* filename, unsigned char debug_mode);
+
+// play an audio stream one time though
 pa_callback_data* YZ_play_stream(YZ_audio_stream* audio_stream);
 pa_callback_data* YZ_play_stream(YZ_audio_stream* audio_stream);
-pa_callback_data* YZ_play_stream_dynamic(YZ_audio_stream* audio_stream, double* speed_multiplier, unsigned char* should_loop_audio);
+
+// play an audio stream with optional effects. You can change speed, turn on looping, or set the starting second of the stream.
+pa_callback_data* YZ_play_stream_dynamic(YZ_audio_stream* audio_stream, double* speed_multiplier, unsigned char* should_loop_audio, double start_second, void (*end_function)(void*), void* custom_pointer_for_end_function);
+
+// Initializes the audio player. Playing a stream when the player is uninitializes automatically runs this function.
 void YZ_init_player();
 void YZ_init_player();
+
+// Kills the player and frees memory
 void YZ_kill_player();
 void YZ_kill_player();
 
 
 #endif
 #endif

+ 2 - 0
src/headers/YZ_types.h

@@ -18,6 +18,8 @@ typedef struct {
     double* pitch_multiplier;
     double* pitch_multiplier;
     double* speed_multiplier;
     double* speed_multiplier;
     unsigned char* should_loop_audio;
     unsigned char* should_loop_audio;
+    void(*end_function)(void*);
+    void* custom_pointer_for_end_function;
 } pa_callback_data;
 } pa_callback_data;
 
 
 #endif
 #endif

+ 12 - 3
src/launch program/main.c

@@ -1,15 +1,24 @@
 #include "../headers/yuzuparse.h"
 #include "../headers/yuzuparse.h"
 
 
+void end_func(void* custom_pointer){
+    printf((char*)custom_pointer);
+
+    return;
+}
+
 int main(){
 int main(){
     YZ_audio_stream* audio_stream = YZ_load_audio_file("audio/voice.wav", 0);
     YZ_audio_stream* audio_stream = YZ_load_audio_file("audio/voice.wav", 0);
 
 
     pa_callback_data* callback_data;
     pa_callback_data* callback_data;
 
 
-    double speed = -1;
-    unsigned char should_loop_audio = 1;
+    double speed = 1;
+    unsigned char should_loop_audio = 0;
+    double starting_second = 14;
+
+    char end_string[] = "sound has ended!\n";
 
 
     if (audio_stream)
     if (audio_stream)
-        callback_data = YZ_play_stream_dynamic(audio_stream, &speed, &should_loop_audio);
+        callback_data = YZ_play_stream_dynamic(audio_stream, &speed, &should_loop_audio, starting_second, end_func, end_string);
     
     
     // wait until audio is finished playing until exiting
     // wait until audio is finished playing until exiting
     while (callback_data->current_sample < callback_data->sample_count){}
     while (callback_data->current_sample < callback_data->sample_count){}

+ 25 - 17
src/main/main.c

@@ -67,16 +67,20 @@ int8_t get_1(){
 
 
 int16_t get_2(){
 int16_t get_2(){
     int16_t result = file_buffer[current_byte] | (file_buffer[current_byte + 1] << 8);
     int16_t result = file_buffer[current_byte] | (file_buffer[current_byte + 1] << 8);
+    
     current_byte += 2;
     current_byte += 2;
+    
     return result;
     return result;
 }
 }
 
 
 int32_t get_4(){
 int32_t get_4(){
     int32_t result = file_buffer[current_byte] | 
     int32_t result = file_buffer[current_byte] | 
-                     (file_buffer[current_byte + 1] << 8) | 
-                     (file_buffer[current_byte + 2] << 16) | 
-                     (file_buffer[current_byte + 3] << 24);
+        (file_buffer[current_byte + 1] << 8) | 
+        (file_buffer[current_byte + 2] << 16) | 
+        (file_buffer[current_byte + 3] << 24);
+    
     current_byte += 4;
     current_byte += 4;
+
     return result;
     return result;
 }
 }
 
 
@@ -180,7 +184,6 @@ YZ_audio_stream* YZ_load_wav(){
     return audio_stream;
     return audio_stream;
 }
 }
 
 
-
 YZ_audio_stream* YZ_load_mp3(){return NULL;}
 YZ_audio_stream* YZ_load_mp3(){return NULL;}
 YZ_audio_stream* YZ_load_ogg(){return NULL;}
 YZ_audio_stream* YZ_load_ogg(){return NULL;}
 YZ_audio_stream* YZ_load_flac(){return NULL;}
 YZ_audio_stream* YZ_load_flac(){return NULL;}
@@ -223,7 +226,7 @@ YZ_audio_stream* YZ_load_audio_file(char* filename, unsigned char debug_mode){
     }
     }
     
     
     current_byte = 0;
     current_byte = 0;
-
+    
     if (!strcmp(filetype_string, "wav") || !strcmp(filetype_string, "WAV") || !strcmp(filetype_string, "wave") || !strcmp(filetype_string, "WAVE")){ 
     if (!strcmp(filetype_string, "wav") || !strcmp(filetype_string, "WAV") || !strcmp(filetype_string, "wave") || !strcmp(filetype_string, "WAVE")){ 
         return YZ_load_wav();
         return YZ_load_wav();
     } else 
     } else 
@@ -266,19 +269,22 @@ int portaudio_callback(const void* input, void* output, unsigned long frame_coun
         }
         }
 
 
         if (data->current_sample >= data->sample_count || data->current_sample < 0){
         if (data->current_sample >= data->sample_count || data->current_sample < 0){
-            free(audio_data);
-
-            return 1;
+            goto END;
         }
         }
     }
     }
 
 
     if (data->current_sample >= data->sample_count){
     if (data->current_sample >= data->sample_count){
-        free(audio_data);
-
-        return 1;
+        goto END;
     }
     }
-    
+
     return 0;
     return 0;
+
+    END:
+        if (data->end_function) data->end_function(data->custom_pointer_for_end_function);
+
+        free(audio_data);
+
+        return 1;    
 }
 }
 
 
 unsigned char player_is_initialized = 0;
 unsigned char player_is_initialized = 0;
@@ -320,7 +326,7 @@ void YZ_kill_player(){
     if(error != paNoError) { printf("PortAudio error: %d\n", error); exit(1);}
     if(error != paNoError) { printf("PortAudio error: %d\n", error); exit(1);}
 }
 }
 
 
-pa_callback_data* play_stream(YZ_audio_stream* audio_stream, double* pitch_multiplier, double* speed_multiplier, unsigned char* should_loop_audio){
+pa_callback_data* play_stream(YZ_audio_stream* audio_stream, double* pitch_multiplier, double* speed_multiplier, unsigned char* should_loop_audio, double starting_second, void (*end_func)(void* custom_pointer_for_end_function), void* custom_pointer_for_end_function){
     PaError error;
     PaError error;
     
     
     PaStream *stream;
     PaStream *stream;
@@ -328,12 +334,14 @@ pa_callback_data* play_stream(YZ_audio_stream* audio_stream, double* pitch_multi
     pa_callback_data* callback_data = malloc(sizeof(pa_callback_data));
     pa_callback_data* callback_data = malloc(sizeof(pa_callback_data));
 
 
     callback_data->channel_count = audio_stream->channel_count;
     callback_data->channel_count = audio_stream->channel_count;
-    callback_data->current_sample = 0;
+    callback_data->current_sample = starting_second >= 0 ? audio_stream->sample_rate * starting_second : audio_stream->sample_count;
     callback_data->pcm_data = audio_stream->pcm_data;
     callback_data->pcm_data = audio_stream->pcm_data;
     callback_data->sample_count = audio_stream->sample_count;
     callback_data->sample_count = audio_stream->sample_count;
     callback_data->pitch_multiplier = pitch_multiplier;
     callback_data->pitch_multiplier = pitch_multiplier;
     callback_data->speed_multiplier = speed_multiplier;
     callback_data->speed_multiplier = speed_multiplier;
     callback_data->should_loop_audio = should_loop_audio;
     callback_data->should_loop_audio = should_loop_audio;
+    callback_data->end_function = end_func;
+    callback_data->custom_pointer_for_end_function = custom_pointer_for_end_function;
 
 
     if (!player_is_initialized) YZ_init_player();
     if (!player_is_initialized) YZ_init_player();
 
 
@@ -350,10 +358,10 @@ pa_callback_data* YZ_play_stream(YZ_audio_stream* audio_stream){
     double value = 1;
     double value = 1;
     unsigned char antivalue = 0;
     unsigned char antivalue = 0;
 
 
-    return play_stream(audio_stream, &value, &value, &antivalue);
+    return play_stream(audio_stream, &value, &value, &antivalue, (double)antivalue, NULL, NULL);
 }
 }
 
 
 // play an audio stream and modify pitch and speed dynamically. Pitch and speed changes work in real time due to it being pointers to the values
 // play an audio stream and modify pitch and speed dynamically. Pitch and speed changes work in real time due to it being pointers to the values
-pa_callback_data* YZ_play_stream_dynamic(YZ_audio_stream* audio_stream, double* speed_multiplier, unsigned char* should_loop_audio){
-    return play_stream(audio_stream, NULL, speed_multiplier, should_loop_audio);
+pa_callback_data* YZ_play_stream_dynamic(YZ_audio_stream* audio_stream, double* speed_multiplier, unsigned char* should_loop_audio, double starting_second, void (*end_func)(void*), void* custom_pointer_for_end_function){
+    return play_stream(audio_stream, NULL, speed_multiplier, should_loop_audio, starting_second, end_func, custom_pointer_for_end_function);
 }
 }