/* Spectroscopic Toolkit version 1.96, november 8, 2011. Copyright (c) 2000-2011 - 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. ST translates spectroscopic (i.e. quantumphysical) data to audiofile. This file may also generates video-frames. */ #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" /* Needed because of video-rendering routine here. */ #include "ST_integers.h" #include "ST_engine.h" /* Private definition of ENGINE struct needed here. */ /* SORRY we'd better not include ST_engine.h here! */ #include "c00.h" /* To check prototypes. */ typedef struct { 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]; } week; /* For duration- and amplitude-compensation. */ /*---------------------------------------------------------------------------------------------------*/ /* Called from c01_twoWeeks(), not from the main()! Comment-out that call there to skip video */ /* rendering. The analysis that is done there is used as basis for the RGB-mixing routine below. */ /* Generates a number of jpeg-images. Put these together (along with the generated audio) in some */ /* other program (for instance "mediaconvert" on Silicon Graphics, or "Quicktime Pro" on Macintosh). */ /* Take care with compression methods: video-RZPA, for example, works not as well as animation-RLE. */ /* Video parameters below (framesPerSecond, jpegQuality, etc) should also be taken to the conversion */ /* software that bakes an MOV, AVI, MPEG of it all. (experiment with different compressors and para- */ /* meters, for the "amorpheous" images that are generated here are difficult to animate flicker-free */ /* on digital computers!) */ short renderVideoFromDatabase00(ENGINEp E, week* wk, double dtime, short NUMSECTS) /* 7 or 14 */ { /*------------- VIDEO PARAMETERS: ---------------------------------------------------------------*/ double framesPerSecond = 30.0000; /* Adjust video parameters here. */ unsigned int pictureWidth = 320, pictureHeight = 240; int jpegQuality = 75; /* 75% quality. */ /*-----------------------------------------------------------------------------------------------*/ RGBPIC PIC; /* Two structs from my "gt-toolkit". */ char jpegname[64]; /* To hold filenames of numbered images. */ short n, c; unsigned int i; /* To index audio-envelope-table,32 bit! */ unsigned int xCenter, yCenter; /* These ints also MUST be 32 bit! */ int xx, yy, dx, dy, maxDistFromCenter, distFromCenter, r; unsigned long vframe = 0; float env, mixedRGB[3]; double time, infl_start, real_start, real_end; unsigned char R, G, B, Rprev=0, Gprev=0, Bprev=0; SIGNED16* gaussTable; /*-----------------------------------------------------------------------------------------------*/ /* Black background. */ if (gtCreateMemRGB(&PIC, pictureWidth, pictureHeight, 0,0,0)) /* Create 24 bit RGB space in memory.*/ return(1); /* Exit immediately on mem-failures. */ xCenter = pictureWidth >> 1; /* Center for our algorithm, later on. */ yCenter = pictureHeight >> 1; maxDistFromCenter = (int)(0.5 + sqrt((double)((xCenter * xCenter) + (yCenter * yCenter)))); /*-------------------------------------------- TIME-FRAME-LOOP: -----------------------------------*/ gaussTable = E->envTables[kENV_Gaussian]; /* Use gaussian envelope for convenience here (dirty). */ do { time = (double)vframe / framesPerSecond; /* In seconds. */ mixedRGB[0] = 0.0; mixedRGB[1] = 0.0; mixedRGB[2] = 0.0; for (n=0; n real_start) && (time < real_end)) { i = (unsigned int)(0.5 + (double)kEnvTableSize * (time - real_start) / (5.0 * dtime)); env = (float)gaussTable[i]/32768.0; /* USE AUDIO DSP-ENVELOPE HERE FOR CONVENIENCE. */ env *= env; /* Also square envelope (theoretically more */ mixedRGB[0] += env * wk[n].RGBaccu[0]; /* convincing but also looks better, otherwise */ mixedRGB[1] += env * wk[n].RGBaccu[1]; /* Gauss-bell is flattened-out, causing too */ mixedRGB[2] += env * wk[n].RGBaccu[2]; /* much overlap in our metal-color-pallette.) */ } } /*------------- check for overflow: -----------------*/ if ((c = (short)(0.5 + sqrt(mixedRGB[0]))) > 254) /* Take square root and round to 8 bits. */ printf("Video-R overflow at frame %ld!\n%c", vframe, (char)7); /* Signal overflow but do not exit (yet)! */ R = (unsigned char)c; if ((c = (short)(0.5 + sqrt(mixedRGB[1]))) > 254) printf("Video-G overflow at frame %ld!\n%c", vframe, (char)7); G = (unsigned char)c; if ((c = (short)(0.5 + sqrt(mixedRGB[2]))) > 254) /* 254 instead of 255 for we have to dither yet. */ printf("Video-B overflow at frame %ld!\n%c", vframe, (char)7); B = (unsigned char)c; /*---------------------------------------------------- X-Y-LOOP: ----------*/ for (xx=0; xx> 1, (G + Gprev + (rand() % 3)) >> 1, /* Color-channel-lowpass & dithering. */ (B + Bprev + (rand() % 3)) >> 1); } } } Rprev = R; Gprev = G; Bprev = B; /*-------------------------------------------------------- Dump numbered file to disk. */ sprintf(jpegname, "video/m%05ld.jpg", vframe); /* Specify SUBDIRECTORY here! */ if (gtSaveMemRGB(PIC, jpegname, jpegQuality)) /* Quality in percents. */ printf("Error saving video-frame \"%s\".\n", jpegname); } while (vframe++ < 11925); /* approx for 30 fps & 17530880 audioframes @44.1. */ gtDestroyMemRGB(&PIC); /* Use other software to put togeteher all */ /* jpeg-frames and the audiofile. */ /* printf("Not really an error, just aborting audio: "); return(1); TO ABORT AUDIO CALC. */ return(0); /* 0 means ok: continue with audio-rendering. */ } /*-------------------------------------------------------------------------------------*/ short c01_twoWeeks(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; double a, p, d; long double audioHertz, dur, inflectStart; float RGBreceiver[3]; /* To square our RGB-channels before adding them. */ /* CARE: THE FOLLOWING AMPLITUDES WON'T CLIP ONLY IF PHASE=-1 (oscill.sync.)! */ week sections[14] = { /* Metal/oxide: Atomic.num: OveralAmp: (statistics:) */ /* Au */ {7900, 0.92, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Ag */ {4700, 1.52, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Fe */ {2600, 0.16, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Hg */ {8000, 1.20, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Sn */ {5000, 1.12, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Cu */ {2900, 0.88, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Pb */ {8200, 1.40, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /*------------- +1 IONS: ----------------------------------------------------------------*/ /* Au+*/ {7901,/*--*/0.00, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Ag+*/ {4701, 1.20, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Fe+*/ {2601, 0.12, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Hg+*/ {8001, 1.10, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Sn+*/ {5001, 1.15, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Cu+*/ {2901, 0.20, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}}, /* Pb+*/ {8201, 1.20, 9.0e99, 0.0, 9.0e99, 0.0, 0.0, 0.0, {0.0, 0.0, 0.0}} }; /* 3.15 3.24 3.26 3.32 3.38! 3.46 3.62 */ float brightness[14] = { 3.22e-9, 3.24e-9, 3.26e-9, 3.30e-9, 3.36e-9, 3.46e-9, 3.62e-9, 3.61e-9, 2.60e-9, 1.64e-9, 1.59e-9, 1.54e-9, 1.49e-9, 1.47e-9 }; /* 3.62e-9 2.63 1.64 1.61 1.59 1.49 1.49 */ /*------------------------------------------------------------ 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; /*------------------------------------------------------------ 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("\"c01_twoWeeks()\" analyzing '%s'...\n", kST_bin_base); while (fread(&pbinBuff, sizeof(pbinBuff), 1, pbinDatabaseFP)) { /* atomicNumber = pbinBuff.ELEM / 100; */ /* ionizationLevel = pbinBuff.ELEM - (100 * atomicNumber); */ n = 0; while ((sections[n].element != pbinBuff.ELEM) && (n < 14)) /* See if it's in our selection. */ n++; if (n < 14) /* Take +1 ions & neutral atoms. */ { 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 in the second step! */ 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; } /*------------- accumulate lines in RGB-accumulators (squared sum): -------------*/ if ((more.frequency >= 40.0e13) && (more.frequency <= 77.0e13)) { /* Tested for non-ions: 3.1e-9 is ok. */ /* For ions 1.49e-9 is ok, 1.5 is too high. */ gtHertzToRGB((float)more.frequency, brightness[n] * 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<14; n++) /* AT CYCLE #2 these minima & maxima are used to scale. */ { if (msg != NULL) fprintf(msg, "-------- element %d: --------\n\ loAmp = %.8f; hiAmp = %.8f; RATIO=%.2f.\nloFreq = %.6f; hiFreq = %.6f; RATIO=%.2f.\n", sections[n].element, sections[n].loAmp, sections[n].hiAmp, sections[n].hiAmp / sections[n].loAmp, sections[n].loFreq, sections[n].hiFreq, sections[n].hiFreq / sections[n].loFreq); /*---------------- 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 = sections[n].loFreq - sections[n].hiFreq; sections[n].durA = 0.5 / d; /* A=slope and B=offset. */ sections[n].durB = 0.5 - (0.5 * sections[n].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("NOT Rendering video...\n"); /* Comment out these lines to skip video rendering. */ /* if (renderVideoFromDatabase00(E, sections, dayTime, 14)) Pass pointer to analysis results */ /* { and amount of seconds for 1 day. */ /* printf("Video rendering failed\n"); e = 4; goto clFile; } */ /*-------------------------------- CYCLE #2: Post (audio-)wavelets into linked list. -------*/ rewind(pbinDatabaseFP); if (msg != NULL) { fprintf(msg, "1/transpositionRatio = %.1Lf.\n", 1.0 / transpositionRatio); fprintf(msg, "dayTime = %.6Lf s.\n", dayTime); fprintf(msg, "Using oscillator sync (phase=-1) to prevent 'glisandi' at Fe and Cu.\n"); } printf("Composing c01_twoWeeks...\n"); 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: */ n = 0; while ((sections[n].element != pbinBuff.ELEM) && (n < 14)) /* See if it's in our selection. */ n++; /* Lookup our "n"-index. */ if (n < 14) /* +1 ions and neutral metals only. */ { 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 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(1.90e-13 * 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.5; else p = -0.5; if (n & 1) /* Alternate between left and right channel. */ p += 0.118; /* Odd ones (Ag, Hg, Cu) slighty to the right. */ else p -= 0.118; /* Even ones (Au, Fe, Sn, Pb) slighty to the left. */ /*---------------- Duration & starttime: ---------------------------*/ /* Only lowest freq of one metal (section) gets unit-duration */ dur = dayTime * d; inflectStart = 44.0 + ((double)n * dayTime); /* 44+: 0 to 180. */ if (addWAVELET(E, msg, 0, /* Hz */ audioHertz, /* amp */ a, /* pan */ p, /* top */ inflectStart + (0.5 * dur), /* All start-inflections together. */ /* dur */ dur, /* Top at inflectstart + DUR/2. */ /* env */ kENV_Gaussian, /* pha */ -1)) /* -1 = sync with other oscills; */ /* 1 = zero-phase cosine at TOP; 0 = zero-phase sine at TOP. */ /* Using phase=1 gives strange descending 'glissandi' at Fe and Cu! */ { if (msg) fprintf(msg, "Illegal wavelet!\n"); e = 3; goto clFile; } linesUsed++; } else { linesSkipped++; if (msg) fprintf(msg, "SKIPPED-AMP %d: Hz=%12.6Lf lggf=%5.2f.\n", pbinBuff.ELEM, audioHertz, pbinBuff.LOGGF); } } else { linesSkipped++; if (msg) fprintf(msg, "SKIPPED-FRQ %d: Hz=%12.6Lf lggf=%5.2f.\n", pbinBuff.ELEM, 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) { if (msg != NULL) fprintf(msg, "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; }