/* Spectroscopic Toolkit version 1.93 by Pieter Suurmond, 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. Using the binary database-file 'gfall.pbin', produced by the 'pconv'-program that reads Robert Kurucz's latest data file. More Hydrogen studies (based on earlier ST.c03_H). */ #include /* Standard headers. */ #include #include #include "ST_wavelets.h" /* Prototype addWAVELET(). */ #include "ST_pconv.h" /* Defines structure of the binary database-file. */ #include "gt.h" /* For gtInitHertzToRGBtables() and gtHertzToRGB(). */ #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. */ /*-------------------- RGB-values for video: */ float RGBaccu[3]; } c05_STATISTICS; /* For duration- and amplitude-compensation. */ /*---------------------------------------------------------------------------------------------*/ #define kc03_H 1 /* Creates nice 'hydrogen-atmosphere' */ short c05_niceHydrogenBirds(long double start, ENGINEp E, FILE* msg) /* with birds and crickets... */ { /* Decaying structure, starts with lower */ FILE *pbinDatabaseFP; /* pitches, ends with high pitches. */ 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, d; long double audioHertz, dur; long repeat; float RGBreceiver[3]; /* To square our RGB-channels before adding. */ c05_STATISTICS sections[kc03_H] = { /* Atomic.num: OveralAmp: (statistics:) */ /* H */ { 1, 2.50, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0} } }; /* was 6.00. */ /* For He, take 2 and 4.00 as OveralAmp, also add 1 to pbinBuff.UPP.J in the top-calculation below. (He is not as nice as H.) */ /*------------------------------------------------------------ Frequency & time transpositions: */ 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.5 * lowestFreq; /* Rise to 1 within a fifth. */ 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; double scaledAvalue, rep; /*------------------------------------------------------------ Open binary version of Kurucz's file. */ pbinDatabaseFP = fopen(kST_bin_base, "rb"); if (pbinDatabaseFP != NULL) { gtInitHertzToRGBtables(); /* Don't forget this, else it'll all be black or random. */ /*------------------------------ 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"); } } /*------------- accumulate lines in RGB-accumulators (squared sum): -------------*/ if ((more.frequency >= 40.0e13) && (more.frequency <= 77.0e13)) { /* 3.1e-9 not tested yet for H. */ gtHertzToRGB((float)more.frequency, 3.1e-9 * more.Avalue, RGBreceiver); sections[n].RGBaccu[0] += RGBreceiver[0] * RGBreceiver[0]; sections[n].RGBaccu[1] += RGBreceiver[1] * RGBreceiver[1]; sections[n].RGBaccu[2] += RGBreceiver[2] * RGBreceiver[2]; } } } /*--------------------- 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 * d * d; /* Highest frequency per element -18.021 dB. */ /* ----------------- Amplitude: ------------------------------------*/ /* Compress amplitude-range (per section) by approx 4th-power-root. */ a *= sections[n].overalAmplitude * pow(0.6e-14 * more.Avalue, 0.26); /*-------------------- Amplitude treshold (23 bit) -----------------*/ if (a >= 1.192093E-7) { /*---------------- Number of repetitions:---------------------*/ scaledAvalue = log(more.Avalue / sections[n].loAmp); /* 0 .. very high. */ scaledAvalue /= log(sections[n].hiAmp / sections[n].loAmp); /* 0 .. 1. */ rep = 1L + (long)(199.0 * scaledAvalue); /* 1 .. 200. */ /*---------------- Duration & starttime: ---------------------------*/ /* Lowest freq of gets unit-duration, highest gets 0.5^4. */ dur = d * d * (0.01 + (0.09 * scaledAvalue)); /* 10 ... 100 milliseconds. */ for (repeat = 1; ((repeat <= rep) && (a >= 1.192093E-7)); repeat++) { if (addWAVELET (E, msg, 0, /* Hz */ audioHertz, /* amp */ a, /* pan */ -0.25 - (0.5 * pbinBuff.LOW.E / ELOWmax), /* left. */ /* top */ start + (4.0 * pbinBuff.LOW.E / EUPPmax) + (0.006 * audioHertz) + 2.0 * (long double)repeat * d * d * (0.5+scaledAvalue) / (pbinBuff.LOW.J), /* dur */ dur, /* env */ kENV_Gaussian, /* syn */ -1)) /* -1 = sync with other oscillators. */ { printf("Illegal wavelet (1)!\n"); e = 3; goto clFile; } if (addWAVELET (E, msg, 0, /* Hz */ audioHertz, /* amp */ a, /* pan */ 0.25 + (0.5 * pbinBuff.UPP.E / EUPPmax), /* right. */ /* top */ start + (4.0 * pbinBuff.UPP.E / EUPPmax) + (0.006 * audioHertz) + 2.0 * (long double)repeat * d * d * (0.5+scaledAvalue) / (pbinBuff.UPP.J), /* dur */ dur, /* env */ kENV_Gaussian, /* syn */ -1)) /* -1 = sync with other oscillators. */ { printf("Illegal wavelet (2)!\n"); e = 4; goto clFile; } a *= 0.99; dur *= 0.99; /* After 200 times, 0.13. */ } linesUsed++; } else { linesSkipped++; if (msg) fprintf(msg, "SKIPPED-AMP %d: Hz=%12.6Lf lggf=%5.2f.\n", atomicNumber, audioHertz, pbinBuff.LOGGF); } } else { linesSkipped++; if (msg) 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) 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; } /*-----------------------------------------------------------------------*/ short c05_HydrogenBirds(ENGINEp E, FILE* msg) /* with birds and crickets. */ { return c05_niceHydrogenBirds(6.00, E, msg); } /*-----------------------------------------------------------------------------------------------------*/ /* Creates nice 'hydrogen-atmosphere' */ short c05_HydrogenBirds2(long double start, ENGINEp E, FILE* msg)/* with birds and crickets... */ { /* Decaying structure, starts with lower */ FILE *pbinDatabaseFP; /* pitches, ends with high pitches. */ pieterPacked pbinBuff; derivedData more; /* See ST.gfall.pbin.c. */ unsigned long linesRead = 0, linesUsed = 0, linesSkipped = 0; short e = 0; double audioHertz, a, dur, d; long repeat; c05_STATISTICS section = /* Atomic.num: OveralAmp: (statistics:) */ /* H */ { 1, 6.00, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0} }; /* For He, take 2 and 4.00 as OveralAmp, also add 1 to pbinBuff.UPP.J in the top-calculation below. (He is not as nice as H.) */ /*------------------------------------------------------------ Frequency & time transpositions: */ double transpositionRatio = pow(2.0, -40.0); /* 40 octaves down. */ /*------------------------------------------------------------ Smoother frequency windowing: */ double lowestFreq = 30.0; /* (trapeziod instead of brickwall) */ double aboveLowestFreq = 2.0 * lowestFreq; /* Rise to 1 within 1 octave. */ 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; double scaledAvalue, rep; /*------------------------------------------------------------ Open binary version of Kurucz's file. */ pbinDatabaseFP = fopen(kST_bin_base, "rb"); if (pbinDatabaseFP) { /*------------------------------ CYCLE #1 (analysis) ------------------*/ printf("Analyzing database '%s'...\n", kST_bin_base); while (fread(&pbinBuff, sizeof(pbinBuff), 1, pbinDatabaseFP)) { if (pbinBuff.ELEM == 100) /* Only Hydrogen, no 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 < section.loFreq) section.loFreq = audioHertz; if (audioHertz > section.hiFreq) section.hiFreq = audioHertz; if (more.Avalue < section.loAmp) section.loAmp = more.Avalue; if (more.Avalue > section.hiAmp) section.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: ------------*/ if (msg) /* AT CYCLE #2 these minima & maxima are used to scale. */ { fprintf(msg, "-------- element %d: --------\n\ loAmp = %.20f; hiAmp = %.8f; Amp-ratio=%.2f.\n\ loFreq = %.6f; hiFreq = %.6f; Freq-ratio=%.2f.\n", section.element, section.loAmp, section.hiAmp, section.hiAmp / section.loAmp, section.loFreq, section.hiFreq, section.hiFreq / section.loFreq); fprintf(msg, "ELOWmin = %.20f; ELOWmax = %.2f.\n\ EUPPmin = %.2f; EUPPmax = %.2f.\n", ELOWmin, ELOWmax, EUPPmin, EUPPmax); } /*---------------- 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 = section.loFreq - section.hiFreq; section.durA = 0.5 / d; /* A=slope and B=offset. */ section.durB = 0.5 - (0.5 * section.hiFreq / d); /*---------------- Linear bass-boost-filter-function (per element): --------------------*/ /* SAME "d" as with duration may be used: highest freq (per element) gets -6.021 dB. */ /*-------------------------------- VIDEO RENDERING (before posting audio-wavelets) ---------*/ /* printf("Rendering video...\n"); Comment out these lines to skip video rendering. */ /* if (renderVideoFromDatabaseH00(sections, dayTime)) Pass pointer to analysis results */ /* { e = 4; goto clFile; } and amount of seconds for 1 day. */ /*-------------------------------- CYCLE #2: Post (audio-)wavelets into linked list. -------*/ rewind(pbinDatabaseFP); printf("Posting wavelets (c05_H())...\n"); if (msg) fprintf(msg, "1/transpositionRatio = %.1f.\n", 1.0 / transpositionRatio); while (fread(&pbinBuff, sizeof(pbinBuff), 1, pbinDatabaseFP)) { if (pbinBuff.ELEM == 100) { moreLineData(&pbinBuff, &more); /* Calc some more data. */ /*----------------------------------------------------------------------*/ audioHertz = more.frequency * transpositionRatio; if ((audioHertz > lowestFreq) && (audioHertz < nyquist)) { /*------------------ Less brickwall-shaped filtering: --------------*/ if (audioHertz < aboveLowestFreq) a = (audioHertz - lowestFreq) / aboveHertz; /* Rise to 1 within 1 octave. */ 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 = (section.durA * audioHertz) + section.durB; /*------------------ Bass boost-filter (per element): --------------*/ a *= d * d * d; /* Highest frequency per element -18.021 dB. */ /* ----------------- Amplitude: ------------------------------------*/ /* Compress amplitude-range (per section) by approx 4th-power-root. */ a *= section.overalAmplitude * pow(1.0e-16 * more.Avalue, 0.26); /*-------------------- Amplitude treshold (23 bit) -----------------*/ if (a >= 1.192093E-7) { /*---------------- Number of repetitions:---------------------*/ scaledAvalue = log(more.Avalue / section.loAmp); /* 0 .. very high. */ scaledAvalue /= log(section.hiAmp / section.loAmp); /* 0 .. 1. */ rep = 1L + (long)(199.0 * scaledAvalue); /* 1 .. 200. */ /*---------------- Duration & starttime: ------------------------------*/ /* Lowest freq of gets unit-duration, highest gets 0.5^4. */ dur = d * d * (0.01 + (0.04 * scaledAvalue)); /* 10 ... 50 milliseconds. */ for (repeat = 1; ((repeat <= rep) && (a >= 1.192093E-7)); repeat++) { if (addWAVELET (E, msg, 0, /* Hz */ audioHertz, /* amp */ a, /* pan */ -0.25 -(0.5 * pbinBuff.LOW.E / ELOWmax), /* left. */ /* top */ start + 2.0 * (double)repeat * d * d * (0.5+scaledAvalue) / (pbinBuff.LOW.J), /* dur */ dur, /* env */ kENV_Gaussian, /* syn */ -1)) /* -1 = sync with other oscillators. */ { printf("Illegal wavelet (1)!\n"); e = 3; goto clFile; } if (addWAVELET (E, msg, 0, /* Hz */ audioHertz, /* amp */ a, /* pan */ 0.25 +(0.5 * pbinBuff.UPP.E / EUPPmax), /* right. */ /* top */ start + 2.0 * (double)repeat * d * d * (0.5+scaledAvalue) / (pbinBuff.UPP.J), /* dur */ dur, /* env */ kENV_Gaussian, /* syn */ -1)) /* -1 = sync with other oscillators. */ { printf("Illegal wavelet (2)!\n"); e = 4; goto clFile; } a *= 0.99; /* Weaker and weaker. */ dur *= 0.99; /* Shorter and shorter. */ } linesUsed++; } else { linesSkipped++; if (msg) fprintf(msg, "SKIPPED-AMP: Hz=%12.6f lggf=%5.2f.\n", audioHertz, pbinBuff.LOGGF); } } else { linesSkipped++; if (msg) fprintf(msg, "SKIPPED-FRQ: Hz=%12.6f lggf=%5.2f.\n", 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; }