1 /*
2 * audio.h
3 * DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath
4 * DIN Is Noise is released under GNU Public License 2.0
5 * For more information, please visit https://dinisnoise.org/
6 */
7 
8 
9 #ifndef __AUDIO
10 #define __AUDIO
11 
12 #include "RtAudio.h"
13 
14 #include <vector>
15 #include <string>
16 
17 typedef float sample_t;
18 
19 struct audio_out {
20 
21 	enum {INVALID=-1};
22 
23 	// audio devices
24 	RtAudio dac;
25 	std::vector<RtAudio::DeviceInfo> infos;
26 	std::vector<std::string> names; // use @ settings screen
27 	int num_devices, default_device, current_device, next_device, last_device;
28 
29 	// sample rate vars
30 	int sample_rate;
31 	int samples_per_buffer; // buffer has all channels
32 	int samples_channel_size, samples_buffer_size; // in bytes
33 	void set_sample_rate (int s);
34 	void set_samples_per_channel (int spc);
35 
36 	// for settings ui
37 	int i_sample_rate;
38 	void find_sample_rate_id (unsigned int sr);
39 	int goto_next_device (int i);
40 	int goto_next_sample_rate_id (int i);
41 
42   std::string prefs_name;
43   void load_prefs ();
44   void save_prefs ();
45   void defaults ();
46 
47   audio_out ();
48   ~audio_out ();
49 
50   void alloc ();
51 	void probe ();
52 	int open ();
53   int open (int id, unsigned int sr, unsigned int spc);
54 	int start ();
55 	int close ();
56 	void list ();
57 
58 
59   // written by the main thread
60   //
61   // a multi buffer scheme
62   //
63   // main thread writes a buffer, while audio thread streams another to audio card
64   //
65 
66 	int num_samples_buffers;
67   sample_t * samples_buffers; // a bunch of sample buffers
68   int* available; // buffer available for streaming?
69 
70   sample_t *readp, *writep;
71   int readi, writei;
can_writeaudio_out72   inline int can_write () {return !available [writei];}
73 
74 	// reuseable buffers
75 	// result, AM, FM, gater, volume, mix, mix alpha, buffer L,R , fader 1,2
76 	sample_t *result, *ams, *fms, *gatr, *vol, *mix, *mixa, *bufL, *bufR, *fdr1, *fdr2;
77 
78 	int num_channels;
79 	int samples_per_channel;
80 	int last_sample;
81 
82   static int audio_wanted (void *ob, void *ib, unsigned int spc, double t, RtAudioStreamStatus status, void *data);
83 
84 
85 };
86 
87 extern audio_out aout;
88 extern int SAMPLE_RATE;
89 extern float SAMPLE_DURATION;
90 
91 #endif
92