To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / JackDevice / ContextJack.h

History | View | Annotate | Download (5.23 KB)

1 1:b5bcad8e7803 f
/*
2

3 5:75b744078d66 f
 Copyright (C) 2016  Queen Mary University of London
4
 Author: Fiore Martin
5 1:b5bcad8e7803 f

6 5:75b744078d66 f
 This file is part of Collidoscope.
7

8
 Collidoscope is free software: you can redistribute it and/or modify
9
 it under the terms of the GNU General Public License as published by
10
 the Free Software Foundation, either version 3 of the License, or
11
 (at your option) any later version.
12

13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 GNU General Public License for more details.
17

18
 You should have received a copy of the GNU General Public License
19
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20

21
 This file incorporates work covered by the following copyright and permission notice:
22

23
    Copyright (c) 2014, The Cinder Project
24

25
    This code is intended to be used with the Cinder C++ library, http://libcinder.org
26

27
    Redistribution and use in source and binary forms, with or without modification, are permitted provided that
28
    the following conditions are met:
29 1:b5bcad8e7803 f

30
    * Redistributions of source code must retain the above copyright notice, this list of conditions and
31 5:75b744078d66 f
    the following disclaimer.
32 1:b5bcad8e7803 f
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
33 5:75b744078d66 f
    the following disclaimer in the documentation and/or other materials provided with the distribution.
34 1:b5bcad8e7803 f

35 5:75b744078d66 f
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
36
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
37
    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
38
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
39
    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42
    POSSIBILITY OF SUCH DAMAGE.
43

44 1:b5bcad8e7803 f
*/
45
46
#pragma once
47
48
#include "cinder/audio/Context.h"
49
#include <jack/jack.h>
50
51
namespace cinder { namespace audio { namespace linux {
52
53
class ContextJack;
54
class InputDeviceNodeJack;
55
56 4:ab6db404403a f
/**
57
 * OutputNode (as in the cinder::audio::OutputNode) that sends audio to the sound card using the jack audio callback method.
58
 */
59 1:b5bcad8e7803 f
class OutputDeviceNodeJack : public OutputDeviceNode {
60
  public:
61 5:75b744078d66 f
    OutputDeviceNodeJack( const DeviceRef &device, const Format &format, const std::shared_ptr<ContextJack> &context );
62 1:b5bcad8e7803 f
63 4:ab6db404403a f
    /** Gives this output node a reference to the JackInputNode.
64
     *  In initialize() the reference is used to give the input node access to jack input ports
65
     */
66 1:b5bcad8e7803 f
    void setInput(InputDeviceNodeRef inputDeviceNode);
67
68
  protected:
69 5:75b744078d66 f
    void initialize()               override;
70
    void uninitialize()             override;
71
    void enableProcessing()         override;
72
    void disableProcessing()        override;
73
    bool supportsProcessInPlace() const override    { return false; }
74 1:b5bcad8e7803 f
75
  private:
76 4:ab6db404403a f
    // this is called by jack in the audio thread at every tick of the sound card
77 1:b5bcad8e7803 f
    static int jackCallback(jack_nframes_t nframes, void* userData);
78
79
80 5:75b744078d66 f
    void renderToBufferFromInputs();
81 1:b5bcad8e7803 f
82 4:ab6db404403a f
    /**
83
     * RenderData is passed as user_data to jack when the jack process callback is installed
84
     */
85 1:b5bcad8e7803 f
    struct RenderData{
86
        RenderData() : inputNode(nullptr), outputNode(nullptr), context(nullptr){}
87
        ~RenderData() { inputNode = nullptr; outputNode = nullptr;  context = nullptr; }
88
        Node* outputNode;
89
        Node* inputNode;
90
        ContextJack* context;
91
    } mRenderData;
92
93
    std::weak_ptr<ContextJack>  mCinderContext;
94
95 5:75b744078d66 f
    jack_client_t *mClient;
96 1:b5bcad8e7803 f
97
    std::array< jack_port_t*, 2 > mOutputPorts;
98
99
    std::shared_ptr<InputDeviceNodeJack> mInputDeviceNode;
100
};
101
102 4:ab6db404403a f
/**
103
 * InputNode (as in the cinder::audio::OutputNode) that reads audio from the sound card using the jack audio callback method.
104
 */
105 1:b5bcad8e7803 f
class InputDeviceNodeJack : public InputDeviceNode {
106
  friend OutputDeviceNodeJack;
107
108
  public:
109 5:75b744078d66 f
    InputDeviceNodeJack( const DeviceRef &device, const Format &format, const std::shared_ptr<ContextJack> &context );
110 1:b5bcad8e7803 f
111
  protected:
112 5:75b744078d66 f
    void initialize()               override;
113
    void uninitialize()             override;
114
    void enableProcessing()         override;
115
    void disableProcessing()        override;
116
    void process( Buffer *buffer )  override;
117 1:b5bcad8e7803 f
118
  private:
119
    std::array< jack_port_t*, 2 > mInputPorts;
120
};
121
122
class ContextJack : public Context {
123
  public:
124 5:75b744078d66 f
    ContextJack() {}
125
    virtual ~ContextJack() {}
126 1:b5bcad8e7803 f
127
128 5:75b744078d66 f
    OutputDeviceNodeRef createOutputDeviceNode( const DeviceRef &device, const Node::Format &format = Node::Format() ) override;
129
    InputDeviceNodeRef  createInputDeviceNode( const DeviceRef &device, const Node::Format &format = Node::Format()  ) override;
130 1:b5bcad8e7803 f
131 5:75b744078d66 f
    OutputDeviceNodeRef mOutputDeviceNode;
132
    InputDeviceNodeRef  mInputDeviceNode;
133 1:b5bcad8e7803 f
134
135
  private:
136 5:75b744078d66 f
};
137 1:b5bcad8e7803 f
138
} } } // namespace cinder::audio::linux