annotate src/portaudio_20140130/bindings/cpp/example/sine.cxx @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents e3d5853d5918
children
rev   line source
cannam@124 1 // ---------------------------------------------------------------------------------------
cannam@124 2
cannam@124 3 #include <iostream>
cannam@124 4 #include <cmath>
cannam@124 5 #include <cassert>
cannam@124 6 #include <cstddef>
cannam@124 7 #include "portaudiocpp/PortAudioCpp.hxx"
cannam@124 8
cannam@124 9 // ---------------------------------------------------------------------------------------
cannam@124 10
cannam@124 11 // Some constants:
cannam@124 12 const int NUM_SECONDS = 5;
cannam@124 13 const double SAMPLE_RATE = 44100.0;
cannam@124 14 const int FRAMES_PER_BUFFER = 64;
cannam@124 15 const int TABLE_SIZE = 200;
cannam@124 16
cannam@124 17 // ---------------------------------------------------------------------------------------
cannam@124 18
cannam@124 19 // SineGenerator class:
cannam@124 20 class SineGenerator
cannam@124 21 {
cannam@124 22 public:
cannam@124 23 SineGenerator(int tableSize) : tableSize_(tableSize), leftPhase_(0), rightPhase_(0)
cannam@124 24 {
cannam@124 25 const double PI = 3.14159265;
cannam@124 26 table_ = new float[tableSize];
cannam@124 27 for (int i = 0; i < tableSize; ++i)
cannam@124 28 {
cannam@124 29 table_[i] = 0.125f * (float)sin(((double)i/(double)tableSize)*PI*2.);
cannam@124 30 }
cannam@124 31 }
cannam@124 32
cannam@124 33 ~SineGenerator()
cannam@124 34 {
cannam@124 35 delete[] table_;
cannam@124 36 }
cannam@124 37
cannam@124 38 int generate(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,
cannam@124 39 const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags)
cannam@124 40 {
cannam@124 41 assert(outputBuffer != NULL);
cannam@124 42
cannam@124 43 float **out = static_cast<float **>(outputBuffer);
cannam@124 44
cannam@124 45 for (unsigned int i = 0; i < framesPerBuffer; ++i)
cannam@124 46 {
cannam@124 47 out[0][i] = table_[leftPhase_];
cannam@124 48 out[1][i] = table_[rightPhase_];
cannam@124 49
cannam@124 50 leftPhase_ += 1;
cannam@124 51 if (leftPhase_ >= tableSize_)
cannam@124 52 leftPhase_ -= tableSize_;
cannam@124 53
cannam@124 54 rightPhase_ += 3;
cannam@124 55 if (rightPhase_ >= tableSize_)
cannam@124 56 rightPhase_ -= tableSize_;
cannam@124 57 }
cannam@124 58
cannam@124 59 return paContinue;
cannam@124 60 }
cannam@124 61
cannam@124 62 private:
cannam@124 63 float *table_;
cannam@124 64 int tableSize_;
cannam@124 65 int leftPhase_;
cannam@124 66 int rightPhase_;
cannam@124 67 };
cannam@124 68
cannam@124 69 // ---------------------------------------------------------------------------------------
cannam@124 70
cannam@124 71 // main:
cannam@124 72 int main(int, char *[]);
cannam@124 73 int main(int, char *[])
cannam@124 74 {
cannam@124 75 try
cannam@124 76 {
cannam@124 77 // Create a SineGenerator object:
cannam@124 78 SineGenerator sineGenerator(TABLE_SIZE);
cannam@124 79
cannam@124 80 std::cout << "Setting up PortAudio..." << std::endl;
cannam@124 81
cannam@124 82 // Set up the System:
cannam@124 83 portaudio::AutoSystem autoSys;
cannam@124 84 portaudio::System &sys = portaudio::System::instance();
cannam@124 85
cannam@124 86 // Set up the parameters required to open a (Callback)Stream:
cannam@124 87 portaudio::DirectionSpecificStreamParameters outParams(sys.defaultOutputDevice(), 2, portaudio::FLOAT32, false, sys.defaultOutputDevice().defaultLowOutputLatency(), NULL);
cannam@124 88 portaudio::StreamParameters params(portaudio::DirectionSpecificStreamParameters::null(), outParams, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff);
cannam@124 89
cannam@124 90 std::cout << "Opening stereo output stream..." << std::endl;
cannam@124 91
cannam@124 92 // Create (and open) a new Stream, using the SineGenerator::generate function as a callback:
cannam@124 93 portaudio::MemFunCallbackStream<SineGenerator> stream(params, sineGenerator, &SineGenerator::generate);
cannam@124 94
cannam@124 95 std::cout << "Starting playback for " << NUM_SECONDS << " seconds." << std::endl;
cannam@124 96
cannam@124 97 // Start the Stream (audio playback starts):
cannam@124 98 stream.start();
cannam@124 99
cannam@124 100 // Wait for 5 seconds:
cannam@124 101 sys.sleep(NUM_SECONDS * 1000);
cannam@124 102
cannam@124 103 std::cout << "Closing stream..." <<std::endl;
cannam@124 104
cannam@124 105 // Stop the Stream (not strictly needed as termintating the System will also stop all open Streams):
cannam@124 106 stream.stop();
cannam@124 107
cannam@124 108 // Close the Stream (not strictly needed as terminating the System will also close all open Streams):
cannam@124 109 stream.close();
cannam@124 110
cannam@124 111 // Terminate the System (not strictly needed as the AutoSystem will also take care of this when it
cannam@124 112 // goes out of scope):
cannam@124 113 sys.terminate();
cannam@124 114
cannam@124 115 std::cout << "Test finished." << std::endl;
cannam@124 116 }
cannam@124 117 catch (const portaudio::PaException &e)
cannam@124 118 {
cannam@124 119 std::cout << "A PortAudio error occured: " << e.paErrorText() << std::endl;
cannam@124 120 }
cannam@124 121 catch (const portaudio::PaCppException &e)
cannam@124 122 {
cannam@124 123 std::cout << "A PortAudioCpp error occured: " << e.what() << std::endl;
cannam@124 124 }
cannam@124 125 catch (const std::exception &e)
cannam@124 126 {
cannam@124 127 std::cout << "A generic exception occured: " << e.what() << std::endl;
cannam@124 128 }
cannam@124 129 catch (...)
cannam@124 130 {
cannam@124 131 std::cout << "An unknown exception occured." << std::endl;
cannam@124 132 }
cannam@124 133
cannam@124 134 return 0;
cannam@124 135 }
cannam@124 136
cannam@124 137