From b9d11daf34b032ae88a606d6c3d6d196a72ee37c Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Sat, 8 Mar 2025 22:04:39 +0100 Subject: [PATCH] some changes, again --- src/chimer95.c | 78 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/src/chimer95.c b/src/chimer95.c index 1d32955..368305a 100644 --- a/src/chimer95.c +++ b/src/chimer95.c @@ -34,6 +34,7 @@ volatile sig_atomic_t to_run = 1; volatile sig_atomic_t playing_sequence = 0; volatile int sequence_position = 0; volatile int sequence_type = 0; // 0 = none, 1 = 29:56, 2 = 59:55 +volatile time_t last_sequence_time = 0; // Track when we last played a sequence static void stop(int signum) { (void)signum; @@ -44,19 +45,20 @@ static void stop(int signum) { void show_version() { printf("chimer95 (gts time signal encoder by radio95) version 1.0\n"); } + void show_help(char *name) { printf( "Usage: %s\n" " -o,--output Override output device [default: %s]\n" - " -F,--frequency GTS Frequency [default: %.1f]\n" + " -F,--frequency GTS Frequency [default: %.1f Hz]\n" " -s,--samplerate Output Samplerate [default: %d]\n" " -v,--volume Output volume [default: %.2f]\n" - " -o,--offset GTS Offset [default: %d]\n" + " -t,--offset GTS Offset [default: %d s]\n" ,name ,OUTPUT_DEVICE ,FREQ - ,(int)SAMPLE_RATE - ,MASTER_VOLUME, + ,SAMPLE_RATE + ,MASTER_VOLUME ,OFFSET ); } @@ -68,48 +70,56 @@ int main(int argc, char **argv) { char audio_output_device[64] = OUTPUT_DEVICE; float master_volume = MASTER_VOLUME; float freq = FREQ; - int sample_rate = (int)SAMPLE_RATE; + int sample_rate = SAMPLE_RATE; int offset = OFFSET; // #region Parse Arguments int opt; - const char *short_opt = "o:F:s:v:o:h"; - struct option long_opt[] = - { - {"output", required_argument, NULL, 'o'}, - {"frequency", required_argument, NULL, 'F'}, - {"samplerate", required_argument, NULL, 's'}, - {"volume", required_argument, NULL, 'v'}, - {"offset", required_argument, NULL, 'o'}, + const char *short_opt = "o:F:s:v:t:h"; + struct option long_opt[] = + { + {"output", required_argument, NULL, 'o'}, + {"frequency", required_argument, NULL, 'F'}, + {"samplerate", required_argument, NULL, 's'}, + {"volume", required_argument, NULL, 'v'}, + {"offset", required_argument, NULL, 't'}, // Changed from 'o' to 't' to avoid duplicate - {"help", no_argument, NULL, 'h'}, - {0, 0, 0, 0} - }; + {"help", no_argument, NULL, 'h'}, + {0, 0, 0, 0} + }; while((opt = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) { switch(opt) { case 'o': // Output Device memcpy(audio_output_device, optarg, 63); + audio_output_device[63] = '\0'; // Ensure null-termination break; case 'F': // Frequency freq = strtof(optarg, NULL); break; - case 's': //Sample rate + case 's': // Sample rate sample_rate = strtol(optarg, NULL, 10); break; case 'v': // Volume master_volume = strtof(optarg, NULL); break; - case 'o': // Offset + case 't': // Offset (changed from 'o' to 't') offset = strtol(optarg, NULL, 10); break; case 'h': show_help(argv[0]); - return 1; + return 0; // Return 0 for help, not 1 } } // #endregion + printf("Configuration:\n"); + printf(" Output device: %s\n", audio_output_device); + printf(" Frequency: %.1f Hz\n", freq); + printf(" Sample rate: %d Hz\n", sample_rate); + printf(" Volume: %.2f\n", master_volume); + printf(" Time offset: %d seconds\n", offset); + // #region Setup devices // Define formats and buffer atributes @@ -121,12 +131,12 @@ int main(int argc, char **argv) { pa_buffer_attr input_buffer_atr = { .maxlength = buffer_maxlength, - .fragsize = buffer_tlength_fragsize + .fragsize = buffer_tlength_fragsize }; pa_buffer_attr output_buffer_atr = { .maxlength = buffer_maxlength, .tlength = buffer_tlength_fragsize, - .prebuf = buffer_prebuf + .prebuf = buffer_prebuf }; int opentime_pulse_error; @@ -164,6 +174,7 @@ int main(int argc, char **argv) { // Parameters for the time signals int elapsed_samples = 0; int total_sequence_samples = 0; + int sequence_completed = 0; // Flag to track if we've already reported completion // For 29:56 - Play pip ... pip ... pip ... pip ... beep (4.5 seconds total) // Each pip is 0.1s with 0.9s pause, and beep is 0.5s @@ -180,6 +191,9 @@ int main(int argc, char **argv) { int pause_samples = (int)((PIP_PAUSE / 1000.0) * sample_rate); int beep_samples = (int)((BEEP_DURATION / 1000.0) * sample_rate); + printf("Ready to play time signals.\n"); + printf("Will trigger at XX:29:%02d and XX:59:%02d\n", 56+offset, 55+offset); + while (to_run) { // Clear the output buffer memset(output, 0, sizeof(output)); @@ -190,18 +204,24 @@ int main(int argc, char **argv) { int second = utc_time->tm_sec; // Check if we need to start a time signal sequence - if (minute == 29 && second == 56+offset && !playing_sequence) { + // Only start a new sequence if we're not already playing one and + // if we haven't played this exact sequence already (using the timestamp check) + if (minute == 29 && second == (56+offset) && !playing_sequence && difftime(now, last_sequence_time) >= 1.0) { printf("Starting 29:56 time signal sequence\n"); playing_sequence = 1; sequence_type = 1; // 29:56 pattern elapsed_samples = 0; total_sequence_samples = samples_29_56; - } else if (minute == 59 && second == 55+offset && !playing_sequence) { + sequence_completed = 0; + last_sequence_time = now; + } else if (minute == 59 && second == (55+offset) && !playing_sequence && difftime(now, last_sequence_time) >= 1.0) { printf("Starting 59:55 time signal sequence\n"); playing_sequence = 1; sequence_type = 2; // 59:55 pattern elapsed_samples = 0; total_sequence_samples = samples_59_55; + sequence_completed = 0; + last_sequence_time = now; } // If we're playing a sequence, generate the appropriate sounds @@ -209,9 +229,12 @@ int main(int argc, char **argv) { for (int i = 0; i < BUFFER_SIZE; i++) { if (elapsed_samples >= total_sequence_samples) { // End of sequence + if (!sequence_completed) { + printf("Time signal sequence completed\n"); + sequence_completed = 1; + } playing_sequence = 0; output[i] = 0; - printf("Time signal sequence completed\n"); } else { // Determine if we should be playing a pip, beep, or silence if (sequence_type == 1) { // 29:56 pattern: pip ... pip ... pip ... pip ... beep @@ -266,7 +289,14 @@ int main(int argc, char **argv) { to_run = 0; break; } + + // Small delay to prevent CPU hogging when idle + if (!playing_sequence) { + struct timespec ts = {0, 10000000}; // 10ms pause when not playing + nanosleep(&ts, NULL); + } } + printf("Cleaning up...\n"); pa_simple_free(output_device); return 0;