FreeCalypso > hg > sipout-test-utils
annotate tone-detect/g711-tone-detect.c @ 16:4f81b959a5f5
sipout-test-voice: implement PCMU GSM uplink catcher
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 13 May 2024 22:10:25 -0800 |
parents | ff535725e01f |
children |
rev | line source |
---|---|
9
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This program reads a G.711 sample stream recording (typically extracted |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * from an RTP stream pcap with rtp-g711-extr by the same author) and feeds |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * it to SpanDSP modem connection tone detector. |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 */ |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdio.h> |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdint.h> |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdlib.h> |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <string.h> |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <strings.h> |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <spandsp.h> |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include "../include/pstn_defs.h" |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 static char *data_filename; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 static int use_alaw, tone_type_sought; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 static g711_state_t *g711_dec_state; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 static modem_connect_tones_rx_state_t *tones_rx_state; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 static unsigned frame_count; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 static void report_func(void *user_data, int code, int level, int delay) |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 { |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 printf("@%u.%03us: %s\n", frame_count / 50, (frame_count % 50) * 20, |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 modem_connect_tone_to_str(code)); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 } |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 main(argc, argv) |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 char **argv; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 { |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 FILE *inf; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 uint8_t g711_buf[FRAME_20MS]; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 int16_t linear[FRAME_20MS]; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 int cc; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 switch (argc) { |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 case 3: |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 data_filename = argv[1]; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 tone_type_sought = atoi(argv[2]); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 break; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 case 4: |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 data_filename = argv[1]; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 if (strcmp(argv[2], "alaw")) |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 goto usage; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 use_alaw = 1; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 tone_type_sought = atoi(argv[3]); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 break; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 default: |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 usage: |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 fprintf(stderr, |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 "usage: %s data-filename [alaw] tone-type-code\n", |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 argv[0]); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 exit(1); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 inf = fopen(data_filename, "r"); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (!inf) { |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 perror(data_filename); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 exit(1); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 g711_dec_state = g711_init(NULL, use_alaw ? G711_ALAW : G711_ULAW); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 if (!g711_dec_state) { |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 fprintf(stderr, "error: g711_init() failed!\n"); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 exit(1); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 } |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 tones_rx_state = modem_connect_tones_rx_init(NULL, tone_type_sought, |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 report_func, NULL); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 if (!tones_rx_state) { |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 fprintf(stderr, |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 "error: modem_connect_tones_rx_init() failed!\n"); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 exit(1); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 } |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 for (frame_count = 0; ; frame_count++) { |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 cc = fread(g711_buf, 1, FRAME_20MS, inf); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 if (cc == 0) |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 break; |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 g711_decode(g711_dec_state, linear, g711_buf, cc); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 modem_connect_tones_rx(tones_rx_state, linear, cc); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 } |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 exit(0); |
ff535725e01f
g711-tone-detect program written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 } |