1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <time.h>
6 
7 #include "general.h"
8 #include "fileoutput.h"
9 
10 #define RECORD_SIZE 60000
11 
12 /*
13 
14 Ringtone Tools - Copyright 2001-2005 Michael Kohn (mike@mikekohn.net)
15 This falls under the Kohnian license.  Please read
16 it at http://ringtonetools.mikekohn.net/
17 
18 This program is NOT opensourced.  You may not use any part
19 of this program for your own software.
20 
21 */
22 
wav2pdb(FILE * in,FILE * out,struct note_t * note)23 int wav2pdb(FILE *in, FILE *out, struct note_t *note)
24 {
25 char name[32],riff_type[5];
26 int ch,ch1,t,l,r;
27 int channels,sample_rate,bytes_per_sample;
28 int data_length,records;
29 
30   read_chars(in,name,4);
31   l=read_long(in);
32   read_chars(in,riff_type,4);
33 
34   if (strcasecmp(name,"RIFF")!=0 || strcasecmp(riff_type,"wave")!=0)
35   {
36     printf("Invalid WAV file: %s\n",note->filename);
37     return 1;
38   }
39 
40   if (note->songname[0]==0)
41   { strcpy(name,"Ringtone Tools"); }
42     else
43   { strncpy(name,note->songname,32); }
44 
45   l=strlen(name);
46   if (l>31) l=31;
47 
48   for (t=0; t<l; t++)
49   { putc(name[t],out); }
50   for (r=t; r<32; r++)
51   { putc(0,out); }
52 
53   write_word_b(out,0x18);
54   write_word_b(out,0x1);
55 
56   write_long_b(out,time(NULL));
57   write_long_b(out,time(NULL));
58 
59   write_long_b(out,0);
60 
61   write_long_b(out,0);
62   write_long_b(out,0);
63   write_long_b(out,0);
64 
65   putc('r',out);
66   putc('i',out);
67   putc('n',out);
68   putc('g',out);
69 
70   putc('Q',out);
71   putc('C',out);
72   putc('B',out);
73   putc('A',out);
74 
75   write_long_b(out,0);
76   write_long_b(out,0);
77 
78   read_chars(in,name,4);
79   read_long(in);
80   read_word(in);
81   channels=read_word(in);
82   sample_rate=read_long(in);
83   read_long(in);
84   bytes_per_sample=read_word(in);
85   read_word(in);
86 
87   read_chars(in,name,4);
88   data_length=read_long(in);
89   data_length=data_length+6;
90 
91   r=ftell(in);
92   fseek(in,0,SEEK_END);
93   l=ftell(in);
94   fseek(in,r,SEEK_SET);
95 
96   if ((l-r)<data_length) data_length=(l-r);
97 
98   l=data_length/bytes_per_sample;
99 
100   records=(l+12)/RECORD_SIZE;
101 
102   if ((l%RECORD_SIZE)!=0) records++;
103 
104   r=78+(records*8);
105 
106   write_word_b(out,records);
107 
108   for (t=0; t<records; t++)
109   {
110     write_long_b(out,r);
111     putc(0,out);
112     putc(0,out);
113     putc(0,out);
114     putc(0,out);
115     r=r+RECORD_SIZE;
116   }
117 
118   if (channels==2)
119   {
120     if (quiet==0) printf("Warning:  This WAV is stereo.  Mixing channels.\n");
121   }
122 
123   if ((bytes_per_sample==2 && channels==2) || bytes_per_sample==4)
124   {
125     if (quiet==0) printf("Warning:  This WAV is 16 bit.  Downsampling to 8 bit.\n");
126   }
127 
128   if (sample_rate!=11025 && sample_rate!=16000)
129   {
130     if (quiet==0)
131     {
132       printf("\nWarning: This WAV is sampled at %d Hz.  The Kyocera 6035 plays back\n",sample_rate);
133       printf("either 11025Hz or 16000Hz.  Your ringtone could sound awkward.\n");
134       printf("You should resample this WAV with a program such as SOX\n");
135       printf("before using Ringtonetools on it.\n\n");
136     }
137   }
138 
139   write_long_b(out,1);
140   write_word_b(out,records-1);
141 
142   write_word_b(out,l);
143   write_word_b(out,0);
144   write_word_b(out,sample_rate);
145 
146   for (t=0; t<l; t++)
147   {
148     if (bytes_per_sample==1)
149     {
150       ch=getc(in);
151       putc(ch,out);
152     }
153       else
154     if (bytes_per_sample==2 && channels==2)
155     {
156       ch=getc(in)+getc(in);
157       ch=ch/2;
158       putc(ch,out);
159     }
160       else
161     if (bytes_per_sample==2 && channels==1)
162     {
163       ch=read_word(in);
164       ch=(int)((float)ch*(float)((float)255/(float)65535));
165       putc(ch,out);
166     }
167       else
168     if (bytes_per_sample==4 && channels==2)
169     {
170       ch=read_word(in);
171       ch=(int)((float)ch*(float)((float)255/(float)65535));
172 
173       ch1=read_word(in);
174       ch1=(int)((float)ch1*(float)((float)255/(float)65535));
175 
176       ch=(ch+ch1)/2;
177 
178       putc(ch,out);
179     }
180       else
181    { printf("unknown format\n"); return -1; }
182   }
183 
184   return 0;
185 }
186 
187 
188 
189