Created
March 24, 2025 03:05
-
-
Save x42/8c4b3cb8abaa33586851b5367a0e2a65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* firstNZsample.c - print time of first non-zero audio sample | |
* | |
* (C) 2009 Robin Gareus <[email protected]> | |
* GPLv2 https://www.gnu.org/licenses/gpl-2.0.html | |
* | |
* compile with: | |
* gcc -o firstNZsample firstNZsample.c -Wall -lsndfile | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sndfile.h> | |
int main (int argc, char **argv) | |
{ | |
int chn = -1; | |
char* filename; | |
SF_INFO nfo; | |
SNDFILE* sndfile; | |
if (argc < 2) { | |
fprintf(stderr, "Usage: %s <audio-file> [chn]\n\n", argv[0]); | |
fprintf(stderr, "This tool prints the time of the first non-zero audio-sample.\n\n"); | |
return -1; | |
} | |
filename = argv[1]; | |
if (argc > 2) { | |
chn = atoi (argv[2]) - 1; | |
} | |
memset (&nfo, 0, sizeof(SF_INFO)); | |
if ((sndfile = sf_open (filename, SFM_READ, &nfo)) == 0) { | |
return -1; | |
} | |
if (!sndfile) { | |
return -1; | |
} | |
float* buf = (float*) malloc (nfo.channels * sizeof(float)); | |
if (!buf) return -1; | |
for (int s = 0; s < nfo.frames; ++s) { | |
sf_readf_float (sndfile, buf, 1); | |
for (int c = 0; c < nfo.channels; ++c) { | |
if (chn >= 0 && c != chn) { | |
continue; | |
} | |
if (buf[c] != 0) { | |
printf ("First non-zero sample: %d (chn:%d)\n", s, c + 1); | |
goto end; | |
} | |
} | |
} | |
printf ("All samples are zero.\n"); | |
end: | |
sf_close(sndfile); | |
free(buf); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment