To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at https://github.com/sonic-visualiser/sv-dependency-builds .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / src / portaudio_20161030_catalina_patch / bindings / cpp / example / sine.cxx @ 164:9fa11135915a

History | View | Annotate | Download (3.71 KB)

1
// ---------------------------------------------------------------------------------------
2

    
3
#include <iostream>
4
#include <cmath>
5
#include <cassert>
6
#include <cstddef>
7
#include "portaudiocpp/PortAudioCpp.hxx"
8

    
9
// ---------------------------------------------------------------------------------------
10

    
11
// Some constants:
12
const int NUM_SECONDS = 5;
13
const double SAMPLE_RATE = 44100.0;
14
const int FRAMES_PER_BUFFER = 64;
15
const int TABLE_SIZE = 200;
16

    
17
// ---------------------------------------------------------------------------------------
18

    
19
// SineGenerator class:
20
class SineGenerator
21
{
22
public:
23
        SineGenerator(int tableSize) : tableSize_(tableSize), leftPhase_(0), rightPhase_(0)
24
        {
25
                const double PI = 3.14159265;
26
                table_ = new float[tableSize];
27
                for (int i = 0; i < tableSize; ++i)
28
                {
29
                        table_[i] = 0.125f * (float)sin(((double)i/(double)tableSize)*PI*2.);
30
                }
31
        }
32

    
33
        ~SineGenerator()
34
        {
35
                delete[] table_;
36
        }
37

    
38
        int generate(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, 
39
                const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags)
40
        {
41
                assert(outputBuffer != NULL);
42

    
43
                float **out = static_cast<float **>(outputBuffer);
44

    
45
                for (unsigned int i = 0; i < framesPerBuffer; ++i)
46
                {
47
                        out[0][i] = table_[leftPhase_];
48
                        out[1][i] = table_[rightPhase_];
49

    
50
                        leftPhase_ += 1;
51
                        if (leftPhase_ >= tableSize_)
52
                                leftPhase_ -= tableSize_;
53

    
54
                        rightPhase_ += 3;
55
                        if (rightPhase_ >= tableSize_)
56
                                rightPhase_ -= tableSize_;
57
                }
58

    
59
                return paContinue;
60
        }
61

    
62
private:
63
        float *table_;
64
        int tableSize_;
65
        int leftPhase_;
66
        int rightPhase_;
67
};
68

    
69
// ---------------------------------------------------------------------------------------
70

    
71
// main:
72
int main(int, char *[]);
73
int main(int, char *[])
74
{
75
        try
76
        {
77
                // Create a SineGenerator object:
78
                SineGenerator sineGenerator(TABLE_SIZE);
79

    
80
                std::cout << "Setting up PortAudio..." << std::endl;
81

    
82
                // Set up the System:
83
                portaudio::AutoSystem autoSys;
84
                portaudio::System &sys = portaudio::System::instance();
85

    
86
                // Set up the parameters required to open a (Callback)Stream:
87
                portaudio::DirectionSpecificStreamParameters outParams(sys.defaultOutputDevice(), 2, portaudio::FLOAT32, false, sys.defaultOutputDevice().defaultLowOutputLatency(), NULL);
88
                portaudio::StreamParameters params(portaudio::DirectionSpecificStreamParameters::null(), outParams, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff);
89

    
90
                std::cout << "Opening stereo output stream..." << std::endl;
91

    
92
                // Create (and open) a new Stream, using the SineGenerator::generate function as a callback:
93
                portaudio::MemFunCallbackStream<SineGenerator> stream(params, sineGenerator, &SineGenerator::generate);
94

    
95
                std::cout << "Starting playback for " << NUM_SECONDS << " seconds." << std::endl;
96

    
97
                // Start the Stream (audio playback starts):
98
                stream.start();
99

    
100
                // Wait for 5 seconds:
101
                sys.sleep(NUM_SECONDS * 1000);
102

    
103
                std::cout << "Closing stream..." <<std::endl;
104

    
105
                // Stop the Stream (not strictly needed as termintating the System will also stop all open Streams):
106
                stream.stop();
107

    
108
                // Close the Stream (not strictly needed as terminating the System will also close all open Streams):
109
                stream.close();
110

    
111
                // Terminate the System (not strictly needed as the AutoSystem will also take care of this when it 
112
                // goes out of scope):
113
                sys.terminate();
114

    
115
                std::cout << "Test finished." << std::endl;
116
        }
117
        catch (const portaudio::PaException &e)
118
        {
119
                std::cout << "A PortAudio error occured: " << e.paErrorText() << std::endl;
120
        }
121
        catch (const portaudio::PaCppException &e)
122
        {
123
                std::cout << "A PortAudioCpp error occured: " << e.what() << std::endl;
124
        }
125
        catch (const std::exception &e)
126
        {
127
                std::cout << "A generic exception occured: " << e.what() << std::endl;
128
        }
129
        catch (...)
130
        {
131
                std::cout << "An unknown exception occured." << std::endl;
132
        }
133

    
134
        return 0;
135
}
136

    
137