1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 
6 #include "general.h"
7 #include "fileoutput.h"
8 
9 #define MAX_EMS_LEN 2048
10 
11 /*
12 
13 Ringtone Tools - Copyright 2001-2005 Michael Kohn (mike@mikekohn.net)
14 This falls under the Kohnian license.  Please read
15 it at http://ringtonetools.mikekohn.net/
16 
17 This program is NOT opensourced.  You may not use any part
18 of this program for your own software.
19 
20 */
21 
parse_emelody(FILE * in,FILE * out,int out_type,struct note_t * note)22 int parse_emelody(FILE *in, FILE *out, int out_type, struct note_t *note)
23 {
24 char song[MAX_EMS_LEN];
25 int t,l,ptr,ch;
26 int scale;
27 int length=0,tone=0,octave,modifier;
28 int tone_modifier;
29 int default_octave;
30 
31   scale=-1;
32   default_octave=0;
33   note->bpm=120;
34 
35   check_songname(in,note->songname);
36 
37   ptr=0;
38   while ((ch=getc(in))!=EOF)
39   {
40     song[ptr++]=ch;
41     if (ptr>MAX_EMS_LEN-8) break;
42   }
43 
44   song[ptr]=0;
45 
46 #ifdef DEBUG
47   printf("\n       Song Name: %s\n",note->songname);
48   printf("Beats Per Minute: %d\n",note->bpm);
49 #endif
50 
51   header_route(out,note,8,out_type);
52 
53   for (t=0; t<ptr; t++)
54   {
55     if (song[t]=='r') song[t]='p';
56     if (song[t]=='R') song[t]='P';
57   }
58 
59   l=0;
60   while (l<ptr)
61   {
62     if (song[l]==' ' || song[l]=='\t' || song[l]=='\r' || song[l]=='\n')
63     { l++; continue; }
64 
65     if (song[l]=='@' && note->ats==0)
66     { l++; continue; }
67 
68     if (song[l]=='*')
69     {
70       l++;
71       default_octave=(song[l++]-'0')-3;
72     }
73 
74     if (song[l]=='+' && song[l+1]=='+') { octave=2+default_octave; l=l+2; }
75       else
76     if (song[l]=='+') { octave=1+default_octave; l++; }
77       else
78     { octave=default_octave; }
79 
80     tone_modifier=0;
81 
82     if (song[l]=='#')
83     {
84       tone_modifier=tone_modifier+1;
85       l++;
86     }
87 
88     if (song[l]=='&' || song[l]=='@')
89     {
90       tone_modifier=tone_modifier-1;
91       l++;
92     }
93 
94     if (song[l]=='(' && song[l+1]=='b' && song[l+2]==')')
95     {
96       tone_modifier=tone_modifier-1;
97       l=l+3;
98     }
99 
100     if (tolower(song[l])=='a') tone=10;
101       else
102     if (tolower(song[l])=='h' || tolower(song[l])=='b') tone=12;
103       else
104     if (tolower(song[l])=='c') tone=1;
105       else
106     if (tolower(song[l])=='d') tone=3;
107       else
108     if (tolower(song[l])=='e') tone=5;
109       else
110     if (tolower(song[l])=='f') tone=6;
111       else
112     if (tolower(song[l])=='g') tone=8;
113       else
114     if (tolower(song[l])=='p') tone=0;
115 
116     if (song[l]>='a' && song[l]<='z') length=3;
117       else
118     if (song[l]>='A' && song[l]<='Z') length=2;
119 
120     if (song[l]=='p')
121     {
122       if (song[l+1]=='p' && song[l+2]=='p' && song[l+3]=='p')
123       { length=1; l=l+4; }
124         else
125       if (song[l+1]=='p')
126       { length=2; l=l+2; }
127         else
128       { l++; }
129     }
130       else
131     { l++; }
132 
133     if (song[l]=='.')
134     {
135       modifier=1;
136       l++;
137     }
138       else
139     if (song[l]==':')
140     {
141       modifier=2;
142       l++;
143     }
144 
145     if (song[l]>='0' && song[l]<='3')
146     {
147       if (song[l]=='0')
148       { modifier=1; length=2; }
149         else
150       if (song[l]=='1')
151       { modifier=0; length=2; }
152         else
153       if (song[l]=='2')
154       { modifier=1; length=3; }
155         else
156       if (song[l]=='3')
157       { modifier=0; length=3; }
158 
159       l++;
160     }
161 
162     tone=tone+tone_modifier;
163 
164     if (tone>12)
165     {
166       tone=tone-12;
167       octave++;
168     }
169 
170     if (scale!=octave)
171     {
172       scale=octave;
173     }
174 
175     modifier=0;
176 
177     if (song[l]=='.')
178     {
179       modifier=1;
180       l++;
181     }
182 
183     note->tone=tone;
184     note->length=length;
185     note->modifier=modifier;
186     note->scale=scale;
187 
188     note_route(out,note,8,out_type);
189   }
190 
191   footer_route(out,note,8,out_type);
192 
193   return (0);
194 }
195 
196 
197 
198