fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <random>
  4. #include <cstdint>
  5. #include <vector>
  6. #include <limits>
  7. #include <fstream>
  8. #include <algorithm>
  9. #include <tuple>
  10.  
  11. ///Special Thanks. SoftTuner:http://w...content-available-to-author-only...o.jp/vpack/browse/person/an027069.html
  12.  
  13. typedef std::vector<std::int16_t> DType;
  14. typedef std::tuple<double, double> PType;
  15. typedef std::vector<PType> PVec;
  16. typedef std::tuple<std::uint64_t,std::uint64_t, PVec> RUI;//length,wavetype,effect param.by ui.
  17.  
  18. static const std::uint64_t SampleRate = 44100;
  19. static const double CodeHz = 110;//code A.
  20. static const double Resolution = 100;//must set 10^N.
  21.  
  22. DType MakeVector(std::uint64_t Hz, std::uint64_t millisec) {
  23. std::size_t DS = (1 * Hz * 1)*(static_cast<double>(millisec/1000));//1ch*SampleRate*16bit*1count.
  24. return DType(DS);
  25. }
  26.  
  27. bool MakeSinWave(DType& D, double Hz) {
  28. double PI = 3.14159265358979323846;// from wikipedia jp.
  29. //double Hz = CodeHz*Octrve;
  30. double Div = SampleRate / Hz;//samplerate of 1hz
  31. double Rad = (2*PI) / Div;//make 1hz delta.
  32.  
  33. for(std::size_t i=0;i<D.size();i++){
  34. D[i] = std::sin(Rad*std::fmod(i,Div+std::numeric_limits<double>::epsilon()*2))*std::numeric_limits<DType::value_type>::max();
  35. }
  36. return true;
  37. }
  38. bool MakeSquareWave(DType& D, double Hz) {
  39. //double Hz = CodeHz*Octrve;
  40. double A = SampleRate / Hz;
  41. double E = 1/ (A/2);
  42. int S = 1;
  43. std::size_t j = 0;
  44. for(std::size_t i=0;i<D.size();i++){
  45. D[i] = (std::numeric_limits<DType::value_type>::max()*S)-S;
  46. if ((E*j) >= 1) {
  47. j = 0;
  48. S *= -1;
  49. }
  50. j++;
  51. }
  52. return true;
  53. }
  54. bool MakeTriangleWave(DType& D, double Hz) {
  55. //double Hz = CodeHz*Octrve;
  56. double A = SampleRate / Hz;//sample of 1hz
  57. double B = std::numeric_limits<DType::value_type>::max()*2/(A/2);
  58. double C = 0;
  59. int S = 1;
  60.  
  61. for (std::size_t i=0;i<D.size();i++){
  62. D[i] = C ;
  63. if ((C + (B*S)) >= std::numeric_limits<DType::value_type>::max()) S *= -1;
  64. if ((C + (B*S)) <= std::numeric_limits<DType::value_type>::min()) S *= -1;
  65. C += (B*S);
  66. }
  67. return true;
  68. }
  69. bool MakeSawWave(DType& D,double Hz) {
  70. //double Hz = CodeHz*Octrve;
  71. double A = SampleRate / Hz;//sample of 1hz
  72. double B = std::numeric_limits<DType::value_type>::max()*2/A;
  73. double C = 0;
  74.  
  75. for (std::size_t i=0;i<D.size();i++){
  76. D[i] = C ;
  77. if ((C + B) >= std::numeric_limits<DType::value_type>::max()) C = std::numeric_limits<DType::value_type>::min();
  78. C += B;
  79. }
  80. return true;
  81. }
  82. bool MakeHzNoise(DType& D, double Hz) {
  83. std::random_device rd;
  84. std::mt19937 mt(rd());
  85. std::uniform_int_distribution<> uid(0, std::numeric_limits<DType::value_type>::max() * 2);
  86. // double Hz = CodeHz*Octrve;
  87. double A = SampleRate / Hz;
  88. double B = uid(mt) - std::numeric_limits<DType::value_type>::max();
  89. double E = 1 / (A / 2);
  90. double i = 0;
  91. int S = 1;
  92. std::size_t j = 0;
  93. for(std::size_t i=0;i<D.size();i++){
  94. D[i] = B;
  95. j++;
  96. if ((E*j) >= 1) {
  97. j = 0;
  98. B = uid(mt) - std::numeric_limits<DType::value_type>::max();
  99. }
  100. }
  101. return true;
  102. }
  103. bool MakePerfectNoise(DType& D, std::uint8_t Octrve = 1) {
  104. std::random_device rd;
  105. std::mt19937 mt(rd());
  106. std::uniform_int_distribution<> uid(0, std::numeric_limits<DType::value_type>::max() * 2);
  107. for(std::size_t i=0;i<D.size();i++){
  108. D[i] = uid(mt) - std::numeric_limits<DType::value_type>::max();
  109. }
  110. return true;
  111. }
  112. RUI ShowUI() {
  113. PVec V;
  114. std::uint64_t ms;
  115. double Pos = 0;
  116. double Vol = 0;
  117. double OP = 0;
  118. std::uint8_t A = 0;
  119. std::uint64_t WT = 1;
  120. std::cout << "Welcome Pulse Generator!" << std::endl;
  121. std::cout << "Auther is this need for SONY ACID." << std::endl;
  122. std::cout << "Warning:output file is EASY to CRASH your EAR!!!" << std::endl;
  123. std::cout << "Warning:Becareful for using. and the use under YOUR OWN RISK." << std::endl;
  124. std::cout << std::endl;
  125. std::cout << "I have some question. plz answer." << std::endl;
  126. std::cout << "How Length Are you need? (millisec)" << std::endl;
  127. std::cin >> ms;
  128.  
  129. std::cout << "Ask Wave Type." << std::endl;
  130. std::cout << "1:Sin 2:Square 3:Triangle 4:Saw 5:HzNoise 6:PerfectNoise" << std::endl;
  131. std::cin >> WT;
  132.  
  133.  
  134. std::cout << "Are your use my effecter?:Yes => input y." << std::endl;
  135. std::cin >> A;
  136. if (!(A == 'y' || A=='Y'))return std::make_tuple(ms,WT, V);
  137.  
  138. V.push_back(std::make_tuple(Pos, Vol));
  139.  
  140. while (A == 'y' || A == 'Y') {
  141. std::cout << "Set This Time's End Position:" << OP << "<->"<<Resolution<<"." << std::endl;
  142. std::cin >> Pos;
  143. Pos = std::max(OP, Pos);
  144. Pos = std::min(Resolution, Pos);
  145. OP = Pos;
  146. std::cout << "Set This Time's Sample Level:0<->"<<Resolution<<"." <<"Now:"<<Vol<< std::endl;
  147. std::cin >> Vol;
  148. Vol = std::max(0.0, Vol);
  149. Vol = std::min(Resolution, Vol);
  150.  
  151. V.push_back(std::make_tuple(Pos, Vol));
  152.  
  153. if (Pos >= Resolution) break;
  154.  
  155. std::cout << "Continue?:Yes => input y." << std::endl;
  156. std::cin >> A;
  157. }
  158. std::cout << "End Of Question." << std::endl;
  159. std::cout << std::endl;
  160. std::cout << "*Info*" << std::endl;
  161. std::cout << "Pos,Level" << std::endl;
  162. for (auto&o : V) {
  163. std::tie(Pos, Vol) = o;
  164. std::cout << Pos << "," << Vol << std::endl;
  165. }
  166. return std::make_tuple(ms,WT, V);
  167. }
  168.  
  169.  
  170. bool WriteWaveFile(DType& D,std::uint64_t Hz, std::uint16_t Channel,const char* Name="out.wav") {
  171. std::ofstream ofs(Name, std::ios::binary);
  172. char RIFF[] = "RIFF";
  173. char WAVE[] = "WAVE";
  174. char fmt[] = "fmt ";
  175. char data[] = "data";
  176. std::uint32_t FSize = (44 - 8) + (D.size() * sizeof(DType::value_type));
  177. std::uint32_t ui32 = 0;
  178. std::uint16_t ui16 = 0;
  179. ofs.write(RIFF, 4);
  180. ofs.write(reinterpret_cast<const char*>(&FSize), 4);//fsize-8.
  181. ofs.write(WAVE, 4);
  182. ofs.write(fmt, 4);
  183. ui32 = 16;
  184. ofs.write(reinterpret_cast<const char*>(&ui32), 4);//fmt chunk size
  185. ui16 = 1;
  186. ofs.write(reinterpret_cast<const char*>(&ui16), 2);//sound type. PCM = 1.
  187. ui16 = Channel;
  188. ofs.write(reinterpret_cast<const char*>(&ui16), 2);//Channnel count.
  189. ui32 = static_cast<std::uint32_t>(Hz);
  190. ofs.write(reinterpret_cast<const char*>(&ui32), 4);//Sampling Hz.
  191. ui32 = static_cast<std::uint32_t>(Hz*Channel*(sizeof(DType::value_type)));
  192. ofs.write(reinterpret_cast<const char*>(&ui32), 4);//data speed??
  193. ui16 = Channel*(sizeof(DType::value_type));
  194. ofs.write(reinterpret_cast<const char*>(&ui16), 2);//BlockSize??
  195. ui16 = (sizeof(DType::value_type) * 8);
  196. ofs.write(reinterpret_cast<const char*>(&ui16), 2);//Sampling Bit.
  197. ofs.write(data, 4);
  198. ui32 = static_cast<std::uint32_t>(D.size()*sizeof(DType::value_type));
  199. ofs.write(reinterpret_cast<const char*>(&ui32), 4);//Sample's ALL SIZE.max at 32bit.
  200.  
  201. ofs.write(reinterpret_cast<const char*>(&D[0]), D.size()*sizeof(DType::value_type));//WriteSamples.
  202.  
  203. return true;
  204. }
  205. bool MonoToStereo(DType& D) {
  206. DType St;
  207. for (auto& o : D) {
  208. St.push_back(o);
  209. St.push_back(o);
  210. }
  211.  
  212. D = St;
  213. return true;
  214. }
  215.  
  216. DType Factory(std::uint64_t Type, std::uint64_t ms) {
  217. std::uint8_t Oct = 3;
  218. DType D=MakeVector(SampleRate,ms);
  219.  
  220.  
  221. switch (Type) {
  222. case 1:
  223. MakeSinWave(D,CodeHz * Oct);
  224. break;
  225. case 2:
  226. MakeSquareWave(D,CodeHz * Oct);
  227. break;
  228. case 3:
  229. MakeTriangleWave(D,CodeHz *Oct);
  230. break;
  231. case 4:
  232. MakeSawWave(D,CodeHz *Oct);
  233. break;
  234. case 5:
  235. MakeHzNoise(D,CodeHz *Oct);
  236. break;
  237. case 6:
  238. MakePerfectNoise(D,CodeHz *Oct);
  239. break;
  240. default:
  241. MakeSinWave(D,Oct);
  242. break;
  243. }
  244. return D;
  245. }
  246.  
  247. bool Effecter(DType& D, PVec& P) {
  248. if (D.size() == 0) return false;
  249. std::size_t PosA = 0;
  250. std::size_t PosB = 0;
  251. double Pos = 0;
  252. double Vol = 0;
  253. double VolA= 0;
  254. double VolB = 0;
  255. double VolD = 0;
  256.  
  257. for (std::size_t i = 1; i < P.size(); i++) {
  258. std::tie(Pos, Vol) = P[i];
  259. PosA = D.size()*(Pos / Resolution);
  260. VolA = Vol/Resolution;
  261. std::tie(Pos, Vol) = P[i - 1];
  262. PosB = D.size()*(Pos / Resolution);
  263. VolB = Vol/Resolution;
  264. VolD = (VolA - VolB) / (PosA - PosB);
  265. for (std::size_t j = PosB; j < PosA; j++) {
  266. Vol = (VolB + ((VolD)*(j - PosB)));
  267. D[j] *= Vol;
  268. }
  269. }
  270. return true;
  271. }
  272. DType MakeVector1Hz(std::size_t Hz) {
  273. std::size_t DS = SampleRate/Hz;
  274. return DType(DS);
  275. }
  276. bool MakeSinWave1Hz(DType& D) {
  277. double PI = 3.14159265358979323846;// from wikipedia jp.
  278. double Delta = 360.0 / static_cast<double>(D.size()-1);
  279. double Rad = (2 * PI) / Delta;
  280. std::size_t i = 0;
  281.  
  282. for (std::size_t i = 0; i < D.size(); i++) {
  283. D[i] = std::sin(Rad*std::fmod(i, Delta + std::numeric_limits<double>::epsilon() * 2))*std::numeric_limits<DType::value_type>::max();
  284. }
  285. return true;
  286. }
  287. bool MakeSquareWave1Hz(DType& D) {
  288. double A = D.size();
  289. double E = 1/ (A/2);
  290. int S = 1;
  291. std::size_t j = 0;
  292. for(std::size_t i=0;i<D.size();i++){
  293. D[i] = (std::numeric_limits<DType::value_type>::max()*S)-S;
  294. if ((E*j) >= 1) {
  295. j = 0;
  296. S *= -1;
  297. }
  298. j++;
  299. }
  300. return true;
  301. }
  302. bool MakeSawWave1Hz(DType& D) {
  303. double A = D.size();
  304. double B = std::numeric_limits<DType::value_type>::max() * 2 / A;
  305. double C = 0;
  306.  
  307. for (std::size_t i = 0; i<D.size(); i++) {
  308. D[i] = C;
  309. if ((C + B) >= std::numeric_limits<DType::value_type>::max()) C = std::numeric_limits<DType::value_type>::min();
  310. C += B;
  311. }
  312. return true;
  313. }
  314. bool MakeTriangleWave(DType& D) {
  315. double A = D.size();
  316. double B = std::numeric_limits<DType::value_type>::max() * 2 / (A / 2);
  317. double C = 0;
  318. int S = 1;
  319.  
  320. for (std::size_t i = 0; i<D.size(); i++) {
  321. D[i] = C;
  322. if ((C + (B*S)) >= std::numeric_limits<DType::value_type>::max()) S *= -1;
  323. if ((C + (B*S)) <= std::numeric_limits<DType::value_type>::min()) S *= -1;
  324. C += (B*S);
  325. }
  326. return true;
  327. }bool MakeHzNoise1Hz(DType& D, double Hz) {
  328. std::random_device rd;
  329. std::mt19937 mt(rd());
  330. std::uniform_int_distribution<> uid(0, std::numeric_limits<DType::value_type>::max() * 2);
  331. double A = D.size();
  332. double B = uid(mt) - std::numeric_limits<DType::value_type>::max();
  333. double E = 1 / (A / 2);
  334. double i = 0;
  335. int S = 1;
  336.  
  337. for(std::size_t i=0;i<D.size();i++){
  338. D[i] = B;
  339. i++;
  340. if ((E*i) >= 1) {
  341. i = 0;
  342. B = uid(mt) - std::numeric_limits<DType::value_type>::max();
  343. }
  344. }
  345. return true;
  346. }
  347. bool MakePerfectNoise1hz(DType& D) {
  348. std::random_device rd;
  349. std::mt19937 mt(rd());
  350. std::uniform_int_distribution<> uid(0, std::numeric_limits<DType::value_type>::max() * 2);
  351. for(std::size_t i=0;i<D.size();i++){
  352. D[i] = uid(mt) - std::numeric_limits<DType::value_type>::max();
  353. }
  354. return true;
  355. }
  356. DType FM(DType& D,DType M,double FirstTheta,double SecandTheta,double Time) {
  357. DType R;
  358. double PI = 3.14159265358979323846;
  359. double Rad = 180.0 / PI;
  360.  
  361. for (std::size_t i = 0; i < D.size(); i++) {
  362. double T = (M[i % M.size()]/static_cast<double>(std::numeric_limits<DType::value_type>::max())) * std::sin((FirstTheta*Rad) + (SecandTheta*Rad)*i)*(i/static_cast<double>(D.size()));
  363. R.push_back(D[i] * T);
  364. }
  365.  
  366. return R;
  367.  
  368. }
  369. /** /
  370. DType FM(DType& D,double FirstTheta,double SecandTheta,double Time) {
  371. //DType M = MakeVector1Hz(SecandTheta);
  372. DType M = MakeVector(SampleRate*(SecandTheta/360.0),1000);
  373. DType R;
  374. double PI = 3.14159265358979323846;
  375. double Rad = 180.0 / PI;
  376.  
  377. Make1HzSinWave(M);
  378.  
  379. for (std::size_t i = 0; i < D.size(); i++) {
  380. //double T = (M[i % M.size()]/static_cast<double>(std::numeric_limits<DType::value_type>::max())) * std::sin((FirstTheta*Rad) + (SecandTheta*Rad)*i)*Time;
  381. double T = (D[i % D.size()]/static_cast<double>(std::numeric_limits<DType::value_type>::max())) * std::sin((FirstTheta*Rad) + (SecandTheta*Rad)*i)*(i/static_cast<double>(D.size()));
  382. R.push_back(D[i] * T);
  383. }
  384.  
  385. return R;
  386.  
  387. }
  388. /**/
  389. bool Test() {
  390. DType D = MakeVector(SampleRate, 1000);
  391.  
  392. MakeSinWave(D, CodeHz * 3);
  393. WriteWaveFile(D, SampleRate, 1, "Sin.wav");
  394. MakeSawWave(D, CodeHz * 3);
  395. WriteWaveFile(D, SampleRate, 1, "Saw.wav");
  396. MakeSquareWave(D, CodeHz * 3);
  397. WriteWaveFile(D, SampleRate, 1, "Square.wav");
  398. MakeTriangleWave(D, CodeHz * 3);
  399. WriteWaveFile(D, SampleRate, 1, "Triangle.wav");
  400. MakeHzNoise(D, CodeHz * 3);
  401. WriteWaveFile(D, SampleRate, 1, "HzNoize.wav");
  402. MakePerfectNoise(D, CodeHz * 3);
  403. WriteWaveFile(D, SampleRate, 1, "ParfectNoize.wav");
  404.  
  405. return true;
  406. }
  407. int main() {
  408.  
  409. DType D = MakeVector(SampleRate, 1000);
  410. MakeTriangleWave(D,8);
  411. DType M = MakeVector1Hz(2);
  412. MakeSquareWave1Hz(M);
  413. //WriteWaveFile(D, SampleRate, 1, "Sin.wav");
  414. DType R = FM(D,M, 0, 1, 1);
  415. //WriteWaveFile(R, SampleRate, 1, "FM.wav");
  416.  
  417. // Test();
  418. /* * /
  419. RUI R = ShowUI();
  420. DType D = Factory(std::get<1>(R),std::get<0>(R));
  421. Effecter(D, std::get<2>(R));
  422. MonoToStereo(D);
  423. WriteWaveFile(D, SampleRate, 2);
  424.  
  425. /**/
  426. return 0;
  427.  
  428. }
Success #stdin #stdout 0s 3612KB
stdin
Standard input is empty
stdout
Standard output is empty