Import upstream version 0.99.2
[fmit.git] / libs / Music / ScoreGenerator.cpp
1 // Copyright 2004 "Gilles Degottex"
2
3 // This file is part of "Music"
4
5 // "Music" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 // 
10 // "Music" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 // 
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20 #include "ScoreGenerator.h"
21
22 #include <iostream>
23 //#include <fstream>
24 //#include <sstream>
25 //#include <deque>
26 using namespace std;
27
28 #include <CppAddons/Math.h>
29 //#include <CppAddons/String.h>
30 //using namespace String;
31 #include <CppAddons/Random.h>
32
33 #include "Music.h"
34
35 namespace Music
36 {
37
38 void GenerateScore(     vector<double>& buffer,                 // the score's wav
39                                 vector< vector<int> >& score,   // the score
40                                 double lenght,                                  // lenght of the score in seconds       : R+*
41                                 int dataBySecond,                               // number of data by second                     : ~{11000, 22000, 44100}
42                                 double AFreq,                                   // frequency of A                                       : ~440.0
43                                 int minHT,                                              // minimum halftone                                     : Z             // TODO <0
44                                 int maxHT,                                              // maximum halftone                                     : Z
45                                 int nbNotes,                                    // number of notes in lenght            : N*
46                                 int nbVoice)                                    // number of voices                                     : N*
47 {
48         static Random rdm;
49
50         int size = int(lenght*dataBySecond);
51         int nbHT = maxHT-minHT+1;
52         int note_size = size / nbNotes;
53         cout << "AFreq=" << AFreq << " dataBySecond=" << dataBySecond << endl;
54         cout << "nbNotes=" << nbNotes << " lenght=" << lenght << " minHT=" << minHT << " maxHT=" << maxHT << endl;
55         cout << "size=" << size << " nbHT=" << nbHT << " nbVoice=" << nbVoice << endl;
56
57         cout << "make score ... \t\t" << flush;
58 //      int n=0;
59         for(int i=0; i<nbNotes; i++)
60         {
61                 vector<int> hs(nbVoice);
62                 for(size_t v=0; v<hs.size(); v++)
63                         hs[v] = minHT+rdm.nextInt(maxHT-minHT+1);
64
65 //              cout << "halftone selected=" << h << " freq=" << freq << endl;
66                 for(int j=0; j<note_size; j++)
67                 {
68                         double buff = 0.0;
69                         for(size_t v=0; v<hs.size(); v++)
70                                 buff += sin( ((2.0*Math::Pi)/dataBySecond)*h2f(hs[v], AFreq)*double(j) );
71
72 //                      int index = i*note_size+j;
73                         // add data index and wav value
74         
75                         buffer.push_back(buff);
76                         // add score halftones (solution)
77                         vector<int> score_at(nbHT);
78                         for(int vh=0; vh<nbHT; vh++)
79                         {
80                                 int ampl = 0;
81                                 for(size_t v=0; v<hs.size(); v++)
82                                         if(hs[v]==vh+minHT)
83                                                 ampl++;
84                                 score_at[vh] = ampl;
85                         }
86                         score.push_back(score_at);
87 //                      n = undoable_out_percent(cout, 100.0f*index/size, n);
88                 }
89         }
90 //      undoable_out_clear(cout, n);
91         cout << "done   " << endl;
92 }
93
94 }
95