Cabbage Logo
Back to Cabbage Site

From C++ to Csound

Hi guys,

today I’m getting frustrated by trying to convert this simple C++ program to Csound code.
Is an Euclidean Rhythm generator: it print the rhythm as a binary code.
The things that I’d like to do is to print the rhythm inside a table or array to play it with sound, but I can’t find a solution to print and create something similar to" void print" and “void musical print”

anyone have suggestion or experience with C++? :relaxed:

#include  < iostream>
 #include < cmath>
 #include < conio.h>

using namespace std;

void print(int nzeri, bool reverse){
     if (reverse==0){
     cout<<"1";
     for (int i=0; i<nzeri; i++){cout<<"0";}
} 
else {cout<<"0";
     for (int i=0; i<nzeri; i++){cout<<"1";}
     }
     }
     
     
void musicalprint(int nzeri, bool reverse){
     if (reverse==0){
     cout<<"x ";
     for (int i=0; i<nzeri; i++){cout<<". ";}
} 
else {cout<<". ";
     for (int i=0; i<nzeri; i++){cout<<"x ";}
     }
     }     
     
int main(){
    int battiti, accenti, zeri, temp;
    int dimshort, dimlong, resto, i, j, nlong, nshort, divint;
    char quit;
    bool rev=0;
    here:
    rev=0;
    cout<<"----------------------------------------------------"<<endl;
    cout<<"Equal distribution following Euclid algorithm \"DE BORD\" "<<endl;
    cout<<"----------------------------------------------------"<<endl;
    dati:
    cout<<endl;
    cout<<"Introduce the beat number: ";
    cin>>battiti;
    cout<<"Introduci the beat number: ";
    cin>>accenti;
    
    if (battiti<accenti) {cout<<endl<<"ERROR: the beats are less than the accent!"<<endl<<endl; goto dati;}
    cout<<endl;
    if (accenti<=0){cout<<endl<<"ERROR: the accent numeber can't be zero or negative!"<<endl<<endl;  goto dati;}
    if (battiti<=0){cout<<endl<<"ERROR: the accent numeber can't be zero or negative!"<<endl<<endl;  goto dati;}
    zeri=battiti-accenti;
    
    if (accenti>zeri && zeri!=0) {temp=accenti; accenti=zeri; zeri=temp; rev=1;} //Inverto zeri e uni, poi li rinverto alla fine
    
    dimshort=zeri/accenti;
    dimlong=dimshort+1;
    resto=zeri-(dimshort*accenti);
    nlong=resto;
    nshort=accenti-resto;
    //-----------------------
    if (nlong<nshort&& nlong!=0){temp=nlong; nlong=nshort; nshort=temp; temp=dimshort; dimshort=dimlong; dimlong=temp;}
    divint=nlong/nshort;
    resto=nlong-(nshort*divint);

    cout<<"Stringa ";
    for (i=0; i<nshort; i++){
        for (j=0; j<divint; j++){print(dimlong,rev);}
        print(dimshort, rev);
        }
    for (i=0; i<resto; i++){print(dimlong,rev);}
    
    cout<<endl<<endl<<"Result \"equa\" dei battiti e':"<<endl<<endl;  
    for (int repeat=0; repeat<15; repeat++){
          for (i=0; i<nshort; i++){
              for (j=0; j<divint; j++){musicalprint(dimlong,rev);}
              musicalprint(dimshort, rev);
        }
    for (i=0; i<resto; i++){musicalprint(dimlong,rev);}  
    }
    cout<<endl<<endl<<endl;
    cout<<"Premi 'q' per uscire, o un qualsiasi altro tasto per riavviare. ";
    quit=(char)_getch();
    if(quit!='q') {system("CLS"); goto here;}
    
    return 0;
    
}

Perhaps you could write the print function like this (warning, untested code!!):

gkArray[] init 8

instr 1
kNzeri = p4
iReverse = p5
iIndex init 0

while iIndex<kNeri do
	if iReverse == 0
		gkArray[iIndex] = 0;
	else
		gkArray[iIndex] = 1;
	endif
	iIndex += 1
od

endin