FreeCalypso > hg > freecalypso-reveng
annotate compal/melody-extr/extr-onemel.c @ 392:35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 01 Apr 2022 06:03:47 +0000 |
parents | |
children |
rev | line source |
---|---|
392
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This program is our attempt at extracting built-in melodies |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * from Mot C1xx firmware images. |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdio.h> |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdlib.h> |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 FILE *binf; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 int freq_offset; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 unsigned time_factor; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 char *note_names[48] = { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 "f4","fs4","g4","gs4","a4","as4","b4", "c5","cs5","d5","ds5","e5", |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 "f5","fs5","g5","gs5","a5","as5","b5", "c6","cs6","d6","ds6","e6", |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 "f6","fs6","g6","gs6","a6","as6","b6", "c7","cs7","d7","ds7","e7", |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 "f7","fs7","g7","gs7","a7","as7","b7", "c8","cs8","d8","ds8","e8" |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 }; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 open_and_seek_input(binfname, offset_arg) |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 char *binfname, *offset_arg; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 u_long offset; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 binf = fopen(binfname, "r"); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (!binf) { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 perror(binfname); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 exit(1); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 offset = strtoul(offset_arg, 0, 0); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 fseek(binf, offset, SEEK_SET); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 } |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 process_record() |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 u_char record[4]; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 int note, vol_adj; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 unsigned duration; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 if (fread(&record, sizeof record, 1, binf) != 1) { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 fprintf(stderr, "error reading from binary file\n"); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 exit(1); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 } |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 duration = record[0] * time_factor + 1; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 if (record[2]) { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 note = record[2] + freq_offset; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 if (note < 1 || note > 48) { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 fprintf(stderr, "error: note is out of range\n"); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 exit(1); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 } |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 note--; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 printf("%s\t64\t%u", note_names[note], duration); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 vol_adj = record[1]; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (vol_adj) { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 if (vol_adj >= 128) |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 vol_adj -= 256; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 printf("\t# Vol %+d", vol_adj); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 } |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 fputs("\nrest\t\t2\n", stdout); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } else { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 printf("rest\t\t%u\n", duration); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 } |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 } |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 main(argc, argv) |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 char **argv; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 unsigned n, num_entries; |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 if (argc != 6) { |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 fprintf(stderr, |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 "usage: %s fw-image-file file-offset num-entries freq-off time-mult\n", |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 argv[0]); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 exit(1); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 } |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 open_and_seek_input(argv[1], argv[2]); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 num_entries = strtoul(argv[3], 0, 0); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 freq_offset = atoi(argv[4]); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 time_factor = atoi(argv[5]); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 for (n = 0; n < num_entries; n++) |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 process_record(); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 exit(0); |
35009c936a4a
compal/melody-extr: first attempt at actual melody extraction
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 } |