szymski

Free memory to sound

Feb 15th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.05 KB | None | 0 0
  1. import std.stdio;
  2. import std.random;
  3. import std.math;
  4. import std.algorithm;
  5. import core.sys.linux.sys.sysinfo;
  6. import core.sys.linux.time;
  7.  
  8. void main()
  9. {
  10.     sysinfo_ info2;
  11.     sysinfo(&info2);
  12.  
  13.     enum freeConst = 0.000001f;
  14.  
  15.     float firstFreeRam = info2.freeram * freeConst;
  16.  
  17.     writeln(firstFreeRam);
  18.  
  19.     enum sampleRate = 8000;
  20.  
  21.     float freq = 0f;
  22.     float nextTime = 0f;
  23.  
  24.     for(int sample = 0; ; sample++) {
  25.         float value = 0f;
  26.         float time = sample / cast(float)sampleRate;
  27.        
  28.         //writeln("Sample:, ", sample, " Time: ", time);
  29.  
  30.         //float freq = sample / 100f + 50f;
  31.        
  32.         if(time > nextTime) {
  33.             nextTime = time + 0.1f;
  34.  
  35.             sysinfo_ info;
  36.             sysinfo(&info);
  37.  
  38.             freq = (info.freeram * freeConst - firstFreeRam) * -0.6f + 500f;
  39.         }
  40.  
  41.         value = sin(2 * PI * time * freq);
  42.  
  43.         //writeln(cast(int)((clamp(value, -1f, 1f) * 0.5f + 0.5f) * 255f));
  44.  
  45.         write(cast(char)cast(int)((clamp(value, -1f, 1f) * 0.5f + 0.5f) * 255f));
  46.  
  47.         if(sample % (sampleRate / 5) == 0) {
  48.             timespec ts;
  49.             ts.tv_nsec = 200_000_000;
  50.             nanosleep(&ts, &ts);
  51.         }
  52.     }
  53. }
Add Comment
Please, Sign In to add comment