Counterpoint.hpp

Go to the documentation of this file.
00001 #ifndef COUNTERPOINT_HPP
00002 #define COUNTERPOINT_HPP
00003 /*
00004   Status: RO
00005   Return-Path: <bil@ccrma.Stanford.EDU>
00006   Received: from smtp3.Stanford.EDU ([171.67.16.138])
00007   by aaron.mail.atl.earthlink.net (EarthLink SMTP Server) with ESMTP id 1ctFSm3Lh3Nl3qa0
00008   for <gogins@pipeline.com>; Mon, 15 Nov 2004 07:22:30 -0500 (EST)
00009   Received: from cm-mail.stanford.edu (cm-mail.Stanford.EDU [171.64.197.135])
00010   by smtp3.Stanford.EDU (8.12.11/8.12.11) with ESMTP id iAFCMSeh031083
00011   for <gogins@pipeline.com>; Mon, 15 Nov 2004 04:22:29 -0800
00012   Received: from ccrma (cmn13.stanford.edu [171.64.197.162])
00013   by cm-mail.stanford.edu (8.11.6/8.11.6) with ESMTP id iAFCMSH28453
00014   for <gogins@pipeline.com>; Mon, 15 Nov 2004 04:22:28 -0800
00015   Message-ID: <41989F84.7000906@ccrma>
00016   Date: Mon, 15 Nov 2004 04:22:28 -0800
00017   From: Bill Schottstaedt <bil@ccrma.Stanford.EDU>
00018   User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.2) Gecko/20040308
00019   X-Accept-Language: en-us, en
00020   MIME-Version: 1.0
00021   To: Michael Gogins <gogins@pipeline.com>
00022   Subject: Re: Permission
00023   References: <000301c4cab5$48db27f0$6501a8c0@Generator>
00024   In-Reply-To: <000301c4cab5$48db27f0$6501a8c0@Generator>
00025   Content-Type: text/plain; charset=us-ascii; format=flowed
00026   Content-Transfer-Encoding: 7bit
00027   X-ELNK-AV: 0
00028 
00029   You're most welcome to use my Fux code and article in any
00030   way you like.  I did a C translation of it about 10 years
00031   ago -- I think the original SAIL article is at ccrma-ftp
00032   as fux.txt, and I'll append what I think is the C version --
00033   if it doesn't work, let me know, and I'll scrounge around
00034   some old disks at home.  (This project was a ton of fun --
00035   I've always wanted to go back and carry it further, but
00036   have never had time):
00037 */
00038 
00039 /* AUTOMATIC COUNTERPOINT
00040  *
00041  * from "Automatic Species Counterpoint", Bill Schottstaedt, CCRMA STAN-M-19, May 1984
00042  * translated from SAIL to C in March 1995
00043  *
00044  * This file can be treated as a stand-alone C program or as a foreign function module for Lisp.
00045  *
00046  * make fux creates the C program
00047  * cc fux.c -c -O -DCM creates the module
00048  *
00049  * See the "main" function for examples, or fux.lisp (which ties fux.c into CMN/CM).
00050  */
00051 
00052 #include "Platform.hpp"
00053 #ifdef SWIG
00054 %module CsoundAC
00055 %{
00056 #include <string>
00057 #include <cstdarg>
00058 #include <stdio.h>
00059 #include <malloc.h>
00060 #include <boost/random.hpp>
00061 #include <boost/random/variate_generator.hpp>
00062 %}
00063 #else
00064 #include <string>
00065 #include <cstdarg>
00066 #include <stdio.h>
00067 #include <malloc.h>
00068 #include <boost/numeric/ublas/matrix.hpp>
00069 #include <boost/numeric/ublas/vector.hpp>
00070 #include <boost/random.hpp>
00071 #include <boost/random/variate_generator.hpp>
00072 #include "Random.hpp"
00073 using namespace boost::numeric;
00074 #endif
00075 
00076 class Counterpoint
00077 {
00078 public:
00079   void (*messageCallback)(CSOUND *csound, int attribute, const char *format, va_list valist);
00080   void message(const char *format,...);
00081   void message(const char *format, va_list valist);
00082   int MostNotes;
00083   int MostVoices;
00084   enum
00085     {
00086       MostNotes_ = 128,
00087       MostVoices_ = 12
00088     };
00089   long randx;
00090   boost::numeric::ublas::matrix<int> Ctrpt;
00091   boost::numeric::ublas::matrix<int> Onset;
00092   boost::numeric::ublas::matrix<int> Dur;
00093   boost::numeric::ublas::vector<int> TotalNotes;
00094   boost::numeric::ublas::matrix<int> BestFit;
00095   boost::numeric::ublas::matrix<int> BestFit1;
00096   boost::numeric::ublas::matrix<int> BestFit2;
00097   boost::numeric::ublas::vector<int> vbs;
00098   boost::numeric::ublas::matrix<int> RhyPat;
00099   boost::numeric::ublas::vector<int> RhyNotes;
00100   int Fits[3];
00101   virtual void initialize(int mostnotes, int mostvoices);
00102   virtual void clear();
00103   Counterpoint();
00104   virtual ~Counterpoint();
00105   int ABS(int i);
00106   int MIN(int a, int b);
00107   int MAX(int a, int b);
00108   void ARRBLT(int *dest, int *source, int num);
00109   enum
00110     {
00111       Unison = 0,
00112       MinorSecond = 1,
00113       MajorSecond = 2,
00114       MinorThird = 3,
00115       MajorThird = 4,
00116       Fourth = 5,
00117       Tritone = 6,
00118       Fifth = 7,
00119       MinorSixth = 8,
00120       MajorSixth = 9,
00121       MinorSeventh = 10,
00122       MajorSeventh = 11,
00123       Octave = 12
00124     };
00125 #if !defined(SWIG)
00126   static int PerfectConsonance[13];
00127   static int ImperfectConsonance[13];
00128   static int Dissonance[13];
00129 #endif
00130   enum
00131     {
00132       Aeolian = 1,
00133       Dorian = 2,
00134       Phrygian = 3,
00135       Lydian = 4,
00136       Mixolydian = 5,
00137       Ionian = 6,
00138       Locrian = 7
00139     };
00140 #if !defined(SWIG)
00141   static int _Ionian[12];
00142   static int _Dorian[12];
00143   static int _Phrygian[12];
00144   static int _Lydian[12];
00145   static int _Mixolydian[12];
00146   static int _Aeolian[12];
00147   static int _Locrian[12];
00148 #endif
00149   int InMode(int Pitch, int Mode);
00150 #if !defined(SWIG)
00151   static int BadMelodyInterval[13];
00152 #endif
00153   int BadMelody(int Intv);
00154   int ASkip(int Interval);
00155   int AStep(int Interval);
00156   int AThird(int Interval);
00157   int ASeventh(int Interval);
00158   int AnOctave(int Interval);
00159   int ATenth(int Interval);
00160   enum
00161     {
00162       DirectMotion = 1,
00163       ContraryMotion = 2,
00164       ObliqueMotion = 3,
00165       NoMotion = 4
00166     };
00167   int MotionType(int Pitch1, int Pitch2, int Pitch3, int Pitch4);
00168   int DirectMotionToPerfectConsonance(int Pitch1, int Pitch2, int Pitch3, int Pitch4);
00169   int ConsecutiveSkipsInSameDirection(int Pitch1, int Pitch2, int Pitch3);
00170   int LowestSemitone;
00171   int HighestSemitone;
00172   int OutOfRange(int Pitch);
00173   int ExtremeRange(int Pitch);
00174   int BasePitch,Mode,TotalTime;
00175   int Us(int n, int v);
00176   int LastNote(int n, int v);
00177   int FirstNote(int n, int v);
00178   int NextToLastNote(int n, int v);
00179   void SetUs(int n, int p, int v);
00180   int TotalRange(int Cn, int Cp, int v);
00181   int Cantus(int n, int v);
00182   int VIndex(int Time, int VNum);
00183   int Other(int Cn, int v, int v1);
00184   int Bass(int Cn, int v);
00185   enum {
00186 
00187     WholeNote = 8,
00188     HalfNote = 4,
00189     DottedHalfNote = 6,
00190     QuarterNote = 2,
00191     DottedQuarterNote = 3,
00192     EighthNote = 1
00193   };
00194   int Beat8(int n);
00195   int DownBeat(int n, int v);
00196   int UpBeat(int n, int v);
00197   int PitchRepeats(int Cn, int Cp, int v);
00198   enum {
00199     One = 0,
00200     Two = 2,
00201     Three = 3,
00202     Four = 4,
00203     Five = 5,
00204     Six = 6,
00205     Eight = 8
00206   };
00207   int Size(int MelInt);
00208   int TooMuchOfInterval(int Cn, int Cp, int v);
00209   int ADissonance(int Interval, int Cn, int Cp, int v, int Species);
00210   int Doubled(int Pitch, int Cn, int v);
00211   enum
00212     {
00213       infinity = 1000000,
00214       Bad = 100,
00215       RealBad = 200
00216     };
00217   int UnisonPenalty;
00218   int DirectToFifthPenalty;
00219   int DirectToOctavePenalty;
00220   int ParallelFifthPenalty;
00221   int ParallelUnisonPenalty;
00222   int EndOnPerfectPenalty;
00223   int NoLeadingTonePenalty;
00224   int DissonancePenalty;
00225   int OutOfRangePenalty;
00226   int OutOfModePenalty;
00227   int TwoSkipsPenalty;
00228   int DirectMotionPenalty;
00229   int PerfectConsonancePenalty;
00230   int CompoundPenalty;
00231   int TenthToOctavePenalty;
00232   int SkipTo8vePenalty;
00233   int SkipFromUnisonPenalty;
00234   int SkipPrecededBySameDirectionPenalty;
00235   int FifthPrecededBySameDirectionPenalty;
00236   int SixthPrecededBySameDirectionPenalty;
00237   int SkipFollowedBySameDirectionPenalty;
00238   int FifthFollowedBySameDirectionPenalty;
00239   int SixthFollowedBySameDirectionPenalty;
00240   int TwoSkipsNotInTriadPenalty;
00241   int BadMelodyPenalty;
00242   int ExtremeRangePenalty;
00243   int LydianCadentialTritonePenalty;
00244   int LowerNeighborPenalty;
00245   int UpperNeighborPenalty;
00246   int OverTwelfthPenalty;
00247   int OverOctavePenalty;
00248   int SixthLeapPenalty;
00249   int OctaveLeapPenalty;
00250   int BadCadencePenalty;
00251   int DirectPerfectOnDownbeatPenalty;
00252   int RepetitionOnUpbeatPenalty;
00253   int DissonanceNotFillingThirdPenalty;
00254   int UnisonDownbeatPenalty;
00255   int TwoRepeatedNotesPenalty;
00256   int ThreeRepeatedNotesPenalty;
00257   int FourRepeatedNotesPenalty;
00258   int LeapAtCadencePenalty;
00259   int NotaCambiataPenalty;
00260   int NotBestCadencePenalty;
00261   int UnisonOnBeat4Penalty;
00262   int NotaLigaturePenalty;
00263   int LesserLigaturePenalty;
00264   int UnresolvedLigaturePenalty;
00265   int NoTimeForaLigaturePenalty;
00266   int EighthJumpPenalty;
00267   int HalfUntiedPenalty;
00268   int UnisonUpbeatPenalty;
00269   int MelodicBoredomPenalty;
00270   int SkipToDownBeatPenalty;
00271   int ThreeSkipsPenalty;
00272   int DownBeatUnisonPenalty;
00273   int VerticalTritonePenalty;
00274   int MelodicTritonePenalty;
00275   int AscendingSixthPenalty;
00276   int RepeatedPitchPenalty;
00277   int NotContraryToOthersPenalty;
00278   int NotTriadPenalty;
00279   int InnerVoicesInDirectToPerfectPenalty;
00280   int InnerVoicesInDirectToTritonePenalty;
00281   int SixFiveChordPenalty;
00282   int UnpreparedSixFivePenalty;
00283   int UnresolvedSixFivePenalty;
00284   int AugmentedIntervalPenalty;
00285   int ThirdDoubledPenalty;
00286   int DoubledLeadingTonePenalty;
00287   int DoubledSixthPenalty;
00288   int DoubledFifthPenalty;
00289   int TripledBassPenalty;
00290   int UpperVoicesTooFarApartPenalty;
00291   int UnresolvedLeadingTonePenalty;
00292   int AllVoicesSkipPenalty;
00293   int DirectToTritonePenalty;
00294   int CrossBelowBassPenalty;
00295   /* I added the following during the translation to C */
00296   int CrossAboveCantusPenalty;
00297   int NoMotionAgainstOctavePenalty;
00298   int SpecialSpeciesCheck(int Cn, int Cp, int v, int Other0, int Other1, int Other2, int NumParts,
00299                           int Species, int MelInt, int Interval, int ActInt, int LastIntClass, int Pitch, int LastMelInt, int CurLim);
00300   enum
00301     {
00302       INTERVALS_WITH_BASS_SIZE = 8
00303     };
00304   int IntervalsWithBass[INTERVALS_WITH_BASS_SIZE];
00305   /* 0 = octave, 2 = step, 3 = third, 4 = fourth, 5 = fifth, 6 = sixth, 7 = seventh */
00306   void AddInterval(int n);
00307   int OtherVoiceCheck(int Cn, int Cp, int v, int NumParts, int Species, int CurLim);
00308   int Check(int Cn, int Cp, int v, int NumParts, int Species, int CurLim);
00309   int BestFitPenalty,MaxPenalty,Branches,AllDone;
00310   float PenaltyRatio;
00311   enum
00312     {
00313       NumFields = 16,
00314       Field = (MostVoices_+1),
00315       EndF = (Field*NumFields)
00316     };
00317   int SaveIndx(int indx, int *Sp);
00318   void SaveResults(int CurrentPenalty, int Penalty, int v1, int Species);
00319 #if !defined(SWIG)
00320   static int Indx[17];
00321 #endif
00322   int Look(int CurPen, int CurVoice, int NumParts, int Species, int Lim, int *Pens, int *Is, int *CurNotes);
00323   void BestFitFirst(int CurTime, int CurrentPenalty, int NumParts, int Species, int BrLim);
00324   void FillRhyPat();
00325   float RANDOM(float amp);
00326   void UsedRhy(int n);
00327   int CurRhy(int n);
00328   void CleanRhy();
00329   int GoodRhy();
00330   void counterpoint(int OurMode, int *StartPitches, int CurV, int CantusFirmusLength, int Species, int *cantus);
00331   void AnySpecies(int OurMode, int *StartPitches, int CurV, int CantusFirmusLength, int Species);
00332   void fillCantus(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7, int c8, int c9, int c10, int c11, int c12, int c13, int c14);
00333   void toCsoundScore(std::string filename, double secondsPerPulse);
00334   void winners(int v1, int *data, int *best, int *best1, int *best2, int *durs);
00335 #if !defined(SWIG)
00336   static boost::mt19937 mersenneTwister;
00337   boost::variate_generator<boost::mt19937, boost::uniform_real<> > *uniform_real_generator;
00338 #endif
00339 };
00340 
00341 #endif
00342 

Generated on Sun Nov 9 00:04:51 2008 for Csound and CsoundAC by  doxygen 1.5.6