summaryrefslogtreecommitdiff
path: root/3rdparty/portaudio/bindings/cpp/source/portaudiocpp/BlockingStream.cxx
blob: b26b9e81a4f025db9077f1ab7972db6fde6fbcd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "portaudiocpp/BlockingStream.hxx"

#include "portaudio.h"

#include "portaudiocpp/StreamParameters.hxx"
#include "portaudiocpp/Exception.hxx"

namespace portaudio
{

	// --------------------------------------------------------------------------------------

	BlockingStream::BlockingStream()
	{
	}

	BlockingStream::BlockingStream(const StreamParameters &parameters)
	{
		open(parameters);
	}

	BlockingStream::~BlockingStream()
	{
		try
		{
			close();
		}
		catch (...)
		{
			// ignore all errors
		}
	}

	// --------------------------------------------------------------------------------------

	void BlockingStream::open(const StreamParameters &parameters)
	{
		PaError err = Pa_OpenStream(&stream_, parameters.inputParameters().paStreamParameters(), parameters.outputParameters().paStreamParameters(), 
			parameters.sampleRate(), parameters.framesPerBuffer(), parameters.flags(), NULL, NULL);

		if (err != paNoError)
		{
			throw PaException(err);
		}
	}

	// --------------------------------------------------------------------------------------

	void BlockingStream::read(void *buffer, unsigned long numFrames)
	{
		PaError err = Pa_ReadStream(stream_, buffer, numFrames);

		if (err != paNoError)
		{
			throw PaException(err);
		}
	}

	void BlockingStream::write(const void *buffer, unsigned long numFrames)
	{
		PaError err = Pa_WriteStream(stream_, buffer, numFrames);

		if (err != paNoError)
		{
			throw PaException(err);
		}
	}

	// --------------------------------------------------------------------------------------

	signed long BlockingStream::availableReadSize() const
	{
		signed long avail = Pa_GetStreamReadAvailable(stream_);

		if (avail < 0)
		{
			throw PaException(avail);
		}

		return avail;
	}

	signed long BlockingStream::availableWriteSize() const
	{
		signed long avail = Pa_GetStreamWriteAvailable(stream_);

		if (avail < 0)
		{
			throw PaException(avail);
		}

		return avail;
	}

	// --------------------------------------------------------------------------------------

} // portaudio