annotate src/portaudio/bindings/cpp/example/sine.cxx @ 107:71c914cf6201

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