audioDevice.h 3.6 KB
Newer Older
jcaceres's avatar
jcaceres committed
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**********************************************************************
 * File: audioDevice.h
 **********************************************************************
 * A project of the SoundWIRE research group at CCRMA
 * http://www-ccrma.stanford.edu/groups/soundwire/
 * --------------------------------------------------------------------
 * Coded by Daniel Walling (dwalling@ccrma.stanford.edu)
 * --------------------------------------------------------------------
 */

#ifndef	_AUDIO_DEVICE_H
#define	_AUDIO_DEVICE_H

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <stk/RtAudio.h>
#include "audioInfo.h"
#include "JackClient.h"
#include "qthread.h"
#include "StreamBD.h"
/**
 * @brief Interfaces with an RtAudio sound device.
 */

// typedef class AudioDevice
class AudioDevice
{
      public:
	/** @brief Modes the audio device can be opened in:
         *
         * RECORD - The device can only be used for audio input.
         * PLAYBACK - The device can only be used for audio output.
         * DUPLEX - The device can be used for both input and output.
         */
	enum audioDeviceModeT
	{ RECORD, PLAYBACK, DUPLEX };
public:
	AudioInput *inThread;
AudioOutput *outThread;
void setThreads(streamThreads t);

      private:
	//RtAudio *device; //!< RtAudio Device.
	  RtAudio * device;
	JackClient *jdevice;

	char *buffer;		//!< Pointer to the RtAudio audio buffer.
	void *jibuffer;
    void *jobuffer;	  
	int streamID;		//!< RtAudio Stream ID
	int audioDeviceID;	//!< Audio device ID.
	AudioInfoT audioInfo;	//!< Relevant audio information.
	int numBuffers;		//!< Length of the device buffer (in buffers
	//!< of size AudioInfoT->framesPerBuffer).
	int bytesPerBuffer;	//!< Total number of bytes per audio buffer.
	int *stringsPerChan;	//!< Array of number of NetHarp strings per audio output channel for audio monitoring.
	bool harp;		//!< Harp mode?
	QSemaphore *readLock;	//!< Thread protection.
	QSemaphore *writeLock;	//!< Thread protection.
	audioDeviceModeT mode;	//!< Device mode.

	void generateHarpMixMap ();	//!< Generates mix map for mixing X NetHarp strings to Y audio channels

	void checkRequirements (int i, RtAudioDeviceInfo * info);


      public:

	/**
         * @brief Constructor.
         *
         * @param audioDeviceID - The device ID of the sound device to use (-1 for default).
         * @param numBuffers - The length of the queue of audio buffers maintained on the audio device.
         * @param audioInfo - Relevant audio information.
         */
	  AudioDevice (int audioDeviceID, int numBuffers,
				    audioDeviceModeT mode,
				    AudioInfoT audioInfo);

	  AudioDevice (bool test);

	 ~AudioDevice ();

	/**
         * @brief tick the audio device.
         *
         * Plays the samples currently in the buffer, and (if in duplex mode), reads in a new buffer from the audio inputs.
         */
	void tick ();
      /**
         * @brief tick the audio device.
         *
         *  Jack audio version, jack subsystem calls jtick.
         */
	void jtick ();

	/**
         * @brief read a buffer of audio samples from the device into the <b>to</b> buffer. (blocks).
         */
	void readBuffer (void *to);

	/**
         * @brief write a buffer of audio samples from the <b>from</b> buffer to the audio device buffer.  (blocks).
         */
	void writeBuffer (void *from);
	QWaitCondition jackWait;
	void bufferPtrs (void * jib, void * job);
	void jackStart ();
	void jackStop ();
	void probeAudioDevices ();	//!< Probes audio devices to see if all required functionality is supported.
	void unlockRead ();
	void harpTick (void *toBuffer, void *fromBuffer);
	bool jack;
// } *AudioDeviceT;
};

#endif