/* Spectroscopic Toolkit version 1.93, july 12, 2008. Copyright (c) 2000-2008 - 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. */ #include /* Standard headers. */ #include #include #include "ST_wavelets.h" /* Prototype addWAVELET(). */ #include "ST_pconv.h" #include "c00.h" typedef struct /* Same as in c01_twoWeeks(). */ { int element; double overalAmplitude; /*-------------------- diagnostic: */ double loFreq, /* AUDIO-freq. */ hiFreq, loAmp, /* Hi-Hertz(!), not audio-amplitude yet. */ hiAmp; double durA, durB; /* Slope & offset. Lowest freq->1; highest freq->0.5. */ } c03_STATISTICS; /* For duration- and amplitude-compensation. */ /*-------------------------------------------------------------------------------------*/ #define kc03_H 1 short c03_H(ENGINEp E, FILE* msg) { FILE *pbinDatabaseFP; pieterPacked pbinBuff; derivedData more; /* See ST.gfall.pbin.c. */ unsigned long linesRead = 0, linesUsed = 0, linesSkipped = 0; short e = 0, n; short atomicNumber, ionizationLevel; double a, p, d; long double audioHertz, dur; c03_STATISTICS sections[kc03_H] = { /* Atomic.num: OveralAmp: (statistics:) */ /* H */ { 1, 1.00, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0 } }; /* With increased amplitude, pan may go out of range. peak value 37.61%.with amp=1. We therefore don't maximize. */ /*------------------------------------------------------------ Frequency & time transpositions: */ long double dayTime = 24.0 * 3600.0 / 4096.0; /* Secs per day, transposed 12 octs. */ long double transpositionRatio = pow(2.0, -40.0); /* 40 octaves down. */ /*------------------------------------------------------------ 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; /*------------------------------------------------------------ Energy stats: */ double ELOWmin = 9999999999999999.9, ELOWmax = 0.0, EUPPmin = 9999999999999999.9, EUPPmax = 0.0; long double newTop; /*------------------------------------------------------------ Open binary version of Kurucz's file. */ pbinDatabaseFP = fopen(kST_bin_base, "rb"); if (pbinDatabaseFP != NULL) { /*------------------------------ CYCLE #1 (analysis) ------------------*/ printf("Analyzing database '%s'...\n", kST_bin_base); while (fread(&pbinBuff, sizeof(pbinBuff), 1, pbinDatabaseFP)) { atomicNumber = pbinBuff.ELEM / 100; ionizationLevel = pbinBuff.ELEM - (100 * atomicNumber); n = 0; /* See if it's in our selection. */ while ((sections[n].element != atomicNumber) && (n < kc03_H)) n++; if ((n < kc03_H) && (!ionizationLevel)) /* Skip IONS. */ { 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 < sections[n].loFreq) sections[n].loFreq = audioHertz; if (audioHertz > sections[n].hiFreq) sections[n].hiFreq = audioHertz; if (more.Avalue < sections[n].loAmp) sections[n].loAmp = more.Avalue; if (more.Avalue > sections[n].hiAmp) sections[n].hiAmp = more.Avalue; if (pbinBuff.LOW.E < ELOWmin) ELOWmin = pbinBuff.LOW.E; if (pbinBuff.LOW.E > ELOWmax) ELOWmax = pbinBuff.LOW.E; if (pbinBuff.UPP.E < EUPPmin) EUPPmin = pbinBuff.UPP.E; if (pbinBuff.UPP.E > EUPPmax) EUPPmax = pbinBuff.UPP.E; if (pbinBuff.LOW.E >= pbinBuff.UPP.E) { if (msg) fprintf(msg, "Energy levels swapped!\n\n"); } } } } /*--------------------- Print statistics for the selected metals: ------------*/ for (n=0; n lowestFreq) && (audioHertz < nyquist)) { /*------------------ Less brickwall-shaped filtering: --------------*/ 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 Compensation per element: ---------------*/ /* Only lowest freq of one metal (section) gets unit-duration and unit-amplit., */ /* all higher frequencies sound shorter. loFreq -> 1, hiFreq -> 0.5. */ d = (sections[n].durA * audioHertz) + sections[n].durB; /*------------------ Bass boost-filter (per element): --------------*/ a *= d; /* Highest frequency per element -6.021 dB. */ /* ----------------- Amplitude: ------------------------------------*/ /* Compress amplitude-range (per section) by approx 4th-power-root. */ a *= sections[n].overalAmplitude * pow(0.4e-14 * more.Avalue, 0.26); /*-------------------- Amplitude treshold (23 bit) -----------------*/ if (a >= 1.192093E-7) { /*------------------------ Panning: ---------------------------------------*/ if (pbinBuff.LOW.J == pbinBuff.UPP.J) p = 0.0; else if (pbinBuff.LOW.J > pbinBuff.UPP.J) /* Depend upon quantum-states. */ p = +0.6; else p = -0.6; /*---------------- Duration & starttime: ---------------------------*/ /* Only lowest freq of element gets unit-duration */ dur = 0.5 * dayTime * d; /*------------------------------ ONE FOR THE LOWER ENERGY LEVEL: ----------*/ newTop = 16.6 + (dayTime * pbinBuff.LOW.E / EUPPmax); /* Low levels first. */ if (addWAVELET(E, msg, 0, /* Hz */ audioHertz, /* amp */ a, /* pan */ p + 1.2, /* top */ newTop, /* dur */ dur, /* env */ kENV_Gaussian, /* pha */ 1)) /* -1 = in sync with other oscillators (sounds rough). */ { /* +1 = cosine aligned with envelope (sounds smooth). */ e = 331; goto clFile; } /*------------------------------ AND ONOTHER ONE FOR THE UPPER LEVEL: -----*/ newTop = 16.6 + (dayTime * pbinBuff.UPP.E / EUPPmax); /* Low levels first. */ if (addWAVELET(E, msg, 0, /* Hz */ audioHertz, /* amp */ a, /* pan */ p, /* top */ newTop, /* dur */ dur, /* env */ kENV_Gaussian, /* pha */ 1)) { e = 332; goto clFile; } linesUsed++; } else { linesSkipped++; if (msg != NULL) fprintf(msg, "SKIPPED-AMP %d: Hz=%12.6Lf lggf=%5.2f.\n", atomicNumber, audioHertz, pbinBuff.LOGGF); } } else { linesSkipped++; if (msg != NULL) fprintf(msg, "SKIPPED-FRQ %d: Hz=%12.6Lf lggf=%5.2f.\n", atomicNumber, audioHertz, pbinBuff.LOGGF); } printLineEssential(&pbinBuff, msg); /* stdout or logFP. */ printLineDerived(&more, msg); } linesRead++; } if (msg != NULL) fprintf(msg, "linesUsed = %ld.\nlinesSkipped = %ld.\n", linesUsed, linesSkipped); if (linesRead != kKuruczLinesTotal) { printf("Error reading binary database lines!\n"); e = 2; } clFile: fclose(pbinDatabaseFP); } else { if (msg != NULL) fprintf(msg, "Cannot open binary database-file '%s'!\n", kST_bin_base); e = 1; } return e; }