1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include "general.h"
6 #include "fileoutput.h"
7 
8 /*
9 
10 Ringtone Tools - Copyright 2001-2005 Michael Kohn (mike@mikekohn.net)
11 This falls under the Kohnian license.  Please read
12 it at http://ringtonetools.mikekohn.net/
13 
14 This program is NOT opensourced.  You may not use any part
15 of this program for your own software.
16 
17 */
18 
write_nokia_header(FILE * out,struct note_t * note)19 void write_nokia_header(FILE *out, struct note_t *note)
20 {
21 int l,t;
22 
23   bitptr=7;
24 
25   note->bytes=0;
26 
27   push(2,8);   /* ring tone commands */
28   push(74,8);
29   push(29,7);
30 
31   l=strlen(note->songname);
32   push(1,3);   /* song type */
33   push(l,4);   /* length of song name */
34 
35   for (t=0; t<l; t++)
36   { push(note->songname[t],8); }
37 
38   push(1,8);   /* 1 song pattern */
39   push(0,3);   /* pattern header id */
40   push(0,2);   /* A-part */
41 
42   push(note->flats,4); /* looping  */
43 
44 #ifdef DEBUG
45   printf("stackptr=%d   bitptr=%d\n",stackptr,bitptr);
46 #endif
47 
48   note->marker=stackptr;
49   note->mcc=bitptr;
50   push(0,8);
51 
52   l=get_tempo(note->bpm);
53   t=reverse_tempo(l);
54 
55   if (t!=note->bpm)
56   {
57     if (quiet==0)
58     { printf("Warning:  Tempo has been adjusted to %d bpm due to Nokia limitations\n",t); }
59   }
60   push(4,3);       /* set tempo */
61   push(l,5);
62   note->bytes++;
63 
64   push(3,3);       /* set style */
65   push(note->style,2);
66   note->bytes++;
67 
68   push(5,3);       /* set volume */
69   push(note->volume,4);
70   note->bytes++;
71 }
72 
write_nokia_note(FILE * out,struct note_t * note)73 void write_nokia_note(FILE *out, struct note_t *note)
74 {
75   note->scale+=note->transpose;
76   if (note->scale<0) note->scale=0;
77 
78   if (note->prev_scale!=note->scale)
79   {
80     note->prev_scale=note->scale;
81     push(2,3);
82     push(note->scale+1,2);
83     note->bytes++;
84   }
85 
86   push(1,3);
87   push(note->tone,4);
88   push(note->length,3);
89   push(note->modifier,2);
90   note->bytes++;
91 }
92 
write_nokia_style(FILE * out,struct note_t * note)93 void write_nokia_style(FILE *out, struct note_t *note)
94 {
95   push(3,3);       /* set style */
96   push(note->style,2);
97   note->bytes++;
98 }
99 
write_nokia_footer(FILE * out,struct note_t * note)100 void write_nokia_footer(FILE *out, struct note_t *note)
101 {
102   if (bitptr!=7) stackptr++;
103   push(0,8);
104 
105 #ifdef DEBUG
106   printf("commands: %d    a0=%d   b0=%d\n",note->bytes,note->marker,note->mcc);
107 #endif
108 
109   push_addr(note->bytes,8,note->marker,note->mcc);
110   write_codes(out,"1581");
111 }
112 
113 
write_nokia_logo_header(FILE * out,struct note_t * note)114 void write_nokia_logo_header(FILE *out, struct note_t *note)
115 {
116 int t,k,r;
117 
118   stackptr=0;
119   bitptr=7;
120 
121   push(0x30,8);   /* Identifier for version */
122 
123   if (note->mcc!=-1 && note->mnc!=-1)
124   {
125     t=note->mcc%10;
126     k=(note->mcc/10)%10;
127     r=(note->mcc/100)%10;
128     push((k<<4)+r,8);   /* MCC Mobile Country Code */
129     push(0xf0+t,8);     /* MCC Mobile Country Code */
130     t=note->mnc%10;
131     k=(note->mnc/10)%10;
132     push((t<<4)+k,8);   /* MNC Mobile Network Code */
133     push(0x0A,8);       /* Line feed */
134   }
135     else
136   if ((note->bmp_flags&2)==2)
137   {
138     push(0x06,8);
139     k=((note->height*note->width)/8)+4;
140     if (((note->height*note->width)%8)!=0) k++;
141     push((k>>8),8);
142     push((k&255),8);
143   }
144     else
145   if (note->message!=0)
146   {
147     push(0x00,8);
148     k=strlen(note->message);
149     push((k>>8),8);
150     push((k&255),8);
151 
152     for(t=0; t<k; t++)
153     { push(note->message[t],8); }
154 
155     push(0x02,8);
156     k=((note->height*note->width)/8)+4;
157     if (((note->height*note->width)%8)!=0) k++;
158     push((k>>8),8);
159     push((k&255),8);
160   }
161 
162   push(0x00,8);          /* Info Field? */
163   push(note->width,8);   /* height and width DUH */
164   push(note->height,8);
165   push(0x01,8);          /* picture depth */
166 }
167 
write_nokia_logo_footer(FILE * out,struct note_t * note)168 void write_nokia_logo_footer(FILE *out, struct note_t *note)
169 {
170 int x,y,c,ptr;
171 
172   ptr=0;
173   for (y=0; y<note->height; y++)
174   {
175     for (x=0; x<note->width; x++)
176     {
177       c=note->picture[ptr++];
178       c=((c&255)+((c>>8)&255)+((c>>16)&255))/3;
179       if (c>COLOR_THRESHOLD)
180       { push(1,1); }
181         else
182       { push(0,1); }
183     }
184   }
185 
186   stackptr++;
187 
188   if (note->mcc!=-1 && note->mnc!=-1)
189   { write_codes(out,"1582"); }
190     else
191   if ((note->bmp_flags&2)==2)
192   { write_codes(out,"158A"); }
193     else
194   { write_codes(out,"1583"); }
195 }
196 
write_nokia_bpm(FILE * out,struct note_t * note)197 void write_nokia_bpm(FILE *out, struct note_t *note)
198 {
199 int t;
200 
201   t=get_tempo(note->bpm);
202   push(4,3);
203   push(t,5);
204   note->bytes++;
205 }
206 
write_nokia_volume(FILE * out,struct note_t * note)207 void write_nokia_volume(FILE *out, struct note_t *note)
208 {
209   push(5,3);       /* set volume */
210   push(note->volume,4);
211   note->bytes++;
212 }
213 
write_ngg_logo_header(FILE * out,struct note_t * note)214 void write_ngg_logo_header(FILE *out, struct note_t *note)
215 {
216   if (note->ems==0)
217   {
218     fprintf(out,"NOL%c%c%c",0,1,0);
219     write_word(out,note->mcc);
220     write_word(out,note->mnc);
221   }
222     else
223   {
224     fprintf(out,"NGG%c%c%c",0,1,0);
225   }
226 
227   write_word(out,note->width);
228   write_word(out,note->height);
229   write_word(out,1);
230   write_word(out,1);
231   write_word(out,0x53);
232 }
233 
write_ngg_logo_footer(FILE * out,struct note_t * note)234 void write_ngg_logo_footer(FILE *out, struct note_t *note)
235 {
236 int x,y,c,ptr;
237 
238   ptr=0;
239   for (y=0; y<note->height; y++)
240   {
241     for (x=0; x<note->width; x++)
242     {
243       c=note->picture[ptr++];
244       c=((c&255)+((c>>8)&255)+((c>>16)&255))/3;
245       if (c>COLOR_THRESHOLD)
246       { putc('1',out); }
247         else
248       { putc('0',out); }
249     }
250   }
251 
252   fprintf(out,"Created by Michael Kohn's Ringtone Tools - http://ringtonetools.mikekohn.net/");
253 }
254 
255 
256