1f2c6faa51165c86f56eab93d662842e5184a4f5
[blender.git] / intern / audaspace / jack / AUD_JackDevice.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN LGPL LICENSE BLOCK *****
5  *
6  * Copyright 2009 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * AudaSpace is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with AudaSpace.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * ***** END LGPL LICENSE BLOCK *****
24  */
25
26 #include "AUD_FloatMixer.h"
27 #include "AUD_JackDevice.h"
28 #include "AUD_IReader.h"
29 #include "AUD_Buffer.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #ifdef WITH_JACK
35
36 // AUD_XXX this is not realtime suitable!
37 int AUD_JackDevice::jack_mix(jack_nframes_t length, void *data)
38 {
39         AUD_JackDevice* device = (AUD_JackDevice*)data;
40         int samplesize = AUD_SAMPLE_SIZE(device->m_specs);
41         if(device->m_buffer->getSize() < samplesize * length)
42                 device->m_buffer->resize(samplesize * length);
43         device->mix(device->m_buffer->getBuffer(), length);
44
45         float* in = (float*) device->m_buffer->getBuffer();
46         float* out;
47         int count = device->m_specs.channels;
48
49         for(int i = 0; i < count; i++)
50         {
51                 out = (float*)jack_port_get_buffer(device->m_ports[i], length);
52                 for(int j = 0; j < length; j++)
53                         out[j] = in[j * count + i];
54         }
55
56         return 0;
57 }
58
59 void AUD_JackDevice::jack_shutdown(void *data)
60 {
61         AUD_JackDevice* device = (AUD_JackDevice*)data;
62         device->m_valid = false;
63 }
64
65 AUD_JackDevice::AUD_JackDevice(AUD_Specs specs)
66 {
67         if(specs.channels == AUD_CHANNELS_INVALID)
68                 specs.channels = AUD_CHANNELS_STEREO;
69
70         // jack uses floats
71         m_specs = specs;
72         m_specs.format = AUD_FORMAT_FLOAT32;
73
74         jack_options_t options = JackNullOption;
75         jack_status_t status;
76
77         // open client
78         m_client = jack_client_open("Blender", options, &status);
79         if(m_client == NULL)
80                 AUD_THROW(AUD_ERROR_JACK);
81
82         m_buffer = new AUD_Buffer(); AUD_NEW("buffer");
83
84         // set callbacks
85         jack_set_process_callback(m_client, AUD_JackDevice::jack_mix, this);
86         jack_on_shutdown(m_client, AUD_JackDevice::jack_shutdown, this);
87
88         // register our output channels which are called ports in jack
89         m_ports = new jack_port_t*[m_specs.channels]; AUD_NEW("jack_port")
90
91         try
92         {
93                 char portname[64];
94                 for(int i = 0; i < m_specs.channels; i++)
95                 {
96                         sprintf(portname, "out %d", i+1);
97                         m_ports[i] = jack_port_register(m_client, portname,
98                                                                                         JACK_DEFAULT_AUDIO_TYPE,
99                                                                                         JackPortIsOutput, 0);
100                         if(m_ports[i] == NULL)
101                                 AUD_THROW(AUD_ERROR_JACK);
102                 }
103
104                 m_specs.rate = (AUD_SampleRate)jack_get_sample_rate(m_client);
105
106                 // activate the client
107                 if(jack_activate(m_client))
108                         AUD_THROW(AUD_ERROR_JACK);
109         }
110         catch(AUD_Exception e)
111         {
112                 jack_client_close(m_client);
113                 delete[] m_ports; AUD_DELETE("jack_port")
114                 delete m_buffer; AUD_DELETE("buffer");
115                 throw;
116         }
117
118         const char** ports = jack_get_ports(m_client, NULL, NULL,
119                                                                                 JackPortIsPhysical | JackPortIsInput);
120         if(ports != NULL)
121         {
122                 for(int i = 0; i < m_specs.channels && ports[i]; i++)
123                         jack_connect(m_client, jack_port_name(m_ports[i]), ports[i]);
124
125                 free(ports);
126         }
127
128         m_mixer = new AUD_FloatMixer(); AUD_NEW("mixer")
129         m_mixer->setSpecs(m_specs);
130
131         m_valid = true;
132
133         create();
134 }
135
136 AUD_JackDevice::~AUD_JackDevice()
137 {
138         lock();
139         if(m_valid)
140                 jack_client_close(m_client);
141         delete[] m_ports; AUD_DELETE("jack_port")
142         delete m_buffer; AUD_DELETE("buffer");
143         unlock();
144
145         destroy();
146 }
147
148 void AUD_JackDevice::playing(bool playing)
149 {
150         // Do nothing.
151 }
152
153 #endif