|
|
View previous topic :: View next topic |
Author |
Message |
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
C and MATH confusion |
Posted: Wed Feb 05, 2014 6:52 pm |
|
|
in an example code, im trying to convert code,
from the original code
now from the above system, a would either be
a = b or c if both b is the same as c?
eg. a = 4 = 4... this would mean a is 4
eg. a = 3 = 4.. this would mean a is 0?
is this true as in CCS C, this doesn't quite work the same
or should it be
a = b = 5;
and this makes a and b the value of 5 |
|
|
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Wed Feb 05, 2014 7:11 pm |
|
|
Quote: | or should it be
a = b = 5;
and this makes a and b the value of 5 |
Yup |
|
|
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
|
Posted: Wed Feb 05, 2014 7:20 pm |
|
|
Thank you!
it was producing rubbish, so as long as i know this is working the way it should...
this helps |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9269 Location: Greensville,Ontario
|
|
Posted: Wed Feb 05, 2014 7:58 pm |
|
|
hmm..did this test...
from a PIC18F46k22 listing...
z located at 1e
y located at 1d
x located at 1c...
76: x=y=z;
033A C01E MOVFF 0x1e, 0x1d
033E C01D MOVFF 0x1d, 0x1c
77: x=z;
0342 C01E MOVFF 0x1e, 0x1c
78: y=z;
0346 C01E MOVFF 0x1e, 0x1d
so both x and y become whatever z is.
to avoid confusion you can code as 2 lines since it's the same amount of memory and time.
hth
jay |
|
|
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
|
Posted: Wed Feb 05, 2014 9:23 pm |
|
|
OH dear!!! i could have fallen in to that trap! slows this down a bit, but if CCS C seems to make mistakes like that..
i did a test too and it seemed to work, I'm about to test
unsigned int16 a;
signed int16 b;
a = b = 5;
see what happens...
I'm still getting bizarre answers.
Thanks Jay (also sorry for the late reply) |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 126 Location: Bombay, India
|
nothing special about this construct |
Posted: Wed Feb 05, 2014 10:01 pm |
|
|
a = b = c;
Has nothing to do with any specific compiler. It is a familiar C construct that is used by C-coders.
What it means is simply - reading from right to left (that's the way C parses expressions)
c 'is assigned to' b 'is assigned to' a
So,
if c contains the value 3,
b will be made 3
a will be made 3 because b now holds 3.
Hope it helps |
|
|
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
|
Posted: Wed Feb 05, 2014 11:59 pm |
|
|
i've only been coding C for about 2 years really, and the a=b=c has always worked before! lol
ok i've got some sort of really poor voice synth using the polyphonic system 3 oscillation thing and its harder and requires a TON of cycles :(
though again thank you!
i am still trying to figure out the code.
Code: |
//# DKT #//
uint16_t pitchPhase, form1Phase,form2Phase,form3Phase;
uint16_t pitchPhaseInc,form1PhaseInc,form2PhaseInc,form3PhaseInc;
uint8_t form1Amp,form2Amp,form3Amp;
uint8_t noiseMod=10;
const int8_t sinCalc[256] = {
/* This table rolls a lot of functions together for speed.
Extracting phase and amplitude from the nybble packed form
Sine calculation
Exponential amplitude mapping
Scaling to appropriate range
ROUND(
FLOOR(a/16,1)
*SIN(
2
* PI()
* IF(
MOD(a,16),
EXP(0.18*MOD(a,16)),
0
) /16
)*127
,0)
*/
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,2,2,3,3,4,5,6,7,8,10,12,14,17,20,24,
0,4,4,5,6,7,9,11,13,15,18,22,26,31,37,45,
0,5,6,7,8,10,12,14,17,20,24,28,34,41,49,58,
0,5,6,7,9,10,12,15,18,21,26,31,37,44,53,63,
0,5,6,7,8,10,12,14,17,20,24,28,34,41,49,58,
0,4,4,5,6,7,9,11,13,15,18,22,26,31,37,45,
0,2,2,3,3,4,5,6,7,8,10,12,14,17,20,24,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,-2,-2,-3,-3,-4,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,
0,-4,-4,-5,-6,-7,-9,-11,-13,-15,-18,-22,-26,-31,-37,-45,
0,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,-28,-34,-41,-49,-58,
0,-5,-6,-7,-9,-10,-12,-15,-18,-21,-26,-31,-37,-44,-53,-63,
0,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,-28,-34,-41,-49,-58,
0,-4,-4,-5,-6,-7,-9,-11,-13,-15,-18,-22,-26,-31,-37,-45,
0,-2,-2,-3,-3,-4,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24
};
const int8_t sqrCalc[256] ={
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16
};
void audioOn() {
/*
#if defined(__AVR_ATmega8__)
// ATmega8 has different registers
TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
TIMSK = _BV(TOIE2);
#elif defined(__AVR_ATmega1280__)
TCCR3A = _BV(COM3C1) | _BV(WGM30);
TCCR3B = _BV(CS30);
TIMSK3 = _BV(TOIE3);
#else
// Set up PWM to 31.25kHz, phase accurate
TCCR2A = _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(CS20);
TIMSK2 = _BV(TOIE2);
#endif
*/
//setup_timer_2(T2_DIV_BY_1,0xff,1); //22.8 us overflow, 22.8 us interrupt
setup_timer_2(T2_DIV_BY_1,255,1); //256 us overflow, 256 us interrupt
setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_timer_5(T5_DISABLED | T5_DIV_BY_1);
//setup_timer_6(T6_DIV_BY_4,126,1);
//setup_timer_6(T6_DIV_BY_4,200,1);
setup_timer_6(T6_DIV_BY_4,180,1);
setup_ccp1(CCP_PWM|CCP_SHUTDOWN_AC_L|CCP_SHUTDOWN_BD_L);
}
void setup() {
audioOn();
//setup_ccp1(CCP_OFF);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER6);
}
#define FORMANT_SZ 7
enum {
_SP,_DOT,_QM,_COM,_HYP,_IY,_IH,_EH,_AE,_AA,
_AH,_AO,_UH,_AX,_IX,_ER,_UX,_OH,_RX,_LX,
_WX,_YX,_WH,_R,_L,_W,_Y,_M,_N,_NX,
_DX,_Q,_S,_SH,_F,_TH,__H,__X,_Z,_ZH,
_V,_DH,_CHa,_CHb,_Ja,_Jb,_Jc,_Jd,_EY,_AY,
_OY,_AW,_OW,_UW,_Ba,_Bb,_Bc,_Da,_Db,_Dc,
_Ga,_Gb,_Gc,_GXa,_GXb,_GXc,_Pa,_Pb,_Pc,_Ta,
_Tb,_Tc,_Ka,_Kb,_Kc,_KXa,_KXb,_KXc
};
const uint8_t formantTable[] = {
0x0, 0x0, 0x0,0x0,0x0,0x0,0x0,/*00 space*/ 0x13,0x43,0x5b,0x0,0x0,0x0,0x0,/*01 .*/
0x13,0x43,0x5b,0x0,0x0,0x0,0x0,/*02 ?*/ 0x13,0x43,0x5b,0x0,0x0,0x0,0x0,/*03 ,*/
0x13,0x43,0x5b,0x0,0x0,0x0,0x0,/*04 -*/ 0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,0x1,0x0,0xf,/*58 Db*/ 0x6,0x42,0x79,0x0,0x0,0x0,0x0,/*59 Dc*/
0x6,0x6e,0x70,0x1,0x0,0x0,0x0,/*60 Ga*/ 0x6,0x6e,0x6e,0x4,0x1,0x0,0xf,/*61 Gb*/
0x6,0x6e,0x6e,0x0,0x0,0x0,0x0,/*62 Gc*/ 0x6,0x54,0x5e,0x1,0x0,0x0,0x0,/*63 GXa*/
0x6,0x54,0x5e,0x4,0x1,0x0,0xf,/*64 GXb*/ 0x6,0x54,0x5e,0x0,0x0,0x0,0x0,/*65 GXc*/
0x6,0x1a,0x51,0x0,0x0,0x0,0x0,/*66 Pa*/ 0x6,0x1a,0x51,0x0,0x0,0x0,0x0,/*67 Pb*/
0x6,0x1a,0x51,0x0,0x0,0x0,0x0,/*68 Pc*/ 0x6,0x42,0x79,0x0,0x0,0x0,0x0,/*69 Ta*/
0x6,0x42,0x79,0x0,0x0,0x0,0x0,/*70 Tb*/ 0x6,0x42,0x79,0x0,0x0,0x0,0x0,/*71 Tc*/
0x6,0x6d,0x65,0x0,0x0,0x0,0x0,/*72 Ka*/ 0xa,0x56,0x65,0xc,0xa,0x7,0x0,/*73 Kb*/
0xa,0x6d,0x70,0x0,0x0,0x0,0x0,/*74 Kc*/ 0x6,0x54,0x5e,0x0,0x0,0x0,0x0,/*75 KXa*/
0x6,0x54,0x5e,0x0,0xa,0x5,0x0,/*76 KXb*/ 0x6,0x54,0x5e,0x0,0x0,0x0,0x0 /*77 KXc*/
};
uint16_t pitchTable[64] = {
// Covers A1 to C7
58,61,65,69,73,77,82,86,92,97,
103,109,115,122,129,137,145,154,163,173,
183,194,206,218,231,244,259,274,291,308,
326,346,366,388,411,435,461,489,518,549,
581,616,652,691,732,776,822,871,923,978,
1036,1097,1163,1232,1305,1383,1465,1552,1644,1742,
1845,1955,2071,2195
};
const uint8_t frameList[] = {
#if 1
_Da,3,0,39,_Db,1,0,39,_Dc,1,3,39,_EY,8,6,39,_YX,20,3,39, // Dai..
_Z,10,0,36,_IY,35,3,36, // ..sy
_Da,3,0,32,_Db,1,0,32,_Dc,1,3,32,_EY,8,6,32,_YX,20,3,32, // Dai..
_Z,10,0,27,_IY,35,3,27, // ..sy
_Ga,2,0,29,_Gb,2,0,29,_Gc,2,0,29,_IH,10,3,29,_V,5,0,29, // Give
_M,2,0,31,_IY,10,3,31, // me
_YX,5,0,32,_AO,10,0,32,_RX,5,0,32, // your
_AH,25,0,29,_NX,5,0,29, // an..
_S,2,0,32,_ER,10,0,32,_RX,3,0,32, // ..swer
_Da,3,0,27,_Db,1,0,27,_Dc,1,3,27,_UX,80,3,27,_WX,5,0,27, // do
_AY,5,20,34,_YX,10,0,34,_M,8,0,34, // I'm
__H,5,0,39,_AX,30,0,39,_F,10,0,39, // half
_Ka,3,0,36,_Kb,3,0,36,_Kc,4,0,36,_R,5,0,36,_EY,30,0,36, // cra..
_Z,5,0,32,_IY,40,0,32, // ..zy
_AO,10,0,29,_LX,5,0,29, // all
_F,5,0,31,_AO,10,0,31, // for
_DH,5,0,32,_AH,10,0,32, // the
_L,5,0,34,_AH,20,0,34,_V,5,0,34,// love
_AA,10,0,36,_V,5,0,36,// of
_Y,10,0,34,_UX,80,0,34, // you
_IH,10,0,36,_Ta,2,0,36,_Tb,1,0,36,_Tc,2,0,36,// It
_W,2,0,37,_OH,10,0,37,_N,1,0,37,_Ta,1,0,37,_Tb,1,0,37,_Tc,1,0,37,// won't
_Ba,2,0,36,_Bb,1,0,36,_Bc,2,0,36,_IY,10,0,36,// be
_AH,15,0,34,// a
_S,2,0,39,_Ta,2,0,39,_Tb,2,0,39,_Tc,2,0,39,_AY,1,10,39,_YX,10,0,39,// sty..
_L,3,0,36,_IH,10,0,36,_SH,2,0,36,// ..lish
_M,5,0,34,_AE,10,0,34,// ma..
_R,5,0,32,_IH,60,0,32,_Ja,2,0,32,_Jb,2,0,32,_Jc,2,0,32,// ..rriage
_AY,5,10,34,_YX,5,0,34,// I
_Ka,2,0,36,_Kb,2,0,36,_Kc,2,0,36,_AH,20,0,36,_N,2,0,36,_Ta,2,0,36,_Tb,2,0,26,_Tc,2,0,36,// can't
_AX,15,0,32,// a..
_F,5,0,29,_AO,20,0,29,_R,2,0,29,_Da,1,0,29,_Db,1,0,29,_Dc,1,0,29,// ..fford
_AX,15,0,32,// a
_Ka,1,0,29,_Kb,1,0,29,_Kc,1,0,29,_AE,12,0,29,// ca..
_R,5,0,27,_IH,45,0,27,_Ja,2,0,27,_Jb,2,0,27,_Jc,2,0,27,// ..rriage
_Ba,1,0,27,_Bb,1,0,27,_Bc,1,0,27,_AH,10,0,27,_Ta,1,0,27,_Tb,1,0,27,_Tc,1,0,27,// but
_Y,5,0,32,_UH,10,10,32,_L,5,0,32,// you'll
_L,3,0,36,_UH,10,0,36,_Ka,1,0,36,_Kb,1,0,36,_Kc,1,0,36,// look
_S,2,0,34,_W,2,0,34,_IY,20,0,34,_Ta,2,0,34,_Tb,2,0,34,_Tc,2,0,34,// sweet
_AX,15,0,27,// a..
_Ka,2,0,32,_Kb,2,0,32,_Kc,2,0,32,_R,2,0,32,_AA,20,0,32,_S,5,0,32,// ..cross
_DH,5,0,36,_AH,10,0,36,// the
_S,2,0,34,_IY,10,0,34,_Ta,2,0,34,_Tb,2,0,34,_Tc,2,0,34,// seat
_AA,10,0,36,_V,5,0,36,// of
_AE,15,0,37,// a
_Ba,2,0,39,_Bb,2,0,39,_Bc,2,0,39,_AY,5,5,39,_YX,5,0,39,// bi..
_S,5,0,36,_IH,10,0,36,// ..cy..
_Ka,2,0,32,_Kb,2,0,32,_Kc,2,0,32,_L,9,0,32,// ..cle
_M,2,0,34,_EY,5,10,34,_YX,10,0,34,_Da,2,0,34,_Db,2,0,34,_Dc,2,0,34,// made
_F,5,0,27,_OY,1,5,27,_RX,5,0,27,// for
_Ta,2,0,32,_Tb,2,0,32,_Tc,2,0,32,_UX,50,0,32,// two
#endif
_Ta,0,0,61
};
int16 frameTime = 15; // ms
uint16_t basePitch;
int16 formantScale;
#define pgm_read_byte(x) (unsigned int8)read_program_eeprom(x)
#define delay(x) delay_ms(x/2)
void loop() {
formantScale = 54;//random(20,80);//54;
uint8_t *framePos = &frameList;
while(1) {
//uint8_t n;
uint8_t startFormant,staticFrames,tweenFrames;
int16_t startPitch,nextPitch;
uint8_t nextFormant;
int16_t startForm1PhaseInc,startForm2PhaseInc,startForm3PhaseInc;
uint8_t startForm1Amp,startForm2Amp,startForm3Amp;
uint8_t startMod;
uint8_t *formantPos;
// Read next framelist item
startFormant = pgm_read_byte(framePos++);
staticFrames = pgm_read_byte(framePos++);
if (!staticFrames) break; // End of phrase
tweenFrames = pgm_read_byte(framePos++);
startPitch = pitchTable[pgm_read_byte(framePos++)];
nextFormant = pgm_read_byte(framePos);
nextPitch = pitchTable[pgm_read_byte(framePos+3)];
pitchPhaseInc = startPitch;
formantPos = &formantTable + (startFormant * FORMANT_SZ);
form1PhaseInc = startForm1PhaseInc = (pgm_read_byte(formantPos++)*formantScale);
form2PhaseInc = startForm2PhaseInc = (pgm_read_byte(formantPos++)*formantScale);
form3PhaseInc = startForm3PhaseInc = (pgm_read_byte(formantPos++)*formantScale);
form1Amp = startForm1Amp = pgm_read_byte(formantPos++);
form2Amp = startForm2Amp = pgm_read_byte(formantPos++);
form3Amp = startForm3Amp = pgm_read_byte(formantPos++);
noiseMod = startMod = pgm_read_byte(formantPos++);
for (;staticFrames--;) delay(frameTime);
if (tweenFrames) {
uint8_t* formantPos;
int16_t deltaForm1PhaseInc,deltaForm2PhaseInc,deltaForm3PhaseInc;
int8_t deltaForm1Amp,deltaForm2Amp,deltaForm3Amp;
int8_t deltaMod;
uint8_t nextMod;
int16_t deltaPitch;
tweenFrames--;
formantPos = &formantTable + (nextFormant * FORMANT_SZ);
deltaForm1PhaseInc = (pgm_read_byte(formantPos++)*formantScale) - startForm1PhaseInc;
deltaForm2PhaseInc = (pgm_read_byte(formantPos++)*formantScale) - startForm2PhaseInc;
deltaForm3PhaseInc = (pgm_read_byte(formantPos++)*formantScale) - startForm3PhaseInc;
deltaForm1Amp = pgm_read_byte(formantPos++) - startForm1Amp;
deltaForm2Amp = pgm_read_byte(formantPos++) - startForm2Amp;
deltaForm3Amp = pgm_read_byte(formantPos++) - startForm3Amp;
deltaMod = pgm_read_byte(formantPos++) - startMod;
deltaPitch = nextPitch - startPitch;
deltaMod = nextMod - startMod;
for (uint8_t i=1; i<=tweenFrames; i++) {
form1PhaseInc = startForm1PhaseInc + ((i*deltaForm1PhaseInc)/tweenFrames);
form2PhaseInc = startForm2PhaseInc + ((i*deltaForm2PhaseInc)/tweenFrames);
form3PhaseInc = startForm3PhaseInc + ((i*deltaForm3PhaseInc)/tweenFrames);
form1Amp = startForm1Amp + ((i*deltaForm1Amp)/tweenFrames);
form2Amp = startForm2Amp + ((i*deltaForm2Amp)/tweenFrames);
form3Amp = startForm3Amp + ((i*deltaForm3Amp)/tweenFrames);
pitchPhaseInc = startPitch + ((i*deltaPitch)/tweenFrames);
noiseMod = startMod + ((i*deltaMod)/tweenFrames);
delay(frameTime);
}
}
}
delay(300);
}
//#int_timer2
#int_timer6
Void SounderNow(){
int8_t value;
static int8_t noise;
int16_t phaseNoise = noise * noiseMod;
noise += noise<<2; noise++; // noise' = 5*noise+1
form1Phase += form1PhaseInc;
value = pgm_read_byte(sinCalc+(((form1Phase>>8) & 0xf0) | form1Amp));
form2Phase += form2PhaseInc;
value += pgm_read_byte(sinCalc+(((form2Phase>>8) & 0xf0) | form2Amp));
form3Phase += form3PhaseInc;
value += pgm_read_byte(sqrCalc+(((form3Phase>>8) & 0xf0) | form3Amp));
value = (value * (0xff^(pitchPhase>>8)))>>8;
pitchPhase += pitchPhaseInc;
if ((pitchPhase+phaseNoise) < pitchPhaseInc) {
form1Phase = 0;
form2Phase = 0;
form3Phase = 0;
}
set_pwm1_duty(value);
}
|
it sounds bad though :(
its forming the sounds ... at least seems to begin ...
but cannot hear it properly
THE ORIGINAL CODE
Code: |
//# ATMEL stuff #//
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
uint16_t pitchPhase, form1Phase,form2Phase,form3Phase;
uint16_t pitchPhaseInc,form1PhaseInc,form2PhaseInc,form3PhaseInc;
uint8_t form1Amp,form2Amp,form3Amp;
uint8_t noiseMod=10;
int8_t sinCalc[256] PROGMEM = {
/* This table rolls a lot of functions together for speed.
Extracting phase and amplitude from the nybble packed form
Sine calculation
Exponential amplitude mapping
Scaling to appropriate range
ROUND(
FLOOR(a/16,1)
*SIN(
2
* PI()
* IF(
MOD(a,16),
EXP(0.18*MOD(a,16)),
0
) /16
)*127
,0)
*/
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,2,2,3,3,4,5,6,7,8,10,12,14,17,20,24,
0,4,4,5,6,7,9,11,13,15,18,22,26,31,37,45,
0,5,6,7,8,10,12,14,17,20,24,28,34,41,49,58,
0,5,6,7,9,10,12,15,18,21,26,31,37,44,53,63,
0,5,6,7,8,10,12,14,17,20,24,28,34,41,49,58,
0,4,4,5,6,7,9,11,13,15,18,22,26,31,37,45,
0,2,2,3,3,4,5,6,7,8,10,12,14,17,20,24,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,-2,-2,-3,-3,-4,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,
0,-4,-4,-5,-6,-7,-9,-11,-13,-15,-18,-22,-26,-31,-37,-45,
0,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,-28,-34,-41,-49,-58,
0,-5,-6,-7,-9,-10,-12,-15,-18,-21,-26,-31,-37,-44,-53,-63,
0,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,-28,-34,-41,-49,-58,
0,-4,-4,-5,-6,-7,-9,-11,-13,-15,-18,-22,-26,-31,-37,-45,
0,-2,-2,-3,-3,-4,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24
};
int8_t sqrCalc[256] PROGMEM ={
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,1,2,2,2,3,3,4,5,5,6,8,9,11,13,16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16,
0,-1,-2,-2,-2,-3,-3,-4,-5,-5,-6,-8,-9,-11,-13,-16
};
// Changing these will also requires rewriting audioOn()
#if defined(__AVR_ATmega8__)
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 5
// On old ATmega8 boards, output is on pin 11
#define PWM_PIN 11
#define PWM_VALUE OCR2
#define PWM_INTERRUPT TIMER2_OVF_vect
#elif defined(__AVR_ATmega1280__)
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 7
#define PWM_PIN 3
#define PWM_VALUE OCR3C
#define PWM_INTERRUPT TIMER3_OVF_vect
#else
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 5
// For modern ATmega168 boards, output is on pin 3
#define PWM_PIN 3
#define PWM_VALUE OCR2B
#define PWM_INTERRUPT TIMER2_OVF_vect
#endif
void audioOn() {
#if defined(__AVR_ATmega8__)
// ATmega8 has different registers
TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
TIMSK = _BV(TOIE2);
#elif defined(__AVR_ATmega1280__)
TCCR3A = _BV(COM3C1) | _BV(WGM30);
TCCR3B = _BV(CS30);
TIMSK3 = _BV(TOIE3);
#else
// Set up PWM to 31.25kHz, phase accurate
TCCR2A = _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(CS20);
TIMSK2 = _BV(TOIE2);
#endif
}
void setup() {
pinMode(PWM_PIN,OUTPUT);
audioOn();
pinMode(LED_PIN,OUTPUT);
}
#define FORMANT_SZ 7
enum {
_SP,_DOT,_QM,_COM,_HYP,_IY,_IH,_EH,_AE,_AA,
_AH,_AO,_UH,_AX,_IX,_ER,_UX,_OH,_RX,_LX,
_WX,_YX,_WH,_R,_L,_W,_Y,_M,_N,_NX,
_DX,_Q,_S,_SH,_F,_TH,__H,__X,_Z,_ZH,
_V,_DH,_CHa,_CHb,_Ja,_Jb,_Jc,_Jd,_EY,_AY,
_OY,_AW,_OW,_UW,_Ba,_Bb,_Bc,_Da,_Db,_Dc,
_Ga,_Gb,_Gc,_GXa,_GXb,_GXc,_Pa,_Pb,_Pc,_Ta,
_Tb,_Tc,_Ka,_Kb,_Kc,_KXa,_KXb,_KXc
};
uint8_t formantTable[] PROGMEM = {
0x0, 0x0, 0x0,0x0,0x0,0x0,0x0,/*00 space*/ 0x13,0x43,0x5b,0x0,0x0,0x0,0x0,/*01 .*/
0x13,0x43,0x5b,0x0,0x0,0x0,0x0,/*02 ?*/ 0x13,0x43,0x5b,0x0,0x0,0x0,0x0,/*03 ,*/
0x13,0x43,0x5b,0x0,0x0,0x0,0x0,/*04 -*/ 0xa,0x54,0x6e,0xd,0xa,0x8,0x0,/*05 IY*/
0xe,0x49,0x5d,0xd,0x8,0x7,0x0,/*06 IH*/ 0x13,0x43,0x5b,0xe,0xd,0x8,0x0,/*07 EH*/
0x18,0x3f,0x58,0xf,0xe,0x8,0x0,/*08 AE*/ 0x1b,0x28,0x59,0xf,0xd,0x1,0x0,/*09 AA*/
0x17,0x2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,0x0,0x0,0x0,/*63 GXa*/
0x6,0x54,0x5e,0x4,0x1,0x0,0xf,/*64 GXb*/ 0x6,0x54,0x5e,0x0,0x0,0x0,0x0,/*65 GXc*/
0x6,0x1a,0x51,0x0,0x0,0x0,0x0,/*66 Pa*/ 0x6,0x1a,0x51,0x0,0x0,0x0,0x0,/*67 Pb*/
0x6,0x1a,0x51,0x0,0x0,0x0,0x0,/*68 Pc*/ 0x6,0x42,0x79,0x0,0x0,0x0,0x0,/*69 Ta*/
0x6,0x42,0x79,0x0,0x0,0x0,0x0,/*70 Tb*/ 0x6,0x42,0x79,0x0,0x0,0x0,0x0,/*71 Tc*/
0x6,0x6d,0x65,0x0,0x0,0x0,0x0,/*72 Ka*/ 0xa,0x56,0x65,0xc,0xa,0x7,0x0,/*73 Kb*/
0xa,0x6d,0x70,0x0,0x0,0x0,0x0,/*74 Kc*/ 0x6,0x54,0x5e,0x0,0x0,0x0,0x0,/*75 KXa*/
0x6,0x54,0x5e,0x0,0xa,0x5,0x0,/*76 KXb*/ 0x6,0x54,0x5e,0x0,0x0,0x0,0x0 /*77 KXc*/
};
uint16_t pitchTable[64] = {
// Covers A1 to C7
58,61,65,69,73,77,82,86,92,97,
103,109,115,122,129,137,145,154,163,173,
183,194,206,218,231,244,259,274,291,308,
326,346,366,388,411,435,461,489,518,549,
581,616,652,691,732,776,822,871,923,978,
1036,1097,1163,1232,1305,1383,1465,1552,1644,1742,
1845,1955,2071,2195
};
uint8_t frameList[] PROGMEM = {
#if 1
_Da,3,0,39,_Db,1,0,39,_Dc,1,3,39,_EY,8,6,39,_YX,20,3,39, // Dai..
_Z,10,0,36,_IY,35,3,36, // ..sy
_Da,3,0,32,_Db,1,0,32,_Dc,1,3,32,_EY,8,6,32,_YX,20,3,32, // Dai..
_Z,10,0,27,_IY,35,3,27, // ..sy
_Ga,2,0,29,_Gb,2,0,29,_Gc,2,0,29,_IH,10,3,29,_V,5,0,29, // Give
_M,2,0,31,_IY,10,3,31, // me
_YX,5,0,32,_AO,10,0,32,_RX,5,0,32, // your
_AH,25,0,29,_NX,5,0,29, // an..
_S,2,0,32,_ER,10,0,32,_RX,3,0,32, // ..swer
_Da,3,0,27,_Db,1,0,27,_Dc,1,3,27,_UX,80,3,27,_WX,5,0,27, // do
_AY,5,20,34,_YX,10,0,34,_M,8,0,34, // I'm
__H,5,0,39,_AX,30,0,39,_F,10,0,39, // half
_Ka,3,0,36,_Kb,3,0,36,_Kc,4,0,36,_R,5,0,36,_EY,30,0,36, // cra..
_Z,5,0,32,_IY,40,0,32, // ..zy
_AO,10,0,29,_LX,5,0,29, // all
_F,5,0,31,_AO,10,0,31, // for
_DH,5,0,32,_AH,10,0,32, // the
_L,5,0,34,_AH,20,0,34,_V,5,0,34,// love
_AA,10,0,36,_V,5,0,36,// of
_Y,10,0,34,_UX,80,0,34, // you
_IH,10,0,36,_Ta,2,0,36,_Tb,1,0,36,_Tc,2,0,36,// It
_W,2,0,37,_OH,10,0,37,_N,1,0,37,_Ta,1,0,37,_Tb,1,0,37,_Tc,1,0,37,// won't
_Ba,2,0,36,_Bb,1,0,36,_Bc,2,0,36,_IY,10,0,36,// be
_AH,15,0,34,// a
_S,2,0,39,_Ta,2,0,39,_Tb,2,0,39,_Tc,2,0,39,_AY,1,10,39,_YX,10,0,39,// sty..
_L,3,0,36,_IH,10,0,36,_SH,2,0,36,// ..lish
_M,5,0,34,_AE,10,0,34,// ma..
_R,5,0,32,_IH,60,0,32,_Ja,2,0,32,_Jb,2,0,32,_Jc,2,0,32,// ..rriage
_AY,5,10,34,_YX,5,0,34,// I
_Ka,2,0,36,_Kb,2,0,36,_Kc,2,0,36,_AH,20,0,36,_N,2,0,36,_Ta,2,0,36,_Tb,2,0,26,_Tc,2,0,36,// can't
_AX,15,0,32,// a..
_F,5,0,29,_AO,20,0,29,_R,2,0,29,_Da,1,0,29,_Db,1,0,29,_Dc,1,0,29,// ..fford
_AX,15,0,32,// a
_Ka,1,0,29,_Kb,1,0,29,_Kc,1,0,29,_AE,12,0,29,// ca..
_R,5,0,27,_IH,45,0,27,_Ja,2,0,27,_Jb,2,0,27,_Jc,2,0,27,// ..rriage
_Ba,1,0,27,_Bb,1,0,27,_Bc,1,0,27,_AH,10,0,27,_Ta,1,0,27,_Tb,1,0,27,_Tc,1,0,27,// but
_Y,5,0,32,_UH,10,10,32,_L,5,0,32,// you'll
_L,3,0,36,_UH,10,0,36,_Ka,1,0,36,_Kb,1,0,36,_Kc,1,0,36,// look
_S,2,0,34,_W,2,0,34,_IY,20,0,34,_Ta,2,0,34,_Tb,2,0,34,_Tc,2,0,34,// sweet
_AX,15,0,27,// a..
_Ka,2,0,32,_Kb,2,0,32,_Kc,2,0,32,_R,2,0,32,_AA,20,0,32,_S,5,0,32,// ..cross
_DH,5,0,36,_AH,10,0,36,// the
_S,2,0,34,_IY,10,0,34,_Ta,2,0,34,_Tb,2,0,34,_Tc,2,0,34,// seat
_AA,10,0,36,_V,5,0,36,// of
_AE,15,0,37,// a
_Ba,2,0,39,_Bb,2,0,39,_Bc,2,0,39,_AY,5,5,39,_YX,5,0,39,// bi..
_S,5,0,36,_IH,10,0,36,// ..cy..
_Ka,2,0,32,_Kb,2,0,32,_Kc,2,0,32,_L,9,0,32,// ..cle
_M,2,0,34,_EY,5,10,34,_YX,10,0,34,_Da,2,0,34,_Db,2,0,34,_Dc,2,0,34,// made
_F,5,0,27,_OY,1,5,27,_RX,5,0,27,// for
_Ta,2,0,32,_Tb,2,0,32,_Tc,2,0,32,_UX,50,0,32,// two
#endif
_Ta,0,0,61
};
int frameTime = 15; // ms
uint16_t basePitch;
int formantScale;
void loop() {
formantScale = 54;//random(20,80);//54;
uint8_t *framePos = frameList;
while(1) {
int n;
uint8_t startFormant,staticFrames,tweenFrames;
uint16_t startPitch,nextPitch;
uint8_t nextFormant;
int16_t startForm1PhaseInc,startForm2PhaseInc,startForm3PhaseInc;
uint8_t startForm1Amp,startForm2Amp,startForm3Amp;
uint8_t startMod;
uint8_t *formantPos;
// Read next framelist item
startFormant = pgm_read_byte(framePos++);
staticFrames = pgm_read_byte(framePos++);
if (!staticFrames) break; // End of phrase
tweenFrames = pgm_read_byte(framePos++);
startPitch = pitchTable[pgm_read_byte(framePos++)];
nextFormant = pgm_read_byte(framePos);
nextPitch = pitchTable[pgm_read_byte(framePos+3)];
pitchPhaseInc = startPitch;
formantPos = formantTable + startFormant * FORMANT_SZ;
form1PhaseInc = startForm1PhaseInc = pgm_read_byte(formantPos++)*formantScale;
form2PhaseInc = startForm2PhaseInc = pgm_read_byte(formantPos++)*formantScale;
form3PhaseInc = startForm3PhaseInc = pgm_read_byte(formantPos++)*formantScale;
form1Amp = startForm1Amp = pgm_read_byte(formantPos++);
form2Amp = startForm2Amp = pgm_read_byte(formantPos++);
form3Amp = startForm3Amp = pgm_read_byte(formantPos++);
noiseMod = startMod = pgm_read_byte(formantPos++);
for (;staticFrames--;) delay(frameTime);
if (tweenFrames) {
uint8_t* formantPos;
int16_t deltaForm1PhaseInc,deltaForm2PhaseInc,deltaForm3PhaseInc;
int8_t deltaForm1Amp,deltaForm2Amp,deltaForm3Amp;
int8_t deltaMod;
uint8_t nextMod;
int16_t deltaPitch;
tweenFrames--;
formantPos = formantTable + nextFormant * FORMANT_SZ;
deltaForm1PhaseInc = pgm_read_byte(formantPos++)*formantScale - startForm1PhaseInc;
deltaForm2PhaseInc = pgm_read_byte(formantPos++)*formantScale - startForm2PhaseInc;
deltaForm3PhaseInc = pgm_read_byte(formantPos++)*formantScale - startForm3PhaseInc;
deltaForm1Amp = pgm_read_byte(formantPos++) - startForm1Amp;
deltaForm2Amp = pgm_read_byte(formantPos++) - startForm2Amp;
deltaForm3Amp = pgm_read_byte(formantPos++) - startForm3Amp;
deltaMod = pgm_read_byte(formantPos++) - startMod;
deltaPitch = nextPitch - startPitch;
deltaMod = nextMod - startMod;
for (int i=1; i<=tweenFrames; i++) {
form1PhaseInc = startForm1PhaseInc + (i*deltaForm1PhaseInc)/tweenFrames;
form2PhaseInc = startForm2PhaseInc + (i*deltaForm2PhaseInc)/tweenFrames;
form3PhaseInc = startForm3PhaseInc + (i*deltaForm3PhaseInc)/tweenFrames;
form1Amp = startForm1Amp + (i*deltaForm1Amp)/tweenFrames;
form2Amp = startForm2Amp + (i*deltaForm2Amp)/tweenFrames;
form3Amp = startForm3Amp + (i*deltaForm3Amp)/tweenFrames;
pitchPhaseInc = startPitch + (i*deltaPitch)/tweenFrames;
noiseMod = startMod + (i*deltaMod)/tweenFrames;
delay(frameTime);
}
}
}
delay(300);
}
SIGNAL(PWM_INTERRUPT)
{
int8_t value;
static int8_t noise;
int16_t phaseNoise = noise * noiseMod;
noise += noise<<2; noise++; // noise' = 5*noise+1
form1Phase += form1PhaseInc;
value = pgm_read_byte(sinCalc+(((form1Phase>>8) & 0xf0) | form1Amp));
form2Phase += form2PhaseInc;
value += pgm_read_byte(sinCalc+(((form2Phase>>8) & 0xf0) | form2Amp));
form3Phase += form3PhaseInc;
value += pgm_read_byte(sqrCalc+(((form3Phase>>8) & 0xf0) | form3Amp));
value = (value * (0xff^(pitchPhase>>8)))>>8;
pitchPhase += pitchPhaseInc;
if ((pitchPhase+phaseNoise) < pitchPhaseInc) {
form1Phase = 0;
form2Phase = 0;
form3Phase = 0;
}
PWM_VALUE = value + 0x80;
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19587
|
|
Posted: Thu Feb 06, 2014 1:53 am |
|
|
Key problem I can see here, is the use of pgm_read_byte, and the wrong syntax for accessing the address of arrays.
As written, this won't read the byte from the array as required.
First, use rom, not const for the arrays.
Then declare the pointer as:
unin8_t rom *framepos = frameList;
Remember in C, the name of an array, is it's address. You are using &frameList, which says take the address of the array's address... Not correct.
rom does exactly the same as const, except it builds the extra structures to allow a ROM address to be constructed (remember that in the PIC, the ROM data space is a separate memory area to RAM), and declaring a pointer as 'rom', says that this is a pointer to ROM.
Then don't bother with pgm_read_byte, just use *frameList.
If a pointer is declared as to rom (not const), CCS knows to use the program memory read function to access it.
Same applies with formatTable etc..
Now in the original construction, if you did use 'frameList', to access a const array, this does not return the address in ROM. It returns the address of the program to access the array. To get the address of the actual table (if you wanted to use the original approach), you need to use the 'label_address' function. However much more complex than just accessing the table directly.
To show how this works, I took your sin table, and constructed a simple loop to access this:
Code: |
//processor defines here
#include <stdint.h>
rom int8_t sinCalc[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,2,2,3,3,4,5,6,7,8,10,12,14,17,20,24,
0,4,4,5,6,7,9,11,13,15,18,22,26,31,37,45,
0,5,6,7,8,10,12,14,17,20,24,28,34,41,49,58,
0,5,6,7,9,10,12,15,18,21,26,31,37,44,53,63,
0,5,6,7,8,10,12,14,17,20,24,28,34,41,49,58,
0,4,4,5,6,7,9,11,13,15,18,22,26,31,37,45,
0,2,2,3,3,4,5,6,7,8,10,12,14,17,20,24,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,-2,-2,-3,-3,-4,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,
0,-4,-4,-5,-6,-7,-9,-11,-13,-15,-18,-22,-26,-31,-37,-45,
0,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,-28,-34,-41,-49,-58,
0,-5,-6,-7,-9,-10,-12,-15,-18,-21,-26,-31,-37,-44,-53,-63,
0,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24,-28,-34,-41,-49,-58,
0,-4,-4,-5,-6,-7,-9,-11,-13,-15,-18,-22,-26,-31,-37,-45,
0,-2,-2,-3,-3,-4,-5,-6,-7,-8,-10,-12,-14,-17,-20,-24
};
void main(void)
{
uint8_t rom *ptrSin = sinCalc;
int8 ctr;
int8_t val;
for (ctr=0;ctr<255;ctr++)
{
val=*ptrSin; //Then debugging to watch val here
ptrSins++;
}
while(TRUE) ;
}
|
You will merrily see it fetch each value from ROM, correctly signed etc., yet only use 10 RAM locations on this test program.
Best Wishes |
|
|
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
|
Posted: Thu Feb 06, 2014 2:10 pm |
|
|
i posted the original code , not the converted code, i will post the converted and i do indeed use ROM, (it works well with just const too)
i'll paste later |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|