/* Spectroscopic Toolkit version 1.92 by Pieter Suurmond, september 7, 2006. Copyright (c) 2000-2006 - Pieter Suurmond 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. Any person wishing to distribute modifications to the Software is requested to send the modifications to the original developer so that they can be incorporated into the canonical version. 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. This algorithm renders the periodic table, from hydrogen (1) up to xenon (54). It actually is another 'Mendelejev progression' (like c04_Mendelejev.c) but I call this piece 'Xenofobia' because it ends with xenon, and is supposed to sound terrifying. The sound track contains large gaps and silences which are meant to be filled with a melodic improvisation on theremin, violin, etc. We start with hydrogen, which belongs to 'the known', we are familiar with its' melodies and harmonies. We end with xenon, which is what we fear: the unknown, strange things in the future, (possible death?). v1: Plays the elements, one after the other, with some overlap though, ordered on atomic number. Duration per element is controlled by variable "dayTime". Also plays all available ions, but a little weaker and little shorter. Each spectral line gets 2 wavelets and both top-times are determined by the 2 energy-levels of the spectral line. Top-times are also affected by strength of the line: the weaker the earlier. Furthermore things like: higher tones have shorter durations and are not as loud as lower tones, panning dependant on quantum-states, etc. Mostly "sound- design", making the result less "painful", less harsh, less dissonant. Care: even decreasing one of the EAMPS below may cause overflow! (Tones may be cancelling out now, and decreasing an amplitude may give an increased result.) A new attempt to 'play' the periodic table, more interestingly. Playing 5 periods, of 18 groups, from H, up to Xe. Starttimes based on position in the periodic table, 5 times 18, like this: H He Li Be B C N O F Ne Na Mg Al Si P S Cl Ar K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe There is an enormous gap (of 16 elements) between H and He which can be filled by elongating the lower elements, especially hydrogen. Probably a hyperbolic mapping-function for duration or smearing will do, for example: duration = 1 / element Then H takes twice the length of He. Maybe even more drastic duration decrease is needed at the beginning. Or, we may use some kind of repetition-factor wich drastically decreases during the first elements. When dayTime = 10.546875, total time (90 element times) is about 16 minutes. Position in the periodic table determines starttime or toptime. Pitch affects both amplitude and length: higher pitches are less loud and have shorter durations. We've never had problems in inventing panning rule, let's forget that for a while. Which kind of envelope? Ions exponentials, atoms gaussians. Less important (i.e. strong) lines should be placed at sections where it is relatively silent so they can be heard, as very short individual granes. So LOGGF should determine/influence starttime. We must try to somehow seperate frequencies that are very near... The 'rules' much change so that it stays 'interesting' to go down the periodic table up to xenon. Earlier experiments (simple Mendelejev progression) proved that it becomes a bit boring after 20 elements or so because spectromorphologic rules do not evolve over time. Group 17 (I use my own numbering here, from 0-17 incl.), the precious gasses, get special treatment: The get exponentially growing envelopes, tops located near the end instead of near the beginning of the element. Exept for its' ions, these get Gaussian envelopes now, whereas ions normally get exponentially decaying exponentials. Rules for group 17 are thus kind of 'reversed'. */ #include /* Standard headers. */ #include #include #include "ST_wavelets.h" /* Prototype addWAVELET(). */ #include "ST_pconv.h" /* Defines structure of the binary database-file. */ #include "c00.h" /*-----------------------------------------------------------------------------------------*/ short determineMendelejevPosition(short element, /* Supply element number 1 to 54 incl. */ short* group, /* Reveives group number 0 to 17 incl. */ short* period) /* Reveives period number 0 to 4 incl. */ { /* Used to calculate starttimes of elements and separate X,Y positions for other mappings. */ if (element < 1) return -1; /* -1 means out of range error. */ if (element < 3) /* Below Li. */ { *period = 0; if (element == 1) *group = 0; else *group = 17; /* 17 = edelgassen. */ return 0; } else if (element < 11) /* Below Na. */ { *period = 1; if (element < 5) /* Below B. */ *group = element-3; /* So Li (element 3) in group 0. */ else *group = element+7; /* So Ne (element 10) in group 17. */ return 0; } else if (element < 19) /* Below K. */ { *period = 2; if (element < 13) /* Below Al. */ *group = element-11; /* So Na (element 11) in group 0. */ else *group = element-1; /* So Ar (element 18) in group 17. */ return 0; } else if (element < 37) /* Below Rb. */ { *period = 3; /* We do not include lanthanum and actinium series (yet). */ *group = element-19; /* So K (element 19) in group 0. */ return 0; } else if (element < 55) /* Below Cs. */ { *period = 4; *group = element-37; /* So Rb (element 37) in group 0. */ return 0; } else return -2; /* -2 means out of range error. */ } /*----------------------------------------------------------------------------------*/ #define kc04X_firstE 1 /* First element (for running tests, to skip elements). */ #define kc04X_lastE 4 /* Last element to include in the run. */ #define kc04X_elems 54 /* For static array-allocation & stats (don't change for running-tests).*/ /*--------------------------------------------------------------------------------------------------*/ short c04_Xenofobia(ENGINEp E, /* Pass pointer to earlier created dsp engine object. */ FILE* msg, /* Any textfile, stdout, or NULL to disable messaging. */ double startTime, /* Starttime in seconds (at least not negative). */ double timeStretch) /* This parameter determines tempo, value > 1 slows it */ { /* down, value < 1 speeds it up. */ /* +1 because we index from 1 instead of 0!!! */ double EAMPS[kc04X_elems + 1] = { /* Linear amplitudes (a little "sound design"): */ /*------------------------------------------------------------------------------*/ /* 1..8: (not:) H He Li Be B C N O */ 0.00, 1.20, 1.04, 0.92, 0.94, 1.02, 0.60, 0.62, 0.72, /* 1.20ok 1.04ok 0.92ok 0.94ok 1.02ok 0.60ok 0.62ok 0.72ok */ /* */ /* 9..16: F Ne Na Mg Al Si P S */ 0.96, 0.70, 0.95, 0.90, 0.97, 0.60, 0.90, 0.90, /* 0.96ok 0.70ok 0.95ok 0.90ok 0.97ok 0.60ok 0.90ok 0.90ok */ /* */ /* EAMPS 1 to 16 (inclusive) above are ok, no overflow/underflow */ /* (with A-factor=1.0e-15, somewhere below and when kc04X_elems = 56). */ /*------------------------------------------------------------------------------*/ /* 17..24: Cl Ar K Ca Sc Ti V Cr */ 0.72, 0.56, 0.78, 0.80, 0.80, 0.78, 0.78, 0.80, /* 0.98, 0.90 OVERFLOWS @ 67.5 secs. */ /* 0.86, 0.82 OVERFLOWS @ 67... secs. */ /* 0.84, 0.62 OVERFLOWS @ 67... secs. */ /* 0.86, 0.54 OVERFLOWS @ 67.44 secs. */ /*------------------------------------------------------------------------------*/ /* 25..32: Mn Fe Co Ni Cu Zn Ga Ge */ 0.50, 0.48, 0.50, 0.50, 0.54, 0.80, 0.80, 0.80, /* to be tested on overflows / peak-values... */ /* 25-32 is a heavy job! Logfile is 117 MB, be very very patient. */ /* 0.50, 0.48 Gives Overflow @ Fe @ 1.16 minutes = 69.6 secs. */ /*------------------------------------------------------------------------------*/ /* 33..40: As Se Br Kr Rb Sr Y Zr */ 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, /*------------------------------------------------------------------------------*/ /* 41..48: Nb Mo Tc Ru Rh Pd Ag Cd */ 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, /*------------------------------------------------------------------------------*/ /* 49..56: In Sn Sb Te I Xe Cs Ba */ 1.00, 1.00, 1.00, 1.00, 1.00, 1.00 }; /*---------------------------------------------------------------- Time transposition: ---------*/ double dayTime = timeStretch * 24.0 * 3600.0 / 8192.0; /* Seconds per day, transposed */ /* 13 octaves up. When time- */ /* This may look strange: '1 element per day'. */ /* Stretch=1.0, 10.546875 se- */ /* conds per element. */ /*---------------------------------------------------------------- Frequency transposition: ----*/ double transpositionRatio = pow(2.0, -40.0); /* 40 octaves down. */ /*----------------------------*/ FILE *pbinDatabaseFP; pieterPacked pbinBuff; derivedData more; /* See ST.gfall.pbin.c. */ unsigned long linesRead = 0, linesUsed = 0, linesSkipped = 0; short e = 0; short group, period, env, sync; int atomicNumber, ionizationLevel, swapped = 0; double audioHertz, a, dur, microTop, top, d, durA, durB; double L, Lp, Lq; /* L,Lp,Lq for logarithmic scaling. */ /*------------------------------------------------------------ Smoother frequency windowing: */ double lowestFreq = 20.0; /* (trapeziod instead of brickwall) */ double aboveLowestFreq = 1.25 * lowestFreq; /* Rise to 1 within 1 major third. */ double aboveHertz = aboveLowestFreq - lowestFreq; double nyquist = 0.5*(double)getSamplerateENGINE(E); double belowNyquist = nyquist / 1.2; /* Fall to 0 within 1 minor third. */ double belowHertz = nyquist - belowNyquist; /*------------------------------------------------------------ GLOBAL freqency/amplitude stats: */ double Fmin = 1.0e31, Fmax = 0.0, /* Audio-Hertz! */ Amin = 1.0e31, Amax = 0.0; /*------------------------------------------------------------ Open binary version of Kurucz's file. */ pbinDatabaseFP = fopen(kST_bin_base, "rb"); if (pbinDatabaseFP) { /*------------------------------ CYCLE #1 (analysis) ----------------------------------------*/ printf("c04_Xenofobia() analyzing database '%s'...\n", kST_bin_base); /* FOR ALL ELEMENTS */ if (msg) { fprintf(msg, "kc04X_firstE = %d\n", kc04X_firstE); fprintf(msg, " kc04X_lastE = %d\n", kc04X_lastE); fprintf(msg, "Linear amplitude-weights for these elements (ions included):\n"); for (atomicNumber=kc04X_firstE; atomicNumber<=kc04X_lastE; atomicNumber++) { fprintf(msg, "%3d=%.2f ", atomicNumber, EAMPS[atomicNumber]); if (!(atomicNumber & 7)) fprintf(msg, "\n"); } fprintf(msg, "\n"); } while (fread(&pbinBuff, sizeof(pbinBuff), 1, pbinDatabaseFP)) /* NOT ONLY THE SE- */ { /* LECTED ONES !!! */ atomicNumber = pbinBuff.ELEM / 100; ionizationLevel = pbinBuff.ELEM - (100 * atomicNumber); if (atomicNumber <= kc04X_elems) /* ALL ATOMS & IONS UP TO kc04X_elems. Do NOT use */ { /* kc04X_firstE & kc04X_lastE during F/A-analysis. */ moreLineData(&pbinBuff, &more); /* Calc some more data. */ audioHertz = more.frequency * transpositionRatio; /* Exclude out-of-range-freqs from analysis. */ /* There could slip through some freqs that are too weak! */ if ((audioHertz > lowestFreq) && (audioHertz < nyquist)) { if (audioHertz < Fmin) /* Audio-Hertz min & max for ALL elements & ions included. */ Fmin = audioHertz; if (audioHertz > Fmax) Fmax = audioHertz; if (more.Avalue < Amin) /* A-values (not audio yet). */ Amin = more.Avalue; if (more.Avalue > Amax) Amax = more.Avalue; if ((pbinBuff.LOW.E >= pbinBuff.UPP.E) && (pbinBuff.ELEM != swapped)) { swapped = pbinBuff.ELEM; /* Print at least SOME swapped levels. */ if (msg) fprintf(msg, "Energy levels swapped (element %d)!\n", pbinBuff.ELEM); } } } } /*--------------------- Print statistics: ------------*/ if (msg) fprintf(msg, "\ F and A analysis include ALL elements and ions up to %d (incl):\n\ Fmin = %.2f; Fmax = %.2f.\n\ Amin = %.8f; Amax = %.2f.\n", kc04X_elems, Fmin, Fmax, Amin, Amax); /*---------------- Linear duration compensation function: ------------------------------*/ /* d(loF) = 1 1 = a loF + b a = 0.5 / (loF - hiF) */ /* d(hiF) = 0.5 0.5 = a hiF + b - 0.5 = (0.5 / (loF - hiF)) hiF + b */ /* d(F) = a F + b ------------------- b = 0.5 - (0.5 hiF / (loF - hiF)) */ /* 0.5 = a (loF - hiF) */ d = Fmin - Fmax; durA = 0.5 / d; /* A=slope and B=offset. */ durB = 0.5 - (0.5 * Fmax / d); /*---------------- Linear bass-boost-filter-function (NOT! per element): --------------------*/ /* SAME "d" as with duration may be used: highest freq (per element) gets -6.021 dB. */ /*----------hmmmm, better use the logarithmic scaling for that? ..... (maybe?)... */ /*---------------- Logarithmic duration compensation function: -------------------------*/ /* 1 = Lp + Lq * log(loF) */ /* 0.5 = Lp + Lq * log(hiF) - L(F) = Lp + Lq * log(F) */ /* ------------------------------ */ /* 0.5 = Lq * log(loF / hiF) 1 = Lp + Lq * log(loF) */ /* 1 = Lp + 0.5 * log(loF) / log(loF / hiF) */ /* Lq = 0.5 / log(loF / hiF) Lp = 1 - 0.5 * log(loF) / log(loF / hiF) */ /* */ L = log(Fmin / Fmax); /* Example: When loF = 100Hz and hiF = 10kHz. */ Lq = 0.5 / L; /* Then Lp = 1.5 and Lq = -0.25. */ Lp = 1.0 - (0.5 * log(Fmin) / L); /* So L(F) = 1.5 - 0.25 * log(F). */ /*---------------------------------------- Thus L(100)=1, L(10000)=0.5, L(1000)=0.75. */ if (msg) { fprintf(msg, "Using lin & log frequency-scalings:\n"); fprintf(msg, " linear: d(F) = %.6f + %6f * F\n", durB, durA); fprintf(msg, " logarithmic: L(F) = %.6f + %6f * log(F)\n", Lp, Lq); } /*-------------------------------- CYCLE #2: Post (audio-)wavelets into linked list. -------*/ rewind(pbinDatabaseFP); printf("c04_Xenofobia() posting wavelets...\n"); if (msg) { fprintf(msg, "1/transpositionRatio = %.1f.\n", 1.0 / transpositionRatio); fprintf(msg, "dayTime = %.6f s.\n", dayTime); } while (fread(&pbinBuff, sizeof(pbinBuff), 1, pbinDatabaseFP)) { /*---------------------------------------------------- Get Atomic number and ionization level. */ atomicNumber = pbinBuff.ELEM / 100; ionizationLevel = pbinBuff.ELEM - (100 * atomicNumber); /*---------------------------------------------------- ELEMENT / ION - selector: */ if ((atomicNumber <= kc04X_lastE) && (atomicNumber >= kc04X_firstE)) /* Ions also */ { moreLineData(&pbinBuff, &more); /* Calc some more data. */ audioHertz = more.frequency * transpositionRatio; if ((audioHertz > lowestFreq) && (audioHertz < nyquist)) { /*------------------ Less brickwall-shaped frequency window: ----------------------*/ if (audioHertz < aboveLowestFreq) a = (audioHertz - lowestFreq) / aboveHertz; /* Rise to 1 within 1 minor third. */ else if (audioHertz > belowNyquist) a = (nyquist - audioHertz) / belowHertz; /* Fall to 0 within 1 major third. */ else a = 1.0; /*---------------- Frequency-dependant compensations ---------------------------*/ /* Lowest frequency gets unit value, highest frequency gets half. */ d = (durA * audioHertz) + durB; /* d gives linear slope from 1 to 0.5. */ L = Lp + Lq * log(audioHertz); /* L gives logarithmic slope from 1 to 0.5. */ /*---------------- Bass boost: --------------------------------*/ a *= d * L; /* Lowest freq 0 dB; Highest frequency -12 dB. */ /* linear as well as logarithmic! */ /* ----------------- Amplitude: ------------------------------------*/ /* Compress amplitude-range (per section) by approx 5th-power-root. */ a *= EAMPS[atomicNumber] * pow(1.00e-14 * more.Avalue, 0.2); /* (0.2= quite compressed) */ a /= (double)(1 + ionizationLevel); /* Ions weaker (-6dB for +1) */ /*-------------------- Amplitude treshold (23 bit) -----------------*/ if (a >= 1.192092896E-7) /* (= 1 / 2^23) */ { /*-------------------- Duration: ---------------------------------------------*/ dur = dayTime / audioHertz; /* Length proportianal to wavelength. */ dur *= 8.0 * pow(more.Avalue, 0.05); /* Extremely compressed A-influence. */ dur /= (double)(1 + ionizationLevel); /* Ions shorter (half for +1). */ dur /= atomicNumber; /* Rules 'contract' as we go further. */ /*----------------------------------------------------------------------------*/ /*==================== element starttime (macro-timing): =========*/ if (determineMendelejevPosition(atomicNumber, &group, &period)) { if (msg) fprintf(msg, "determineMendelejevPosition failed!\n"); e = 88; goto clFile; } top = (double)(group + (18 * period)); /*==================== (micro-timing): ========================================*/ microTop = 0.01 * (pbinBuff.LOGGF + 18.0); /* Weaker components earlier. */ microTop += 0.2 * d * L; /* Higher pitches earlier. */ microTop += 0.05 * (double)pbinBuff.LOW.J; microTop /= (double)(1 + ionizationLevel); /* Ions earlier/more together. */ microTop /= atomicNumber; /* Rules 'contract' as we go further. */ top = startTime + dayTime * /* Relative to absolute time. */ ((double)(group + (18 * period)) + microTop); /*==================== Choose envelope: ==========================*/ if (group == 17) /* Precious gases get reverse envelope rules. */ { if (ionizationLevel) env = kENV_Gaussian; /* Gaussian for ions. */ else env = kENV_ExpRise; /* Exponentially rising (near the end). */ } else { if (ionizationLevel) env = kENV_ExpDecay; /* Exponential decay. */ else env = kENV_Gaussian; /* Gaussian for atoms */ } /*==================== Envelope determines oscillator sync-type: =========*/ if (env == kENV_Gaussian) sync = -1; /* -1 = sync with other oscillators. */ else sync = 1; /* 1 = align cosine with envelope. */ /* We use no aligned sines (0) yet. */ if (addWAVELET(E, msg, 0, audioHertz, a, 0.0000, top, dur, env, sync)) /* FRQ, AMP, PAN, TOP, DUR, ENV, PHA. */ { if (msg) fprintf(msg, "Illegal wavelet c04X!\n"); e = 3; goto clFile; } linesUsed++; } else { linesSkipped++; if (msg) fprintf(msg, "SKIPPED-AMP %d: Hz=%12.6f lggf=%5.2f.\n", atomicNumber, audioHertz, pbinBuff.LOGGF); } } else { linesSkipped++; if (msg) fprintf(msg, "SKIPPED-FRQ %d: Hz=%12.6f lggf=%5.2f.\n", atomicNumber, audioHertz, pbinBuff.LOGGF); } printLineEssential(&pbinBuff, msg); /* stdout or logFP. */ printLineDerived(&more, msg); } linesRead++; } if (msg) fprintf(msg, "linesUsed = %ld.\nlinesSkipped = %ld.\n", linesUsed, linesSkipped); if (linesRead != kKuruczLinesTotal) { if (msg) fprintf(msg, "Error reading binary database lines!\n"); e = 2; } clFile: fclose(pbinDatabaseFP); } else { if (msg) fprintf(msg, "Cannot open binary database-file '%s'!\n", kST_bin_base); e = 1; } return e; }