| 112 | }; |
| 113 | |
| 114 | int main() |
| 115 | { |
| 116 | std::cout << "CHOC Audio Beep Player Example" << std::endl; |
| 117 | std::cout << "===============================" << std::endl; |
| 118 | std::cout << "This example will play a 440Hz beep for 2 seconds using RtAudioMIDIPlayer." << std::endl; |
| 119 | std::cout << std::endl; |
| 120 | |
| 121 | choc::audio::io::AudioDeviceOptions options; |
| 122 | options.sampleRate = 44100; |
| 123 | options.blockSize = 512; |
| 124 | options.outputChannelCount = 2; |
| 125 | options.inputChannelCount = 0; |
| 126 | |
| 127 | auto logMessage = [] (const std::string& message) |
| 128 | { |
| 129 | std::cout << "[Audio] " << message << std::endl; |
| 130 | }; |
| 131 | |
| 132 | choc::audio::io::RtAudioMIDIPlayer player (options, logMessage); |
| 133 | |
| 134 | auto error = player.getLastError(); |
| 135 | if (! error.empty()) |
| 136 | { |
| 137 | std::cerr << "Error creating audio player: " << error << std::endl; |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | std::cout << "Audio setup complete:" << std::endl; |
| 142 | std::cout << " Sample rate: " << player.options.sampleRate << " Hz" << std::endl; |
| 143 | std::cout << " Block size: " << player.options.blockSize << " samples" << std::endl; |
| 144 | std::cout << " Output channels: " << player.options.outputChannelCount << std::endl; |
| 145 | std::cout << std::endl; |
| 146 | |
| 147 | auto beepGenerator = std::make_unique<BeepGenerator>(); |
| 148 | player.addCallback (*beepGenerator); |
| 149 | |
| 150 | std::cout << "Playing beep (440Hz sine wave for 2 seconds)..." << std::endl; |
| 151 | |
| 152 | choc::messageloop::initialise(); |
| 153 | |
| 154 | auto startTime = std::chrono::steady_clock::now(); |
| 155 | |
| 156 | while (beepGenerator->isStillPlaying()) |
| 157 | { |
| 158 | std::this_thread::sleep_for (std::chrono::milliseconds (50)); |
| 159 | |
| 160 | auto elapsed = std::chrono::steady_clock::now() - startTime; |
| 161 | |
| 162 | if (elapsed > std::chrono::seconds (3)) |
| 163 | { |
| 164 | std::cout << "Timeout reached, stopping..." << std::endl; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | std::cout << "Beep finished!" << std::endl; |
| 170 | |
| 171 | player.removeCallback (*beepGenerator); |
nothing calls this directly
no test coverage detected