Facebook
From kubasz, 5 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 261
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <unistd.h>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <string>
  8. #include <AL/al.h>
  9. #include <AL/alc.h>
  10.  
  11. using namespace std;
  12.  
  13. #ifndef M_PI
  14. #define M_PI (3.14159265)
  15. #endif
  16. #define null nullptr
  17. #define FREQ (44100)
  18. #define MULV (32768)
  19.  
  20. ALCdevice*  dev;
  21. ALCcontext* ctx;
  22. ALfloat listenerPos[] = {0.0,0.0,0.0};
  23. const ALfloat listenerVel[] = {5.0,0.0,0.0};
  24. const ALfloat listenerOri[] = {0.0,0.0,1.0, 0.0,1.0,0.0};
  25. const ALfloat srcPos[] = {0.0,0.0,1.0};
  26. ALuint srcId,bufId;
  27. float volume=0.75f;
  28.  
  29. double smoothstep(double a)
  30. {
  31.         return 3*a*a - 2*a*a*a;
  32. }
  33.  
  34. int main(int argc, char** argv)
  35. {
  36.         if(argc!=6){puts("Usage: playsine <Seconds> <Hz> <TFadeoff> <Exp> <Amp>");return -1;}
  37.        
  38.         string sT(argv[1]),sF(argv[2]),sExp(argv[4]),sTFO(argv[3]),sAmp(argv[5]);
  39.         int freq = stoi(sF);
  40.         int time = stoi(sT);
  41.         double Vexp = double(stof(sExp));
  42.         double Tfo = double(stof(sTFO));
  43.         double Amp = double(stof(sAmp));
  44.         int FLEN = time * FREQ;
  45.        
  46.         dev = alcOpenDevice(null);
  47.         if(!dev){return -2;}
  48.         ctx = alcCreateContext(dev,null);
  49.         alcMakeContextCurrent(ctx);
  50.         if(!ctx){return -2;}
  51.  
  52.         alListenerfv(AL_POSITION, listenerPos);
  53.         alListenerfv(AL_VELOCITY, listenerVel);
  54.         alListenerfv(AL_ORIENTATION, listenerOri);
  55.         if(alGetError()!=AL_NO_ERROR){return -2;}
  56.        
  57.         alGenSources(1,&srcId);
  58.         if(alGetError()!=AL_NO_ERROR){return -3;}
  59.         alSourcefv(srcId, AL_POSITION, srcPos);
  60.         alGenBuffers(1,&bufId);
  61.         if(alGetError()!=AL_NO_ERROR){return -3;}
  62.        
  63.         vector<short> arr;
  64.         arr.resize(FLEN);
  65.         double Seclim = Tfo;
  66.         int ilim = FLEN-int(FREQ*Tfo);
  67.         for(int i=0;i<FLEN;i++)
  68.         {
  69.                 double t = double(i)/double(FREQ);
  70.                 double tfe=double(FLEN-i)/double(FREQ);
  71.                 double tf = tfe/Seclim;
  72.                 double OT = (2.f*float(M_PI)*freq)/double(FREQ);
  73.                 double Val = sin( OT * i )*MULV*Amp;
  74.                 if(i>ilim)
  75.                 {
  76.                         Val *= pow(tf,Vexp);
  77.                 }
  78.                 Val *= volume;
  79.                 arr[ i ] = short(Val);
  80.         }
  81.        
  82.         alBufferData(bufId,AL_FORMAT_MONO16,arr.data(),FLEN*2,FREQ);
  83.         alSourcei(srcId, AL_BUFFER, bufId);
  84.         alSourcePlay(srcId);
  85.         ALint state;
  86.         //sleep(time);
  87.         do
  88.         {
  89.                 alGetSourcei(srcId,AL_SOURCE_STATE,&state);
  90.                 //listenerPos[0]+=1.0;
  91.                 //alListenerfv(AL_POSITION, listenerPos);
  92.                 usleep(1000);
  93.         }while(state == AL_PLAYING);
  94.        
  95.        
  96.         alDeleteSources(1,&srcId);
  97.         alDeleteBuffers(1,&bufId);
  98.         alcDestroyContext(ctx);
  99.         alcCloseDevice(dev);
  100.         return 0;
  101. }
  102.