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