Add --no-loop option to stop audio at end of file

This commit is contained in:
xbelanch 2025-08-21 08:44:08 +01:00
parent dc5d94be02
commit baed8484bb
3 changed files with 22 additions and 14 deletions

View File

@ -159,7 +159,7 @@ int fm_mpx_open(char *filename, size_t len) {
// samples provided by this function are in 0..10: they need to be divided by
// 10 after.
int fm_mpx_get_samples(float *mpx_buffer) {
int fm_mpx_get_samples(float *mpx_buffer, uint8_t no_loop) {
get_rds_samples(mpx_buffer, length);
if(inf == NULL) return 0; // if there is no audio, stop here
@ -176,10 +176,15 @@ int fm_mpx_get_samples(float *mpx_buffer) {
return -1;
}
if(audio_len == 0) {
if( sf_seek(inf, 0, SEEK_SET) < 0 ) {
fprintf(stderr, "Could not rewind in audio file, terminating\n");
return -1;
}
if (no_loop) {
fprintf(stderr, "End of file reached, stopping.\n");
return -1;
} else {
if( sf_seek(inf, 0, SEEK_SET) < 0 ) {
fprintf(stderr, "Could not rewind in audio file, terminating\n");
return -1;
}
}
} else {
break;
}
@ -263,4 +268,4 @@ int fm_mpx_close() {
if(audio_buffer != NULL) free(audio_buffer);
return 0;
}
}

View File

@ -22,5 +22,5 @@
*/
extern int fm_mpx_open(char *filename, size_t len);
extern int fm_mpx_get_samples(float *mpx_buffer);
extern int fm_mpx_close();
extern int fm_mpx_get_samples(float *mpx_buffer, uint8_t no_loop);
extern int fm_mpx_close();

View File

@ -144,7 +144,6 @@
#define BCM2708_DMA_END (1<<1)
#define BCM2708_DMA_RESET (1<<31)
#define BCM2708_DMA_INT (1<<2)
#define DMA_CS (0x00/4)
#define DMA_CONBLK_AD (0x04/4)
#define DMA_DEBUG (0x20/4)
@ -318,7 +317,7 @@ map_peripheral(uint32_t base, uint32_t len)
#define DATA_SIZE 5000
int tx(uint32_t carrier_freq, char *audio_file, uint16_t pi, char *ps, char *rt, float ppm, char *control_pipe) {
int tx(uint32_t carrier_freq, char *audio_file, uint16_t pi, char *ps, char *rt, float ppm, char *control_pipe, uint8_t no_loop) {
// Catch all signals possible - it is vital we kill the DMA engine
// on process exit!
for (int i = 0; i < 64; i++) {
@ -519,7 +518,7 @@ int tx(uint32_t carrier_freq, char *audio_file, uint16_t pi, char *ps, char *rt,
while (free_slots >= SUBSIZE) {
// get more baseband samples if necessary
if(data_len == 0) {
if( fm_mpx_get_samples(data) < 0 ) {
if( fm_mpx_get_samples(data, no_loop) < 0 ) {
terminate(0);
}
data_len = DATA_SIZE;
@ -555,6 +554,7 @@ int main(int argc, char **argv) {
char *rt = "PiFmRds: live FM-RDS transmission from the RaspberryPi";
uint16_t pi = 0x1234;
float ppm = 0;
uint8_t no_loop = 0;
// Parse command-line arguments
@ -587,10 +587,13 @@ int main(int argc, char **argv) {
} else if(strcmp("-ctl", arg)==0 && param != NULL) {
i++;
control_pipe = param;
} else {
} else if(strcmp("-no-loop", arg)==0 && param == NULL) {
i++;
no_loop = 1;
} else {
fatal("Unrecognised argument: %s.\n"
"Syntax: pi_fm_rds [-freq freq] [-audio file] [-ppm ppm_error] [-pi pi_code]\n"
" [-ps ps_text] [-rt rt_text] [-ctl control_pipe]\n", arg);
" [-ps ps_text] [-rt rt_text] [-ctl control_pipe] [-no-loop]\n", arg);
}
}
@ -599,7 +602,7 @@ int main(int argc, char **argv) {
char* locale = setlocale(LC_ALL, "");
printf("Locale set to %s.\n", locale);
int errcode = tx(carrier_freq, audio_file, pi, ps, rt, ppm, control_pipe);
int errcode = tx(carrier_freq, audio_file, pi, ps, rt, ppm, control_pipe, no_loop);
terminate(errcode);
}