Bugfixing.
[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 // AUD_XXX this is not realtime suitable!
35 int AUD_JackDevice::jack_mix(jack_nframes_t length, void *data)
36 {
37         AUD_JackDevice* device = (AUD_JackDevice*)data;
38         int samplesize = AUD_SAMPLE_SIZE(device->m_specs);
39         if(device->m_buffer->getSize() < samplesize * length)
40                 device->m_buffer->resize(samplesize * length);
41         device->mix(device->m_buffer->getBuffer(), length);
42
43         float* in = (float*) device->m_buffer->getBuffer();
44         float* out;
45         int count = device->m_specs.channels;
46
47         for(int i = 0; i < count; i++)
48         {
49                 out = (float*)jack_port_get_buffer(device->m_ports[i], length);
50                 for(int j = 0; j < length; j++)
51                         out[j] = in[j * count + i];
52         }
53
54         return 0;
55 }
56
57 void AUD_JackDevice::jack_shutdown(void *data)
58 {
59         AUD_JackDevice* device = (AUD_JackDevice*)data;
60         device->m_valid = false;
61 }
62
63 AUD_JackDevice::AUD_JackDevice(AUD_Specs specs)
64 {
65         if(specs.channels == AUD_CHANNELS_INVALID)
66                 specs.channels = AUD_CHANNELS_STEREO;
67
68         // jack uses floats
69         m_specs = specs;
70         m_specs.format = AUD_FORMAT_FLOAT32;
71
72         jack_options_t options = JackNullOption;
73         jack_status_t status;
74
75         // open client
76         m_client = jack_client_open("Blender", options, &status);
77         if(m_client == NULL)
78                 AUD_THROW(AUD_ERROR_JACK);
79
80         m_buffer = new AUD_Buffer(); AUD_NEW("buffer");
81
82         // set callbacks
83         jack_set_process_callback(m_client, AUD_JackDevice::jack_mix, this);
84         jack_on_shutdown(m_client, AUD_JackDevice::jack_shutdown, this);
85
86         // register our output channels which are called ports in jack
87         m_ports = new jack_port_t*[m_specs.channels]; AUD_NEW("jack_port")
88
89         try
90         {
91                 char portname[64];
92                 for(int i = 0; i < m_specs.channels; i++)
93                 {
94                         sprintf(portname, "out %d", i+1);
95                         m_ports[i] = jack_port_register(m_client, portname,
96                                                                                         JACK_DEFAULT_AUDIO_TYPE,
97                                                                                         JackPortIsOutput, 0);
98                         if(m_ports[i] == NULL)
99                                 AUD_THROW(AUD_ERROR_JACK);
100                 }
101
102                 m_specs.rate = (AUD_SampleRate)jack_get_sample_rate(m_client);
103
104                 // activate the client
105                 if(jack_activate(m_client))
106                         AUD_THROW(AUD_ERROR_JACK);
107         }
108         catch(AUD_Exception e)
109         {
110                 jack_client_close(m_client);
111                 delete[] m_ports; AUD_DELETE("jack_port")
112                 delete m_buffer; AUD_DELETE("buffer");
113                 throw;
114         }
115
116         const char** ports = jack_get_ports(m_client, NULL, NULL,
117                                                                                 JackPortIsPhysical | JackPortIsInput);
118         if(ports != NULL)
119         {
120                 for(int i = 0; i < m_specs.channels && ports[i]; i++)
121                         jack_connect(m_client, jack_port_name(m_ports[i]), ports[i]);
122
123                 free(ports);
124         }
125
126         m_mixer = new AUD_FloatMixer(); AUD_NEW("mixer")
127         m_mixer->setSpecs(m_specs);
128
129         m_valid = true;
130
131         create();
132 }
133
134 AUD_JackDevice::~AUD_JackDevice()
135 {
136         lock();
137         if(m_valid)
138                 jack_client_close(m_client);
139         delete[] m_ports; AUD_DELETE("jack_port")
140         delete m_buffer; AUD_DELETE("buffer");
141         unlock();
142
143         destroy();
144 }
145
146 void AUD_JackDevice::playing(bool playing)
147 {
148         // Do nothing.
149 }