A function to create a padded binary string.
if _bin$ would return "10" this can return "00000010"
EDIT: corrected a typo in function the demo let slip past but using this in another program exposed the error. All good now.
Code: (Select All)
'binpad$
' written with qb64PE 0.8.2
'a function to pad strings for binary values of numbers with leadiing 0's
'
Print "Enter a number and the binary string will be returned"
Do
Input "Enter a number from 0 to 255 (-1 to exit) :", n
If n > -1 And n < 256 Then
bb = n
Print
Print "Integer: "; n
bt$ = binpad$(n, 8)
Print "Binary:"; bt$
_Clipboard$ = bt$ 'write the string to the clipbaord
End If
Loop Until n = -1
Print "BYE."
'===================================================================================
Function binpad$ (b, d)
'binpad$ returns a string of a binary value
'leading 0's will be inserted to pad a string to the minimum size of d characters
'b is the binary value
'd is digits to display
bt$ = _Bin$(b)
If Len(bt$) < d Then
c = d - Len(bt$)
bt$ = String$(c, Asc("0")) + bt$
End If
binpad$ = bt$
End Function
I tried the following bit of code as a set up for it, from a larger subroutine:
...
REDIM ss(8) AS STRING * 8
ss(1) = "1:Ia": ss(2) = "2:Ib": ss(3) = "3:II": ss(4) = "4:III"
ss(5) = "5:IV": ss(6) = "6:V": ss(7) = "7:VI": ss(8) = "8" ' <<<colon D throws the smilie... dwarf stars are happy bastards.
SELECT CASE specindex
CASE IS > 50
DIM m AS _MEM: m = _MEM(ss): MemSmush m, 5: _MEMFREE m 'removes 5th element by copying upper ones down over it
REDIM _PRESERVE ss(UBOUND(ss) - 1) AS STRING * 8
ss(0) = "1234678"
CASE IS <= 34
DIM m1 AS _MEM: m1 = _MEM(ss): MemSmush m1, 7: _MEMFREE m1 'removes the 7th "" """ """
REDIM _PRESERVE ss(UBOUND(ss) - 1) AS STRING * 8
ss(0) = "1234568"
CASE ELSE
ss(0) = "12345678"
END SELECT
ch% = Chs_Key_Button(ss(0), "h", 540, UBOUND(ss), 50, 50, 5, scrw / 2, ss()) ' <<<This last parameter is the issue
...
The idea of this is to filter out certain stellar size choices that don't occur with certain spectral classes (passed by specindex), if anyone is wondering the purpose of it all. The "MemSmush" sub copies all array elements above its second parameter down over that element, and zeros out the uppermost element, saving having to iterate through the array, which is why I opted for a fixed length string array in this instance.
The IDE says 'incorrect array type passed to function on current line' and references the last one shown, i.e. 'ch% = Chs_Key_...'
I've used non-fixed length string arrays with no issues and even did so here with an earlier defined one, in lieu of ss(), which it accepted. Is there a problem passing a fixed length string array to a function? Am I just forgetting some minor syntatic etiquette or what?
I could just load a variable length array in each CASE differently with no more typed code, but it seems a rather clumsy fallback position.
Disclaimer: I'm not sure if I posted about this specifically - if I did, sorry for the repeat!
I know the C compiler built into QB64 is "under the hood", and QB64 is not really a C IDE and compiler.
That said, is there a switch or some hidden setting, so you can type a C program in the editor and get it to compile it to an EXE?
Taking this in another direction, is there a way to see the "intermediate" C code that is generated when it compiles your QB64 program to an EXE? It would be interesting just to look at, to see what the equivalent C code looks like...
Finally, I was looking at dedicated C compilers and IDEs, looking for something that is super simple to install and use. Has anyone got any experience and/or opinions on any of these for Windows?
cLang
Code Blocks
Dev-C++
Embarcadero C++ Builder
GCC
MinGW
MSYS2
Cygwin
Pelles C
Portable C Compiler (PCC)
Tiny C Compiler
Visual Studio 2022 Community
Hi all. First post. I did post a couple of times on Facebook, but now I'm here.
Okay... I have a FOR/NEXT loop that I want to exit if a certain condition occurs. The thing is, if I put a label for it to go to it doesn't do that. It simply resumes on the next line after the loop. I'm using version 2.02
Code: (Select All)
Cls
For x& = 1 To 10
Print x&
If x& = 5 Then Exit For: GoTo stuff
Next x&
This is an improvement on a program I posted a couple days ago. It'll get a some more development at some point.
The screen is randomly populated with an assortment of microbes. They'll skitter about but they do become tired and bored. If you click the mouse that will excite them and get their attention. You can also adjust the temperature and see how they endure. New microbes will slowly spawn in if the population drops.
The program will end when there are no surviving microbes.
Code: (Select All)
'petri_dish
'By James D Jarvis
'
' bunch of wandering microbes in a perti dish
'click on the screen with the left mouse button or drag the mouse around the screen holding hte button down and the ...
'microbes will become more excited and move towards the clicks
'control the temperature with the < and > keys
Do
headcount = 0
Cls
Locate 1, 1: Print tmp; "ø"
While _MouseInput
mouseLeftButton = _MouseButton(1)
If mouseLeftButton Then
taptap = taptap + 50
tapx = _MouseX
tapy = _MouseY
End If
Wend
_Limit 30
For c = 1 To ncells
If health(c) > 0 Then
headcount = headcount + 1
draw_microbe X(c), Y(c), r(c), kkr(c), kkg(c), kkb(c), a(c), c
If Rnd * 10 < 7 Then X(c) = X(c) + xv(c)
If Rnd * 10 < 7 Then Y(c) = Y(c) + yv(c)
If yv(c) > 30 Then yv(c) = 27
If xv(c) > 30 Then xv(c) = 27
If Rnd * 10 < 3 Then xv(c) = xv(c) * .95
If Rnd * 10 < 3 Then yv(c) = yv(c) * .95
If taptap > lstn(c) And tapy < Y(c) Then yv(c) = yv(c) - .5
If taptap > lstn(c) And tapy > Y(c) Then yv(c) = yv(c) + .5
If taptap > lstn(c) And tapx < X(c) Then xv(c) = xv(c) - .5
If taptap > lstn(c) And tapx > X(c) Then xv(c) = xv(c) + .5
If tatap > 0 And X(c) = tapx And Y(x) = tapy Then health(c) = health(c) + 1
If X(c) < r(c) * 2 Then xv(c) = xv(c) * -1
If Y(c) < r(c) * 2 Then yv(c) = yv(c) * -1
If X(c) > xmax - r(c) * 2 Then xv(c) = xv(c) * -1
If Y(c) > ymax - r(c) * 2 Then yv(c) = yv(c) * -1
If tmp < cold(c) Then
xv(c) = xv(c) * .9
health(c) = health(c) - 1
If health(c) < 100 Then kkb(c) = kkb(c) + 1
End If
If tmp > hot(c) * .75 And tmp < hot(c) * 1.2 Then
xv(c) = xv(c) * 1.01
health(c) = health(c) + 1
End If
If tmp > hot(c) * 1.2 Then
xv(c) = xv(c) * .9
health(c) = health(c) - 1
If health(c) < 100 Then kkr(c) = kkr(c) + 1
If kkr(c) > 255 Then kkr(c) = 255
End If
End If
If health(c) > 200 Then health(c) = 200
If health(c) = 0 And Rnd * 400 < 10 And temp > cold(c) Then
X(c) = Int(2 + Rnd * (xmax - 60) / 28) * 30
Y(c) = Int(2 + Rnd * (ymax - 60) / 28) * 30
r(c) = 4 + Int(Rnd * 6)
xv(c) = Int(2 - Rnd * 4)
yv(c) = Int(2 - Rnd * 4)
kkr(c) = 150 + Int(Rnd * 100)
kkg(c) = 150 + Int(Rnd * 100)
kkb(c) = 150 + Int(Rnd * 100)
a(c) = 2 + Int(Rnd * 16)
rt(c) = Int(Rnd * 4) - Int(Rnd * 4)
lstn(c) = Int(Rnd * 100)
cold(c) = temp * .35
hot(c) = temp + 50
health(c) = 100 + Int(Rnd * 100)
headcount = headcount + 1
End If
Next c
_Display
If taptap > 1 Then taptap = taptap - 1
kk$ = InKey$
If kk$ = "," Or kk$ = "<" Then tmp = tmp - .1
If kk$ = "." Or kk$ = ">" Then tmp = tmp + .1
If headcount = 0 Then
Locate 5, 5: Print "ALL MICROBES DIED"
kk$ = Chr$(27)
End If
Loop Until kk$ = Chr$(27)
Input "PRESS ENER TO continue", any$
Sub draw_microbe (x, y, r, kR, kG, kB, arm, c)
'draw a crude radial microbe with flagellum
Draw "C" + Str$(_RGB32(kR, kG, kB))
If x > xmax - r(c) * 2 Then x = xmax - r(c) * 2
If y > ymax - r(c) * 2 Then y = ymax - r(c) * 2
If x < r(c) * 2 Then x = r(c) * 2
If y < r(c) * 2 Then y = r(c) * 2
It's just 2 files (asc2mid.c and mid2asc.c) and I figure regular C code with no fancy OO, it should be pretty straightforward to convert to BASIC, right? I did a little C back (waaaay back) in school (never mind that I hated it!) and did some standard JavaScript back in the day, so I should at least be familiar with the syntax. Should be a snap, right?
NOPE!
I feel like I'm reading Greek. There's all sorts of weird stuff going on - variables named with asterisks (pointers?), if/then and loops without begin/end braces, and even GOTO statements, not to mention a lot of statements strung all together on one line. This is painful. I ran the code through a beautifier which sort of helped, but it also did things like put spaces before/after the asterisks which might be part of pointer variable names or whatever they are. I then went through the code line by line to try and make sense of the control structure and move everything inside of begin/end {} and pretty it up. Results are below - but I am not sure what errors I may have introduced by doing this stuff.
Also I don't have a C compiler to test this, though it might come in handy later if I ever hope to get this Raw Input API multi-mouse code working or experiment with writing WAV files from scratch. Which leads me to a second question - QB64 comes with a C compiler "bundled" with it, right? Is there some way to use it to compile straight C programs?
Is anyone interested in helping make sense of some C code? :-D
Either way, I'll slog through it eventually, just not as fast.
Here is the original code, the attached ZIP also includes my attempt at prettifying it...
"mid2asc.c"
Code: (Select All)
/*
From: http://www.archduke.org/midi/
mid2asc:
Converts midi to ascii. Output can be in one of several formats
determined by command line flags.
Time of an event can be determined by one of
(i) BA+CR: Bar number+Crotchet within bar
(ii) CR: Crotchet number from the very start
(iii) DT: Delta time from previous event - native midi format
(iv) FOL[+/-crotchets]: Time from end of previous note/rest on that track
(v) SIM[+crotchets]: Time from start of previous note/rest on that track
Basic complication of mode (i) is that the Time signature affects
the interpretation of time, and that time signature can change at
any time on any track and should affect all tracks.
Could make it closer to the spec. (e.g., skip over unknown chunk types)
Midi files are going to be ambiguous, e.g. order of some events
occuring at the same time, or whether choose to use running st mode,
or whether to choose to use 0x8* or 0x9* for voff when
velocity=0x40.
Could print key changes from other tracks as comments in sep mode
Copyright November 2002 A.P.Selby
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define MAXLL 10000
#define MAXCH 16
#define MAXNT 128
unsigned char *dat;
int p,siz,pr=0,raw,cro,fol,sep,frpad=9;
int bgn[12][2]={{0,0},{0,1},{1,0},{2,-1},{2,0},{3,0},{3,1},{4,0},{4,1},{5,0},{6,-1},{6,0}};
/* ^ best guess note from pitch relative to keynote: (In C maj/A min this would be: C C# D Eb E F F# G G# A Bb B) */
int maj[14]={0,2,4,5,7,9,11, 12,14,16,17,19,21,23};
char *note2(int n,int s,int pad){
static char buf[100][20];
static int p=0;
int i;
p=(p+1)%100;
buf[p][0]="CDEFGAB"[(n+700000)%7];
i=1;
while(s>0){assert(i<19);buf[p][i++]='#';s--;}
while(s<0){assert(i<19);buf[p][i++]='b';s++;}
while(n>=7){assert(i<19);buf[p][i++]='\'';n-=7;}
while(n<0){assert(i<19);buf[p][i++]='-';n+=7;}
if(pad)while(i<pad&&i<19)buf[p][i++]=' ';
buf[p][i]=0;
return buf[p];
}
char *note(int n,int sf,int mi,int pad){
int a,b,m,a0,b0;
assert(n>=0&&n<128&&sf>=-128&&sf<=127);
a0=(4*sf+7000)%7;b0=(sf+7001)/7-1000;/* (a0,b0) = keynote of (sf,mi) in pair form */
n-=60+maj[a0]+b0;/* subtract keynote in single pitch form */
m=(n+120000)%12;a=bgn[m][0];b=bgn[m][1];/* look up most likely pair form */
return note2(a0+a+((n-m)/12)*7,maj[a0]+maj[a]-maj[a0+a]+b0+b,pad);/* evaluate cocycle! */
}
char *key(int sf,int mi,int pad){
static char buf[100][21];
static int p=0;
int i,n;
p=(p+1)%100;
assert(sf>=-128&&sf<=127);
if(mi)sf+=3;
buf[p][0]="FCGDAEB"[(sf+7001)%7];
n=(sf+7001)/7-1000;i=1;
while(n>0){buf[p][i++]='#';n--;}
while(n<0){buf[p][i++]='b';n++;}
while(i<pad&&i<20)buf[p][i++]=' ';
buf[p][i]=0;
return buf[p];
}
int gcd(int a,int b){if(b==0)return a; else return gcd(b,a%b);}
int sprfrac(char *l,int n,int d,int sum,int pad){
int a,g,s;
s=0;
if(sum){
a=n/d;if(a){
s=sprintf(l,"%d",a);n-=a*d;
if(n==0)goto pad;
s+=sprintf(l+s,"+");
}
}
g=gcd(n,d);n/=g;d/=g;
if(d!=1)s+=sprintf(l+s,"%d/%d",n,d); else s+=sprintf(l+s,"%d",n);
pad:
if(s<pad){memmove(l+pad-s,l,s+1);memset(l,' ',pad-s);s=pad;}
return s;
}
int sprfrac2(char *li,int n,int d){
/* Prints the simplest (or near simplest) number x such that (int)(n/x+.5)=d */
int i,t,dp;
double m,r,x0,x1;
char l[100],*l0,*l1;
if(li)l0=li; else l0=l;
x0=n/(d+.49);x1=n/(d-.49);l1=l0;
/* Find minimum dp such that [x0,x1] intersects Z*10^(-dp) */
for(dp=0,m=1;dp<15;dp++){
r=floor(x1*m);
if(r>x0*m){
t=(int)(r/m);
l1+=sprintf(l1,"%d",t);r-=t*m;
if(dp){
l1+=sprintf(l1,".");
for(i=0;i<dp;i++){m/=10;t=(int)(r/m);l1+=sprintf(l1,"%d",t);r-=t*m;}
}
if((int)(n/atof(l0)+.5)!=d){fprintf(stderr,"Error 2 in prfrac2 n=%d d=%d\n",n,d);exit(1);}
if(li)return l1-l0; else return printf("%s",l);
}
m*=10;
}
fprintf(stderr,"Error 1 in prfrac2 n=%d d=%d\n",n,d);exit(1);
}
void need(int n,char *l){
if(p+n>siz){fprintf(stderr,"Unexpected EOF in %s, p=&%06x n=%d siz=&%06x\n",l,p,n,siz);exit(1);}
}
void skip(int n){need(n,"skip");p+=n;}
void match(char *l){
int i;
need(strlen(l),"match");
for(i=0;l[i];i++)if(l[i]!=dat[p++]){
fprintf(stderr,"Bad match, p=%X i=%d l=%s &%02X!=&%02X\n",p,i,l,l[i],dat[p-1]);exit(1);
}
}
int getn(int n,int a){
int i,t;
need(n,"getn");
for(i=0,t=0;i<n;i++)t=(t<<8)+dat[p+i];
if(a)p+=n;
return t;
}
int get1(){return getn(1,1);}
int get2(){return getn(2,1);}
int get3(){return getn(3,1);}
int get4(){return getn(4,1);}
int look1(){return getn(1,0);}
int getv(){
int c,t;
t=0;
do{
need(1,"getv");c=dat[p++];t=(t<<7)+(c&127);
}while(c&128);
return t;
}
int getnnodt(int st,int e,int *st1){
/* Returns the dt to next non-noteoff event (raw=0), or next event (raw=1). return=-1 means end of track. */
int t,p0,dt,sth;
dt=0;
while(p<e){
dt+=getv();*st1=st;if(raw)return dt;
p0=p;
t=look1();if(t>=0x80)st=get1(); else assert(st>=0x80);
sth=st>>4;
if(sth==8||(sth==9&&dat[p+1]==0))skip(2); else {p=p0;return dt;}
}
return -1;
}
typedef struct {int tt,ch,nt,voff;} noteoffbuf;
int cmp(const void*x,const void*y){return ((noteoffbuf*)x)->tt-((noteoffbuf*)y)->tt;}
int main(int ac,char **av){
int i,s,t,u,bf,bn,btt,fol1,rest,ch,dt,infinity,ln,mi,maxno,nat,non,ols,prb,help,
sk,sf,st,sth,tis0,tis1,tis2,tpc,tr,trl,tt,ty,alltrfl,format,tracks,division,tempo,nobp[MAXCH][MAXNT];
char l[MAXLL],*l1;
double ert;
typedef struct {int a,p0,l,e,p,pv,st,dt,tt,et,pt;} trackinfo;
typedef struct fred {int all,bn,tr;char *l;struct fred *nx;} outline;
trackinfo *tri;
noteoffbuf *nob;
outline *outbuf0,*outbuf;
FILE *fpi;
raw=cro=fol=sep=help=0;
for(i=1;i<ac;i++){
if(!strncmp(av[i],"-c",2))cro=1;
if(!strncmp(av[i],"-f",2))fol=1;
if(!strncmp(av[i],"-r",2))raw=1;
if(!strncmp(av[i],"-s",2))sep=1;
if(!strncmp(av[i],"-h",2)||!strncmp(av[i],"--help",6))help=1;
}
if(ac<2||help){
fprintf(stderr,"Usage: mid2asc [-c] [-f,-r] [-s] midifile > textfile\n\n");
fprintf(stderr," Default timing is to use absolute time determined by BA+CR\n");
fprintf(stderr," and use durations for notes.\n");
fprintf(stderr,"-c Use absolute crotchets rather than BA+CR to determine time.\n");
fprintf(stderr," Useful if inserting or deleting.\n");
fprintf(stderr,"-f 'Follow' (take time from start or end of previous note) to determine time.\n");
fprintf(stderr,"-r \"raw\" format: time determined by DT, and note-off events\n");
fprintf(stderr," are used instead of note durations.\n\n");
fprintf(stderr," Default is to print all tracks merged in chronological order.\n");
fprintf(stderr,"-s Print all tracks separately.\n");
exit(1);
}
if(fol&&raw){fprintf(stderr,"fol and raw modes cannot be combined\n");exit(1);}
fpi=fopen(av[ac-1],"rb");if(!fpi){fprintf(stderr,"Couldn't open %s\n",av[ac-1]);exit(1);}
fseek(fpi,0,SEEK_END);siz=ftell(fpi);rewind(fpi);
if(pr>=1)printf("# File size %d byte%s\n",siz,siz==1?"":"s");
dat=(unsigned char*)malloc(siz);if(!dat){fprintf(stderr,"Couldn't malloc %d bytes\n",siz);exit(1);}
fread(dat,1,siz,fpi);p=0;fclose(fpi);
maxno=siz/2;
match("MThd");
assert(get4()==6);
format=get2();tracks=get2();division=get2();
printf("format=%d tracks=%d division=%d\n",format,tracks,division);
if(division&(1<<15))tpc=48; else tpc=division;
tri=(trackinfo *)malloc(tracks*sizeof(trackinfo));assert(tri);
nob=(noteoffbuf *)malloc(maxno*sizeof(noteoffbuf));assert(nob);
infinity=0x7fffffff;
asc2mid:
Converts ascii to midi. Ascii is in format output by mid2asc in one
of several modes, or it can be in a more mixed format.
Time of an event can be determined by one of
(i) BA+CR: Bar number+Crotchet within bar
(ii) CR: Crotchet number from the very start
(iii) DT: Delta time from previous event - native midi format
(iv) FOL[+/-crotchets]: Time from end of previous note/rest on that track
(v) SIM[+crotchets]: Time from start of previous note/rest on that track
Basic complication of mode (i) is that the Time signature affects
the interpretation of time, and that time signature can change at
any time on any track and should affect all tracks.
At the moment it requires lines of each track to be in chronological
order which can make it slightly awkward to change the track of a
note in sep=1 mode. If all file were in mode (ii), then it could
sort it out for you, I suppose. Or the user could sort the lines
first.
Should make more case insensitive?
MIT License
Copyright November 2002 A.P.Selby
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
char *inbuf,*inend,*ip;
int tr,infinity;
int maj[14]={0,2,4,5,7,9,11, 12,14,16,17,19,21,23};
typedef struct {int l,st,von,bt,pt,et,sta,bn,cr,tt,ln,ip,op;unsigned char *out;} trackinfo;
trackinfo *tri;
/*
trackinfo:
st = midi state (state of previous midi event laid down)
bt = time of last event laid down
pt = start time of last note or rest
et = end time of last note or rest
sta = track state
state 0 ==> Need to read time of new line
state 1 ==> Time of next line specified by tt
state 2 ==> Time of next line specified by bn,cr; tt is valid if there is no time sig. change before it
state 3 ==> Track at an end - no more lines on this track
bn,cr,tt time of next event defined by (bn,cr) or tt according to state
ip = integer input pointer (inbuf+.ip is actual pointer)
ln = input line number
out = pointer to start of output buffer (can change when gets resized)
op = integer output pointer (.out+.op is actual pointer)
l = allocated size of output buffer
von = volume of last note-on event
*/
void err(char *l){
if(tr==-1)fprintf(stderr,"Error: %s\n",l); else fprintf(stderr,"Error: %s at line %d:\n%s\n",l,tri[tr].ln,ip);
exit(1);
}
#define assrt(x,y) if(!(x))err(y);
typedef struct {int tr,tt,ch,nt,voff;} noteoffbuf;
int main(int ac,char **av){
int d,i,n,s,t,cr,bn,ch,nxt,mi,tr0,non,sf,st,rest,tis0,tis1,tis2,tpc,text,tt,siz,format,tracks,division;
char *l1,*l2,*l3;
noteoffbuf *nob;
FILE *fpi;
if(ac<2){
fprintf(stderr,"Usage: asc2mid textfile > midifile\n\n");
fprintf(stderr," Time of an event can be determined by one of\n");
fprintf(stderr," (i) BA+CR: Bar number+Crotchet within bar\n");
fprintf(stderr," (ii) CR: Crotchet number from the very start\n");
fprintf(stderr," (iii) DT: Delta time from previous event - native midi format\n");
fprintf(stderr," (iv) FOL[+/-crotchets]: Time from end of previous note/rest\n");
fprintf(stderr," (v) SIM[+crotchets]: Time from start of previous note/rest\n");
exit(1);
}
fpi=fopen(av[1],"r");if(!fpi){fprintf(stderr,"Couldn't open %s\n",av[1]);exit(1);}
fseek(fpi,0,SEEK_END);siz=ftell(fpi);rewind(fpi);
fprintf(stderr,"File size %d byte%s\n",siz,siz==1?"":"s");
inbuf=(char*)malloc(siz+1);if(!inbuf){fprintf(stderr,"Couldn't malloc %d bytes (inbuf)\n",siz+1);exit(1);}
n=fread(inbuf,1,siz,fpi);assert(n<=siz);siz=n;inbuf[siz]=0;inend=inbuf+siz;fclose(fpi);
for(i=0;i<siz;i++)if(inbuf[i]=='\n')inbuf[i]=0;
tis0=4;tis1=4;tis2=(tis0*4*tpc)/tis1;/* Initial time signature 4/4 */
tt=0;bn=0;cr=0;
/* (tt,bn,cr) are always kept in step, and it is guaranteed that there
are no time sig. changes between tt and the present new time. */
non=0;/* number of pending note-off events */
while(1){
nxt=infinity;tr0=-1;
for(tr=0;tr<tracks;tr++){
if(tri[tr].sta==3)goto nl0;
if(tri[tr].sta==0){
do{
ip=getl(tr);if(!ip){tri[tr].sta=3;goto nl0;}
ip=getns(ip);
l1=strstr(ip,"TR ");if(!l1)err("Expect TR");
}while(strtoi(l1+3,0)!=tr);
if(strncmp(ip,"DT ",3)==0){tri[tr].tt=tri[tr].bt+getfrn(ip+3,tpc);tri[tr].sta=1;goto ok0;}
if(strncmp(ip,"FOL",3)==0||strncmp(ip,"SIM",3)==0){
tri[tr].tt=tri[tr].pt;
if(ip[0]=='F'){
if(tri[tr].et>=0)tri[tr].tt=tri[tr].et; else
fprintf(stderr,"Warning - FOL reverts to SIM following a DT-defined or infinite length note/rest\n");
}
if(ip[3]=='+')tri[tr].tt+=getfrn(ip+4,tpc);
if(ip[3]=='-')tri[tr].tt-=getfrn(ip+4,tpc);
tri[tr].sta=1;goto ok0;
}
if(strncmp(ip,"CR ",3)==0){
tri[tr].tt=getfrn(ip+3,tpc);
tri[tr].sta=1;goto ok0;
}
if(strncmp(ip,"BA ",3)==0){
tri[tr].bn=strtoi(ip+3,&l1)-1;
l1=getns(l1);if(strncmp(l1,"CR ",3))err("Expect CR after BA");
tri[tr].cr=getfrn(l1+3,tpc);
tri[tr].sta=2;goto ok0;
}
err("Expected time specification (DT, FOL, SIM, CR, or BA) at start of line");
}
ok0:
if(tri[tr].sta==2)tri[tr].tt=tt+(tri[tr].bn-bn)*tis2+tri[tr].cr-cr;
if(tri[tr].tt<nxt){nxt=tri[tr].tt;tr0=tr;}
nl0:
tr=tr;/* because ISO C forbids label at end of compound statement, apparently */
}
if(tr0==-1)break;
tr=tr0;ip=inbuf+tri[tr].ip;tri[tr].sta=0;
text=(strstr(ip,"Text")!=0);
/* Need to know if Text line, since otherwise the quoted string
could cause a false match with some other key word */
t=nxt-tt+cr; bn+=t/tis2; cr=t%tis2; tt=nxt;
/* At this point, tt=time of next non-note-off event.
Have to lay down note-off events which have been bypassed. */
tr0=tr;
for(i=0;i<non&&nob[i].tt<=tt;i++){
tr=nob[i].tr;putv(nob[i].tt-tri[tr].bt);tri[tr].bt=nob[i].tt;
if(nob[i].voff!=0x40){
st=0x80+nob[i].ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
put1(nob[i].nt);put1(nob[i].voff);
}else{
st=0x90+nob[i].ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
put1(nob[i].nt);put1(0);
}
}
if(i>0){non-=i;memmove(nob,nob+i,non*sizeof(noteoffbuf));}
tr=tr0;
rest=0;if(!text){l1=strstr(ip,"NT ");if(l1)rest=(toupper(*(getns(l1+3)))=='R');}
if(!rest){putv(tt-tri[tr].bt);tri[tr].bt=tt;}
if(text){
put1(tri[tr].st=0xFF);
l1=strstr(ip,"type");assrt(l1,"missing 'type' in text line");t=strtoi(l1+4,0);put1(t);
l1=strchr(l1+4,'"');assrt(l1,"missing opening quote in text line");l1++;
l2=strrchr(l1,'"');assrt(l2,"missing closing quote in text line");
for(i=s=0;i<l2-l1;i++){
t=l1[i];
if(t=='\\'&&i<l2-l1-1){
t=l1[++i];
if(t=='t')t=9;
if(t=='n')t=10;
if(t=='v')t=11;
if(t=='f')t=12;
if(t=='r')t=13;
}
l1[s++]=t;
}
putv(s);putstn(l1,s);
continue;
}
l1=strstr(ip,"CH ");assrt(l1,"missing channel number");ch=strtoi(l1+3,0)-1;assrt(ch>=0&&ch<16,"channel number out of range");
l1=strstr(ip,"Time signature");if(l1){
tis0=strtoi(l1+14,&l1);tis1=strtoi(l1+1,0);assrt((tis0*4*tpc)%tis1==0,"New time signature not commensurate with division");
tis2=(tis0*4*tpc)/tis1;
put1(tri[tr].st=0xFF);
put1(0x58);putv(4);put1(tis0);
for(i=0,n=tis1;n>1;i++)n>>=1;put1(i);
l2=strstr(ip,"clocks/mtick");assrt(l2,"missing clocks/mtick");put1(strtoi(l2+12,0));
l2=strstr(ip,"crotchets/32ndnote");assrt(l2,"missing crotchets/32ndnote");put1(strtoi(l2+18,0));
continue;
}
l1=strstr(ip,"Channel volume");if(l1){
st=0xB0+ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
t=strtoi(l1+14,0);assrt(t>=0&&t<128,"channel volume out of range");put1(7);put1(t);
continue;
}
l1=strstr(ip,"Instrument");if(l1){
st=0xC0+ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
t=strtoi(l1+10,0)-1;assrt(t>=0&&t<128,"instrument out of range");put1(t);
continue;
}
l1=strstr(ip,"Sysex event");if(l1){
put1(tri[tr].st=strtoi(l1+11,&l1));
n=0;l2=l1;while(1){strtoi(l2,&l3);if(l3==l2)break;l2=l3;n++;}
putv(n);for(l2=l1;n>0;n--)put1(strtoi(l2,&l2));
continue;
}
l1=strstr(ip,"End of track");if(l1){
put1(tri[tr].st=0xFF);
put1(0x2F);put1(0);
continue;
}
l1=strstr(ip,"Tempo");if(l1){
put1(tri[tr].st=0xFF);
n=(int)(60*1000000./atof(l1+5)+.5);assrt(n>=0&&n<=0xFFFFFF,"tempo out of range");
put1(0x51);putv(3);put3(n);
continue;
}
l1=strstr(ip,"Key ");if(l1){
put1(tri[tr].st=0xFF);
l1=getns(l1+4);l2="FCGDAEB";l3=strchr(l2,toupper(*l1));assrt(l3,"unrecognised key");sf=l3-l2-1;
do{l1++;sf+=7*((*l1=='#')-(*l1=='b'));}while(*l1=='#'||*l1=='b');
l2=strstr(l1," major");l3=strstr(l1," minor");assrt((l2!=0)^(l3!=0),"missing minor/major in key");
mi=(l3!=0);sf-=3*mi;
put1(0x59);putv(2);put1(sf&255);put1(mi);
continue;
}
l1=strstr(ip,"Meta Event");if(l1){
put1(tri[tr].st=0xFF);
put1(strtoi(l1+10,&l1));
n=0;l2=l1;while(1){strtoi(l2,&l3);if(l3==l2)break;l2=l3;n++;}
putv(n);for(l2=l1;n>0;n--)put1(strtoi(l2,&l2));
continue;
}
l1=strstr(ip,"ST ");if(l1){
st=strtoi(l1+3,&l1);if(st!=tri[tr].st||((st&0xF0)==0xF0))put1(tri[tr].st=st);
while(1){n=strtoi(l1,&l2);if(l2==l1)break;l1=l2;put1(n);}
continue;
}
l1=strstr(ip,"NT ");if(l1){
tri[tr].pt=tt;tri[tr].et=-1;
n=strton(l1+3,&l1);l1=getns(l1);
if(strncmp(l1,"on",2)==0){
assrt(n>=0,"can't have rest and 'on'");
st=0x90+ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
l2=strstr(l1,"von=");if(l2)tri[tr].von=strtoi(l2+4,0);
assrt(tri[tr].von>=1&&tri[tr].von<128,"von out of range");
put1(n);put1(tri[tr].von);
continue;
}
if(strncmp(l1,"off",3)==0){
l2=strstr(l1,"voff=");if(l2)t=strtoi(l2+5,0); else t=0x40;
assrt(t>=0&&t<128,"voff out of range");
if(t!=0x40){/* not most efficient coding, but most likely to mimic convention used by others(?) */
st=0x80+ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
put1(n);put1(t);
}else{
st=0x90+ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
put1(n);put1(0);
}
continue;
}
d=getfrn(l1,tpc);if(d<infinity){nxt=tt+d;tri[tr].et=nxt;}
if(rest)continue;
assert(n>=0);
l2=strstr(l1,"von=");if(l2)tri[tr].von=strtoi(l2+4,0);
assrt(tri[tr].von>=1&&tri[tr].von<128,"von out of range");
st=0x90+ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
put1(n);put1(tri[tr].von);
if(d<infinity){
l2=strstr(l1,"voff=");if(l2)t=strtoi(l2+5,0); else t=0x40;
for(i=0;i<non;i++)if(nob[i].ch==ch&&nob[i].nt==n){
fprintf(stderr,"Warning - overlapping note at line %d:\n%s\n",tri[tr].ln,ip);
/*
memmove(nob+i,nob+i+1,(non-(i+1))*sizeof(noteoffbuf));non--;
Can't necessarily delete note-off event when notes overlap because
some devices might stack the extra soundings of same note, and so
deleting this event may result in a note stuck on.
*/
break;
}
assrt(non<MAXNOB,"Note-off buffer full");
for(i=0;i<non&&nob[i].tt<=nxt;i++);
memmove(nob+i+1,nob+i,(non-i)*sizeof(noteoffbuf));non++;
nob[i].tr=tr;nob[i].tt=nxt;nob[i].ch=ch;nob[i].nt=n;nob[i].voff=t;
}
continue;
}
fprintf(stderr,"Unrecognised line %d:\n%s\n",tri[tr].ln,ip);exit(1);
}/* main loop */
for(i=0;i<non;i++){
tr=nob[i].tr;putv(nob[i].tt-tri[tr].bt);tri[tr].bt=nob[i].tt;
if(nob[i].voff!=0x40){
st=0x80+nob[i].ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
put1(nob[i].nt);put1(nob[i].voff);
}else{
st=0x90+nob[i].ch;if(st!=tri[tr].st)put1(tri[tr].st=st);
put1(nob[i].nt);put1(0);
}
}
if(i>0){non-=i;memmove(nob,nob+i,non*sizeof(noteoffbuf));}
fprintf(stderr,"Successfully parsed\n");
fwrite(tri[tracks].out,1,tri[tracks].op,stdout);
for(tr=0;tr<tracks;tr++){
t=tri[tr].op;tri[tr].op=0;putst("MTrk");put4(t-8);
if(tri[tr].out)fwrite(tri[tr].out,1,t,stdout); else assert(t==0);
}
return 0;
}
I want to make a program that creates dots in the shape of an ellipse.
So not knowing about the math involved I assumed it would be fairly simple. Then I googled and youtubed and that didn't help much. I did end up with a mild headache though.
There's a fancy formula that stands out from everything I've been learning, but I really don't know how to use it properly. So I thought I'd ask here, in case anyone knows.
The formula is:
x2/a2 + y2/b2 = 1 ( x-squared over a-squared + y-squared over b-squared = 1)
It's been too long since I've worked with algebra so I'm just unsure of how to solve for x. The "=1" part is not helping at all
I've learned the basic principles of the ellipse itself, and so here is an example I'm working with:
The ellipse is 200 wide (2a) by 100 tall (2b)
a = 100
b = 50
located at position 0,0
I know the distance between the focal points is 86.603 in this case
If I could understand how to find a simple x,y point, I'm sure I'd be on my way to writing a program immediately.
The top center point in this case is x0, y50. So I'm wondering what the x value is if y=48. Or if y=46. Or all the way to y=2.
I doubled up my Anemometer and turned them into disco lights! Plus I added a random setting to make them go at a random direction and speed if random mode is turned on. To turn it on press the Space Bar as it says in the title bar. If you press the Space Bar and it doesn't work, press it again a few more times, sometimes it's caught up in a loop I think. To turn random mode off press it again. Without random mode you can use the mouse wheel to change the speed and direction of the lights. Tell me what you think, thanks.
Do
_Limit 50
While _MouseInput: wheel = wheel + _MouseWheel: Wend
a$ = InKey$
If (a$ = " " Or a$ = "r" Or a$ = "R") And rand = 1 Then
rand = 0
a$ = ""
End If
If (a$ = " " Or a$ = "r" Or a$ = "R") And rand = 0 Then
rand = 1
a$ = ""
w = 1
End If
If wheel > 5 Then wheel = 5
If wheel < -5 Then wheel = -5
If rand = 0 Then
w = wheel
_Title "Disco Lights by SierraKen - Use Mouse Wheel - Space Bar to turn random mode on"
End If
If rand = 1 Then
lights = Rnd * 100
If lights > 95 Then w = Int(Rnd * 5) - 5
_Title "Disco Lights by SierraKen - Use Mouse Wheel - Space Bar to turn random mode off"
End If
If t2 < 0 Then GoTo start:
x = (Sin(t) * 20) * (_Pi * 2) + 400
y = (Cos(t) * 10) * (_Pi / 2) + 200
r = (Cos(t) * 180) / _Pi / 1.5 + 50
t = t - (.25 + w / 10)
x2 = (Sin(t2) * 20) * (_Pi * 2) + 400
y2 = (Cos(t2) * 10) * (_Pi / 2) + 200
r2 = (Cos(t2) * 180) / _Pi / 1.5 + 50
t2 = t2 - (.25 + w / 10)
x3 = (Sin(t3) * 20) * (_Pi * 2) + 400
y3 = (Cos(t3) * 10) * (_Pi / 2) + 200
r3 = (Cos(t3) * 180) / _Pi / 1.5 + 50
t3 = t3 - (.25 + w / 10)
For S = .25 To r Step .1
cc = cc - .25
Circle (x, y), S, _RGB32(cc, cc, 100 + cc)
Next S
cc = 200
For S = .25 To r2 Step .1
cc = cc - .25
Circle (x2, y2), S, _RGB32(cc, 100 + cc, cc)
Next S
cc = 200
For S = .25 To r3 Step .1
cc = cc - .25
Circle (x3, y3), S, _RGB32(100 + cc, cc, cc)
Next S
cc = 200
Line (400, 200)-(x, y), _RGB32(255, 255, 255)
Line (400, 200)-(x2, y2), _RGB32(255, 255, 255)
Line (400, 200)-(x3, y3), _RGB32(255, 255, 255)
x4 = (Sin(t4) * 20) * (_Pi * 2) + 400
y4 = (Cos(t4) * 10) * (_Pi / 2) + 400
r4 = (Cos(t4) * 180) / _Pi / 1.5 + 50
t4 = t4 + (.25 + w / 10)
x5 = (Sin(t5) * 20) * (_Pi * 2) + 400
y5 = (Cos(t5) * 10) * (_Pi / 2) + 400
r5 = (Cos(t5) * 180) / _Pi / 1.5 + 50
t5 = t5 + (.25 + w / 10)
x6 = (Sin(t6) * 20) * (_Pi * 2) + 400
y6 = (Cos(t6) * 10) * (_Pi / 2) + 400
r6 = (Cos(t6) * 180) / _Pi / 1.5 + 50
t6 = t6 + (.25 + w / 10)
For S = .25 To r4 Step .1
cc4 = cc4 - .25
Circle (x4, y4), S, _RGB32(cc4, cc4, 100 + cc4)
Next S
cc4 = 200
For S = .25 To r5 Step .1
cc4 = cc4 - .25
Circle (x5, y5), S, _RGB32(cc4, 100 + cc4, cc4)
Next S
cc4 = 200
For S = .25 To r6 Step .1
cc4 = cc4 - .25
Circle (x6, y6), S, _RGB32(100 + cc4, cc4, cc4)
Next S
cc4 = 200
Line (400, 400)-(x4, y4), _RGB32(255, 255, 255)
Line (400, 400)-(x5, y5), _RGB32(255, 255, 255)
Line (400, 400)-(x6, y6), _RGB32(255, 255, 255)
cc2 = 100
For sz = .1 To 100 Step .25
cc2 = cc2 - .25
Circle (400, 550), sz, _RGB32(100 + cc2, 100 + cc2, cc2), , , .5
Next sz
Line (400, 200)-(400, 550), _RGB32(255, 255, 255)
_Display
Cls
Loop Until InKey$ = Chr$(27)