HulaLoop
Simple cross-platform audio loopback and recording.
HulaRingBuffer.h
1 /*
2  * Created using code form: https://app.assembla.com/spaces/portaudio/git/source/master/examples/paex_record_file.c
3  *
4  * This program uses the PortAudio Portable Audio Library.
5  * For more information see: http://www.portaudio.com
6  * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files
10  * (the "Software"), to deal in the Software without restriction,
11  * including without limitation the rights to use, copy, modify, merge,
12  * publish, distribute, sublicense, and/or sell copies of the Software,
13  * and to permit persons to whom the Software is furnished to do so,
14  * subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 /*
29  * The text above constitutes the entire PortAudio license; however,
30  * the PortAudio community also makes the following non-binding requests:
31  *
32  * Any person wishing to distribute modifications to the Software is
33  * requested to send the modifications to the original developer so that
34  * they can be incorporated into the canonical version. It is also
35  * requested that these non-binding requests be included along with the
36  * license above.
37  */
38 
39 #ifndef HL_RING_BUFFER_H
40 #define HL_RING_BUFFER_H
41 
42 #include <pa_ringbuffer.h>
43 #include <pa_util.h>
44 #include <portaudio.h>
45 #include <cstdio>
46 #include <cstdlib>
47 #include <thread>
48 
49 #define SAMPLE_RATE (44100)
50 #define FRAMES_PER_BUFFER (512)
51 #define NUM_SECONDS (10)
52 #define NUM_CHANNELS (2)
53 #define NUM_WRITES_PER_BUFFER (4)
54 
55 // Select sample format.
56 #if 1
57  #define PA_SAMPLE_TYPE paFloat32
58  #define SAMPLE float
59  #define SAMPLE_SILENCE (0.0f)
60  #define PRINTF_S_FORMAT "%.8f"
61 #elif 1
62  #define PA_SAMPLE_TYPE paInt16
63  typedef short SAMPLE;
64  #define SAMPLE_SILENCE (0)
65  #define PRINTF_S_FORMAT "%d"
66 #elif 0
67  #define PA_SAMPLE_TYPE paInt8
68  typedef char SAMPLE;
69  #define SAMPLE_SILENCE (0)
70  #define PRINTF_S_FORMAT "%d"
71 #else
72  #define PA_SAMPLE_TYPE paUInt8
73  typedef unsigned char SAMPLE;
74  #define SAMPLE_SILENCE (128)
75  #define PRINTF_S_FORMAT "%d"
76 #endif
77 
78 #define BYTES_TO_SAMPLES(bytes) ((bytes) / (sizeof(SAMPLE)))
79 #define SAMPLES_TO_BYTES(samples) ((samples) * (sizeof(SAMPLE)))
80 
81 namespace hula
82 {
87 
88  private:
92  SAMPLE *rbMemory;
93 
97  PaUtilRingBuffer rb;
98 
104  static uint32_t nextPowerOf2(uint32_t val)
105  {
106  val--;
107  val = (val >> 1) | val;
108  val = (val >> 2) | val;
109  val = (val >> 4) | val;
110  val = (val >> 8) | val;
111  val = (val >> 16) | val;
112  return ++val;
113  };
114 
115  public:
116  HulaRingBuffer(float maxDuration);
117 
118  ring_buffer_size_t read(SAMPLE *data, ring_buffer_size_t maxSamples);
119  ring_buffer_size_t directRead(ring_buffer_size_t maxSamples, void **dataPtr1, ring_buffer_size_t *size1, void **dataPtr2, ring_buffer_size_t *size2);
120  ring_buffer_size_t write(const SAMPLE *data, ring_buffer_size_t maxSamples);
121 
122  ~HulaRingBuffer();
123 
124  };
125 }
126 
127 #endif // END HL_RING_BUFFER_H
ring_buffer_size_t read(SAMPLE *data, ring_buffer_size_t maxSamples)
Read up to maxSamples from the ring buffer into the memory pointed to by data.
Definition: HulaRingBuffer.cpp:82
~HulaRingBuffer()
Destructor for the ring buffer.
Definition: HulaRingBuffer.cpp:154
ring_buffer_size_t write(const SAMPLE *data, ring_buffer_size_t maxSamples)
Add data to the ring buffer.
Definition: HulaRingBuffer.cpp:130
HulaRingBuffer(float maxDuration)
Create a new ring buffer.
Definition: HulaRingBuffer.cpp:53
HulaLoop wrapper class for PortAudio ring buffer.
Definition: HulaRingBuffer.h:86
ring_buffer_size_t directRead(ring_buffer_size_t maxSamples, void **dataPtr1, ring_buffer_size_t *size1, void **dataPtr2, ring_buffer_size_t *size2)
Fetch direct pointers to memory within the ring buffer.
Definition: HulaRingBuffer.cpp:101
Wrapper around translation functions for Qt.
Definition: Controller.h:10