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