main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 
  2. // TODO:
  3. // * Make this project into an application that starts automatically to test GUI and sound after building the Builder build system.
  4. // * Let the user browse a file system and select a location for a new or existing project.
  5. // * Explain how everything works when starting for the first time, using a command line argument.
  6. // * A catalogue of SDK examples with images and descriptions loaded automatically from their folder.
  7. // * Offer one-click build and execution of SDK examples on multiple platforms, while explaining how the building works.
  8. #include "../../DFPSR/includeFramework.h"
  9. #include "sound.h"
  10. using namespace dsr;
  11. // Global
  12. bool running = true;
  13. Window window;
  14. Component mainPanel;
  15. Component toolPanel;
  16. String interfaceContent =
  17. UR"QUOTE(
  18. Begin : Panel
  19. Name = "mainPanel"
  20. Solid = 0
  21. Begin : Panel
  22. Name = "toolPanel"
  23. Color = 180,180,180
  24. Solid = 1
  25. bottom = 50
  26. End
  27. End
  28. )QUOTE";
  29. static const double pi = 3.1415926535897932384626433832795;
  30. static const double cyclesToRadians = pi * 2.0;
  31. static const int toneCount = 9;
  32. int basicTone, boomSound;
  33. int playing[toneCount];
  34. void createTestProject() {
  35. for (int t = 0; t < toneCount; t++) {
  36. playing[t] = -1;
  37. }
  38. // Pure tone
  39. basicTone = generateMonoSoundBuffer(U"sine", 441, 44100, soundFormat_F32, [](double time) -> double {
  40. return sin(time * (cyclesToRadians * 100));
  41. });
  42. // Loaded from file
  43. boomSound = loadSoundFromFile(file_combinePaths(file_getApplicationFolder(), U"Boom.wav"));
  44. }
  45. static EnvelopeSettings envelope = EnvelopeSettings(0.1, 0.2, 0.8, 0.4, 0.1, -0.02, 0.04, 0.5);
  46. static double previewPressTime = 1.0;
  47. static double previewViewTime = 4.0;
  48. DSR_MAIN_CALLER(dsrMain)
  49. void dsrMain(List<String> args) {
  50. printText(U"Input arguments:\n");
  51. for (int a = 0; a < args.length(); a++) {
  52. printText(U" args[", a, "] = ", args[a], U"\n");
  53. }
  54. // Start sound thread
  55. printText(U"Initializing sound\n");
  56. sound_initialize();
  57. // Create something to test
  58. printText(U"Creating test project\n");
  59. createTestProject();
  60. // Create a window
  61. window = window_create(U"Sound generator", 800, 600);
  62. // Load an interface to the window
  63. window_loadInterfaceFromString(window, interfaceContent);
  64. // Find components
  65. mainPanel = window_findComponentByName(window, U"mainPanel");
  66. toolPanel = window_findComponentByName(window, U"toolPanel");
  67. // Bind methods to events
  68. window_setKeyboardEvent(window, [](const KeyboardEvent& event) {
  69. DsrKey key = event.dsrKey;
  70. if (event.keyboardEventType == KeyboardEventType::KeyDown) {
  71. if (key == DsrKey_Escape) {
  72. running = false;
  73. } else if (key >= DsrKey_1 && key <= DsrKey_9) {
  74. int toneIndex = key - DsrKey_1;
  75. printText(U"Start tone ", toneIndex, U"\n");
  76. playing[toneIndex] = playSound(basicTone, true, 0.25, 0.25, 3.0 + toneIndex * 0.25, envelope);
  77. } else if (key == DsrKey_0) {
  78. playSound(boomSound, false, 0.25, 1.0, 1.0);
  79. }
  80. } else if (event.keyboardEventType == KeyboardEventType::KeyUp) {
  81. if (key >= DsrKey_1 && key <= DsrKey_9) {
  82. int toneIndex = key - DsrKey_1;
  83. printText(U"End tone ", toneIndex, U"\n");
  84. releaseSound(playing[toneIndex]); // Soft stop with following release
  85. } else if (key == DsrKey_Space) {
  86. stopAllSounds();
  87. }
  88. } else if (event.keyboardEventType == KeyboardEventType::KeyType) {
  89. String message;
  90. string_append(message, U"Typed ");
  91. string_appendChar(message, event.character);
  92. string_append(message, " of code ", event.character, "\n");
  93. printText(message);
  94. }
  95. });
  96. /*
  97. component_setMouseDownEvent(mainPanel, [](const MouseEvent& event) {
  98. });
  99. component_setMouseMoveEvent(mainPanel, [](const MouseEvent& event) {
  100. });
  101. component_setMouseUpEvent(mainPanel, [](const MouseEvent& event) {
  102. });
  103. */
  104. window_setCloseEvent(window, []() {
  105. running = false;
  106. });
  107. // Execute
  108. while(running) {
  109. // Wait for actions so that we don't render until an action has been recieved
  110. // This will save battery on laptops for applications that don't require animation
  111. while (!window_executeEvents(window)) {
  112. time_sleepSeconds(0.01);
  113. }
  114. // Fill the background
  115. AlignedImageRgbaU8 canvas = window_getCanvas(window);
  116. image_fill(canvas, ColorRgbaI32(64, 64, 64, 255));
  117. // Draw things
  118. drawEnvelope(canvas, IRect(0, 50, 550, 100), envelope, previewPressTime, previewViewTime);
  119. drawSound(canvas, IRect(0, 150, 550, 100), boomSound);
  120. drawSound(canvas, IRect(0, 250, 550, 100), basicTone);
  121. // Draw interface
  122. window_drawComponents(window);
  123. // Show the final image
  124. window_showCanvas(window);
  125. }
  126. // Close sound thread
  127. printText(U"Terminating sound\n");
  128. sound_terminate();
  129. }