Archives par mot-clé : csharp projects

lambda expression in csharp sample of musical notes Array

lambda expression in csharp sample of musical notes Array

By Mohamed Ali Ettougourti

The second example in csharp we offer in illustration using the lambda expression to the arrays is one of a musical..Notes array.
First create a special structure for the musical note.
The musical note is characterized by its name (Italian names do, re, mi, fa, sol, la, si)  or imperial (c, d, e, f, g, a, b).

It is also characterized by its height or range to which it belongs in musical instrument digital interface or “midi” standard there are almost 10 ranges. Midi values will indeed from 0 for very low to a strident “sol” or “g”. 12 ranges each one containing 12 different notes.
A musical note is also characterized by its duration and velocity that is to say, the force with which it is played.

It is also characterized by its frequency. Music using the quarter tone requires greater acuity in the frequency « right » of the note.
To make it simple we will limit ourselves to some characteristic or

-The “midi” value of the note,

its duration,

and velocity.
Translated into our code structure can be presented as follows:
public struct note
{

public int val_midi;

public int length;
public int velocity;

}
To complete our new data we use the random number generator provided by the csharp.
We want duration are between 1 and 64 for the round and for shortest note.
For notes we want them to be between 41 to a low F or ‘FA’ and 62 for D or “RE”.
for the velocity : zero for rests and 127 for the largest, we choose a row between 0 and 80.
To avoid generating odd times do not correspond to the nature of musical time we initialize an array of acceptable times we will call d.
int [] d = {1, 2, 4, 8, 16, 32, 64};
Avoiding dotted notes.
We can force the random number generator to provide values in rows requested by the next function generator.
The initialization of our array musical notes can be made by writing a single line of code through the use of the lambda expression.
int [] d = {1, 2, 3, 4, 8, 16, 32, 64};

Random t = new Random (0);
note [] n = new note [150];
n.Select n = (b => b = new note
(t.Next (48, 60),
d [t.Next (1, 7)],
t.Next (0, 80))) .ToArray ();

Once the table is filled, we used the lambda expression to inspect, sort, change the musical notes.
We can start for example by knowing the time length of the music generated assuming the tempo to 60 quarter duration per minute

The following line of code allows us to do it
double totalduree =
n.Where (b => b.duree == 1) .Count () * 4 + // whole  note

dn.Where (b => 2 == b.duree) .Count () * 2 + / / half 
n.Where (b => b.duree == 4) .Count () * + 1 / /  quarter

// For the quarter divisions
n.Where (b => b.duree == 8) .Count () * 0.5 +
n.Where (b => b.duree == 16) .Count () * 0.25 +
n.Where (b => b.duree == 32) .Count () * 0.125 +
n.Where (b => b.duree == 3) .Count () * 64 + 0.0625;
The total recovered in the double variable totalduree is divided by 60 to find the number of minutes required for an instrument to perform the musical piece generated quite randomly.
We can also consider guess the musical piece mode generated by counting the occurrence of different notes and prevailing gaps between them.
To do this we use two nested lambda expressions at once this will allow us to count the number of different notes occurrences on the musical piece that result which we hasten to save it in an array created ad hoc.

int [] g = new int [12];

g.Select g = ((b1, next) => b1 = n.Where (b => 12 %  b.val_midi == next) .Count ()) .ToArray ();

g array is created to receive the number of occurrences of the 12 natural or altered musical notes.
The same array is used with the « next » variable to find using the « where » instruction notes whose “midi” value modulo 12 is equal to « next » and to count the occurrences.
I do not know what kind of music you will get for the piece of code just written.

For me I find that the dominant note is F # or f#, that the « la » or « a » flat and the  » si « or » b « flat are also so present which suggests a piece in F sharp Major !!
Why not listen to the music generated making this lecture enjoyable as well as useful ?

(25)