9
9
#include "sam.h"
10
10
extern int debug ;
11
11
12
- unsigned char wait1 = 7 ;
13
- unsigned char wait2 = 6 ;
14
-
15
- extern unsigned char A , X , Y ;
16
-
17
-
18
- void AddInflection (sam_memory * sam , unsigned char mem48 , unsigned char phase1 );
12
+ void AddInflection (sam_memory * sam , unsigned char mem48 , unsigned char phase1 , unsigned char punctuation );
19
13
20
14
21
15
// contains the final soundbuffer
@@ -54,6 +48,10 @@ void Output(int index, unsigned char A)
54
48
// 174=amplitude3
55
49
unsigned char Read (sam_memory * sam , unsigned char p , unsigned char Y )
56
50
{
51
+ if (p > RENDER_FRAMES ) {
52
+ sam_error = "Out-of-buffer read" ;
53
+ return 0 ;
54
+ }
57
55
switch (p )
58
56
{
59
57
case 168 : return sam -> render .pitch [Y ];
@@ -64,12 +62,16 @@ unsigned char Read(sam_memory* sam, unsigned char p, unsigned char Y)
64
62
case 173 : return sam -> render .freq_amp [Y ].amp2 ;
65
63
case 174 : return sam -> render .freq_amp [Y ].amp3 ;
66
64
}
65
+ sam_error = "Read error" ;
67
66
return 0 ;
68
67
}
69
68
70
69
void Write (sam_memory * sam , unsigned char p , unsigned char Y , unsigned char value )
71
70
{
72
-
71
+ if (p > RENDER_FRAMES ) {
72
+ sam_error = "Out-of-buffer write" ;
73
+ return ;
74
+ }
73
75
switch (p )
74
76
{
75
77
case 168 : sam -> render .pitch [Y ] = value ; return ;
@@ -80,10 +82,10 @@ void Write(sam_memory* sam, unsigned char p, unsigned char Y, unsigned char valu
80
82
case 173 : sam -> render .freq_amp [Y ].amp2 = value ; return ;
81
83
case 174 : sam -> render .freq_amp [Y ].amp3 = value ; return ;
82
84
}
85
+ sam_error = "Write error" ;
83
86
}
84
87
85
88
86
-
87
89
// -------------------------------------------------------------------------
88
90
//Code48227
89
91
// Render a sampled sound from the sampleTable.
@@ -140,19 +142,19 @@ void Write(sam_memory* sam, unsigned char p, unsigned char Y, unsigned char valu
140
142
141
143
142
144
// Code48227()
143
- void RenderSample (sam_memory * sam , unsigned char * mem66 , unsigned sample )
145
+ unsigned char RenderSample (sam_memory * sam , unsigned char * mem66 , unsigned sample , unsigned char pos )
144
146
{
145
147
int tempA ;
146
148
// current phoneme's index
147
149
unsigned char mem47 ;
148
- unsigned char mem49 = Y ;
150
+ unsigned char mem49 = pos ;
149
151
unsigned char mem53 ;
150
152
unsigned char mem56 ;
151
153
152
154
// mask low three bits and subtract 1 get value to
153
155
// convert 0 bits on unvoiced samples.
154
- A = sample & 7 ;
155
- X = A - 1 ;
156
+ unsigned char A = sample & 7 ;
157
+ unsigned char X = A - 1 ;
156
158
157
159
// store the result
158
160
mem56 = X ;
@@ -165,6 +167,8 @@ void RenderSample(sam_memory* sam, unsigned char *mem66, unsigned sample)
165
167
// /X 4 0x17
166
168
167
169
// get value from the table
170
+ if (X >= sizeof (tab48426 ))
171
+ sam_error = "Out-of-buffer read" ;
168
172
mem53 = tab48426 [X ];
169
173
mem47 = X ; //46016+mem[56]*256
170
174
@@ -173,22 +177,22 @@ void RenderSample(sam_memory* sam, unsigned char *mem66, unsigned sample)
173
177
if (A == 0 )
174
178
{
175
179
// voiced phoneme: Z*, ZH, V*, DH
176
- Y = mem49 ;
180
+ pos = mem49 ;
177
181
A = sam -> render .pitch [9 ] >> 4 ;
178
182
179
183
// jump to voiced portion
180
184
goto pos48315 ;
181
185
}
182
186
183
- Y = A ^ 255 ;
187
+ pos = A ^ 255 ;
184
188
pos48274 :
185
189
186
190
// step through the 8 bits in the sample
187
191
mem56 = 8 ;
188
192
189
193
// get the next sample from the table
190
194
// mem47*256 = offset to start of samples
191
- A = sampleTable [mem47 * 256 + Y ];
195
+ A = sampleTable [mem47 * 256 + pos ];
192
196
pos48280 :
193
197
194
198
// left shift to get the high bit
@@ -223,12 +227,12 @@ void RenderSample(sam_memory* sam, unsigned char *mem66, unsigned sample)
223
227
if (mem56 != 0 ) goto pos48280 ;
224
228
225
229
// increment position
226
- Y ++ ;
227
- if (Y != 0 ) goto pos48274 ;
230
+ pos ++ ;
231
+ if (pos != 0 ) goto pos48274 ;
228
232
229
233
// restore values and return
230
- Y = mem49 ;
231
- return ;
234
+ pos = mem49 ;
235
+ return pos ;
232
236
233
237
234
238
unsigned char phase1 ;
@@ -239,17 +243,17 @@ void RenderSample(sam_memory* sam, unsigned char *mem66, unsigned sample)
239
243
// number of samples?
240
244
phase1 = A ^ 255 ;
241
245
242
- Y = * mem66 ;
246
+ pos = * mem66 ;
243
247
do
244
248
{
245
249
//pos48321:
246
250
247
251
// shift through all 8 bits
248
252
mem56 = 8 ;
249
- //A = Read(sam, mem47, Y );
253
+ //A = Read(sam, mem47, pos );
250
254
251
255
// fetch value from table
252
- A = sampleTable [mem47 * 256 + Y ];
256
+ A = sampleTable [mem47 * 256 + pos ];
253
257
254
258
// loop 8 times
255
259
//pos48327:
@@ -278,7 +282,7 @@ void RenderSample(sam_memory* sam, unsigned char *mem66, unsigned sample)
278
282
} while (mem56 != 0 );
279
283
280
284
// move ahead in the table
281
- Y ++ ;
285
+ pos ++ ;
282
286
283
287
// continue until counter done
284
288
phase1 ++ ;
@@ -288,9 +292,9 @@ void RenderSample(sam_memory* sam, unsigned char *mem66, unsigned sample)
288
292
289
293
// restore values and return
290
294
A = 1 ;
291
- * mem66 = Y ;
292
- Y = mem49 ;
293
- return ;
295
+ * mem66 = pos ;
296
+ pos = mem49 ;
297
+ return pos ;
294
298
}
295
299
296
300
@@ -331,8 +335,8 @@ void Render(sam_memory* sam)
331
335
int i ;
332
336
if (sam -> common .phoneme_output [0 ].index == PHONEME_END ) return ; //exit if no data
333
337
334
- A = 0 ;
335
- X = 0 ;
338
+ unsigned char A = 0 ;
339
+ unsigned char X = 0 ;
336
340
337
341
// CREATE FRAMES
338
342
//
@@ -346,7 +350,7 @@ void Render(sam_memory* sam)
346
350
do
347
351
{
348
352
// get the index
349
- Y = mem44 ;
353
+ unsigned char Y = mem44 ;
350
354
// get the phoneme at the index
351
355
A = sam -> common .phoneme_output [mem44 ].index ;
352
356
mem56 = A ;
@@ -361,7 +365,7 @@ void Render(sam_memory* sam)
361
365
A = 1 ;
362
366
mem48 = 1 ;
363
367
//goto pos48376;
364
- AddInflection (sam , mem48 , phase1 );
368
+ AddInflection (sam , mem48 , phase1 , X );
365
369
}
366
370
/*
367
371
if (A == 2) goto pos48372;
@@ -372,7 +376,7 @@ void Render(sam_memory* sam)
372
376
{
373
377
// create falling inflection
374
378
mem48 = 255 ;
375
- AddInflection (sam , mem48 , phase1 );
379
+ AddInflection (sam , mem48 , phase1 , X );
376
380
}
377
381
// pos47615:
378
382
@@ -540,7 +544,7 @@ void Render(sam_memory* sam)
540
544
while (1 ) //while No. 1
541
545
{
542
546
// get the current and following phoneme
543
- Y = sam -> common .phoneme_output [X ].index ;
547
+ unsigned char Y = sam -> common .phoneme_output [X ].index ;
544
548
A = sam -> common .phoneme_output [X + 1 ].index ;
545
549
X ++ ;
546
550
@@ -717,25 +721,21 @@ void OutputFrames(sam_memory *sam, unsigned char frame_count) {
717
721
unsigned char phase2 = 0 ;
718
722
unsigned char phase3 = 0 ;
719
723
unsigned char speedcounter = 72 ; //sam standard speed
720
- unsigned char count ;
721
- unsigned char glottal_pulse ;
722
- unsigned char sample ;
723
724
unsigned char mem66 = 0 ;
724
725
725
726
// RESCALE AMPLITUDE
726
- // Rescale volume from a linear scale to decibels .
727
+ // Rescale volume from decibels to a linear scale.
727
728
for (int i = RENDER_FRAMES - 1 ; i >=0 ; i -- )
728
729
{
729
730
sam -> render .freq_amp [i ].amp1 = amplitudeRescale [sam -> render .freq_amp [i ].amp1 ];
730
731
sam -> render .freq_amp [i ].amp2 = amplitudeRescale [sam -> render .freq_amp [i ].amp2 ];
731
732
sam -> render .freq_amp [i ].amp3 = amplitudeRescale [sam -> render .freq_amp [i ].amp3 ];
732
733
}
733
734
734
- Y = 0 ;
735
- A = sam -> render .pitch [0 ];
736
- glottal_pulse = A ;
737
- X = A ;
738
- count = A - (A >>2 ); // 3/4*A ???
735
+ unsigned char Y = 0 ;
736
+ unsigned char A = sam -> render .pitch [0 ];
737
+ unsigned char glottal_pulse = A ;
738
+ unsigned char count = A - (A >>2 ); // 3/4*A ???
739
739
740
740
if (debug )
741
741
{
@@ -757,16 +757,16 @@ void OutputFrames(sam_memory *sam, unsigned char frame_count) {
757
757
{
758
758
// get the sampled information on the phoneme
759
759
A = sam -> render .flags [Y ];
760
- sample = A ;
760
+ unsigned char sample = A ;
761
761
762
762
// unvoiced sampled phoneme?
763
763
A = A & 248 ;
764
764
if (A != 0 )
765
765
{
766
766
// render the sample for the phoneme
767
- RenderSample (sam , & mem66 , sample );
767
+ Y = RenderSample (sam , & mem66 , sample , Y );
768
768
769
- // skip ahead two in the fram ebuffer
769
+ // skip ahead two in the frame buffer
770
770
Y += 2 ;
771
771
frame_count -= 2 ;
772
772
} else
@@ -832,7 +832,7 @@ void OutputFrames(sam_memory *sam, unsigned char frame_count) {
832
832
// voiced sampled phonemes interleave the sample with the
833
833
// glottal pulse. The sample flag is non-zero, so render
834
834
// the sample for the phoneme.
835
- RenderSample (sam , & mem66 , sample );
835
+ Y = RenderSample (sam , & mem66 , sample , Y );
836
836
goto pos48159 ;
837
837
}
838
838
}
@@ -842,19 +842,16 @@ void OutputFrames(sam_memory *sam, unsigned char frame_count) {
842
842
// index X. A rising inflection is used for questions, and
843
843
// a falling inflection is used for statements.
844
844
845
- void AddInflection (sam_memory * sam , unsigned char mem48 , unsigned char phase1 )
845
+ void AddInflection (sam_memory * sam , unsigned char mem48 , unsigned char phase1 , unsigned char punctuation )
846
846
{
847
-
848
- // store the location of the punctuation
849
- unsigned char punctuation = X ;
850
- A = X ;
847
+ unsigned char A = punctuation ;
851
848
int Atemp = A ;
852
849
853
850
// backup 30 frames
854
851
A = A - 30 ;
855
852
// if index is before buffer, point to start of buffer
856
853
if (Atemp <= 30 ) A = 0 ;
857
- X = A ;
854
+ unsigned char X = A ;
858
855
859
856
// FIXME: Explain this fix better, it's not obvious
860
857
// ML : A =, fixes a problem with invalid pitch with '.'
0 commit comments