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 /*
10 
11 Ringtone Tools - Copyright 2001-2005 Michael Kohn (mike@mikekohn.net)
12 This falls under the Kohnian license.  Please read
13 it at http://ringtonetools.mikekohn.net/
14 
15 This program is NOT opensourced.  You may not use any part
16 of this program for your own software.
17 
18 */
19 
20 int get_duration(float duration);
21 
parse_kws(FILE * in,FILE * out,int out_type,struct note_t * note)22 int parse_kws(FILE *in, FILE *out, int out_type, struct note_t *note)
23 {
24 char notes[2048];
25 int duration[2048];
26 int file_length,count;
27 int t,a,b,ch;
28 int quarter_note=1;
29 
30   t=0;
31   if (note->songname[0]==0)
32   {
33     while(note->filename[t]!='.' && note->filename[t]!=0 && t<14)
34     {
35       note->songname[t]=note->filename[t];
36       t++;
37     }
38     note->songname[t]=0;
39   }
40 
41   file_length=read_long(in);
42   file_length=file_length^0xffffffff;
43   count=read_word(in);
44 
45   fseek(in,0,SEEK_END);
46 
47   if (ftell(in)!=file_length)
48   {
49     printf("This is not a KWS file.\n");
50     return -1;
51   }
52   fseek(in,6,SEEK_SET);
53 
54   if (count>2040)
55   {
56     printf("This song is too long.\n");
57     return -1;
58   }
59 
60   for (t=0; t<count; t++)
61   {
62     ch=getc(in);
63     notes[t]=ch;
64     ch=getc(in);
65     if (ch!=4) notes[t]=0;
66 
67     a=getc(in);
68     b=getc(in);
69 
70     a=a^(b<<1);
71 
72     duration[t]=(b<<8)+a;
73 
74     if (t==0) quarter_note=duration[t];
75   }
76 
77   t=0;
78   while(t<8)
79   {
80     note->bpm=60000/quarter_note;
81     if (note->bpm<250) break;
82 
83     quarter_note=quarter_note*2;
84     t++;
85   }
86 
87   t=0;
88   while(t<8)
89   {
90     if (note->bpm>90) break;
91     quarter_note=quarter_note/2;
92     note->bpm=60000/quarter_note;
93 
94     t++;
95   }
96 
97   header_route(out,note,6,out_type);
98 
99   for (t=0; t<count; t++)
100   {
101     if (notes[t]==0)
102     {
103       note->tone=0;
104     }
105       else
106     {
107       note->tone=(notes[t]-1)%12+1;
108       note->scale=(notes[t]-1)/12;
109     }
110 
111     a=get_duration((float)duration[t]/((float)quarter_note*4));
112     note->length=a/2;
113     note->modifier=(a%2)^1;
114 
115     if (t!=count-1 || note->tone!=0)
116     {
117       note_route(out,note,6,out_type);
118     }
119   }
120 
121   footer_route(out,note,6,out_type);
122 
123   return (0);
124 }
125 
126 
127 
128