gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
ALSAExternalPlugin.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef ALSAEXTERNALPLUGIN_H
18 #define ALSAEXTERNALPLUGIN_H
19 
20 #include "ALSA.H"
21 #include <alsa/pcm_external.h>
22 #include <alsa/pcm_plugin.h>
23 
24 #include <typeinfo>
25 #include <sstream>
26 
27 namespace ALSA {
28 
29 class ALSAExternalPlugin : public Software {
30  #define STATICFNNAME(name) name##_static
31  #define STATICFNDEF(retType, name) static retType STATICFNNAME(name) (snd_pcm_extplug_t *extplug)
32  #define STATICFNDEF2(retType, name, arg1_type, arg1) static retType STATICFNNAME(name) (snd_pcm_extplug_t *extplug, arg1_type arg1)
33  #define STATICFNBODY(name) {std::cout<<__func__<<std::endl; return static_cast<ALSAExternalPlugin*>(extplug->private_data)->name();}
34  #define STATICFNBODY2(name, arg1) {std::cout<<__func__<<std::endl; return static_cast<ALSAExternalPlugin*>(extplug->private_data)->name(arg1);}
35  #define STATICFN(retType, name) STATICFNDEF(retType, name) STATICFNBODY(name)
36  #define STATICFN2(retType, name, arg1_type, arg1) STATICFNDEF2(retType, name, arg1_type, arg1) STATICFNBODY2(name, arg1)
37 
38  snd_pcm_extplug_callback_t callbacks;
39 
40  // Define the varextplugus snd_pcm_extplug_callback_t static functextplugns
41  STATICFN(int, close)
42  STATICFN2(int, hwParams, snd_pcm_hw_params_t *, params)
43  STATICFN(int, hwFree)
44  STATICFN2(void, dump, snd_output_t *, out)
45  STATICFN(int, init)
46 // STATICFN(snd_pcm_chmap_query_t **, queryChmaps)
47 // STATICFN(snd_pcm_chmap_t *, getChmap)
48 // STATICFN2(int, setChmap, const snd_pcm_chmap_t *, map)
49 
50  static snd_pcm_sframes_t transfer_static(snd_pcm_extplug_t *extplug, const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset, const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset, snd_pcm_uframes_t size){
51 // std::cout<<__func__<<std::endl;
52  return static_cast<ALSAExternalPlugin*>(extplug->private_data)->doTransfer(dst_areas, dst_offset, src_areas, src_offset, size);
53  }
54 
55 protected:
56  snd_pcm_extplug_t extplug;
57  snd_config_t * slaveConf;
58 
62  void setName(const char *name){
63  extplug.name=name;
64  }
65 public:
66 
68  std::cout<<__func__<<std::endl;
69  slaveConf=NULL;
70  extplug.version = SND_PCM_EXTPLUG_VERSION;
71  setName("ALSAExternalPlugin default name");
72  extplug.callback = &callbacks;
73  extplug.private_data=this;
74 
75  // setup the static callbacks
76  callbacks.close=STATICFNNAME(close);
77  callbacks.hw_params=STATICFNNAME(hwParams);
78  callbacks.hw_free=STATICFNNAME(hwFree);
79  callbacks.dump=STATICFNNAME(dump);
80  callbacks.init=STATICFNNAME(init);
81 // callbacks.query_chmaps=STATICFNNAME(queryChmaps);
82 // callbacks.get_chmap=STATICFNNAME(getChmap);
83 // callbacks.set_chmap=STATICFNNAME(setChmap)
84 
85  callbacks.transfer=transfer_static;
86  }
87 
89  std::cout<<__func__<<std::endl;
90  }
91 
95  virtual int close(){
96  //std::cout<<typeid(this).name()<<'\t'<<__func__<<std::endl;
97  return 0;
98  }
99 
104  virtual int hwParams(snd_pcm_hw_params_t *params){
105  std::cout<<__func__<<std::endl;
106  copyFrom(params);
107  std::cout<<"channels : "<<getChannels()<<std::endl;
108  std::cout<<"fs : "<<getSampleRate(0)<<" Hz"<<std::endl;
109  std::cout<<"period sz: "<<getPeriodSize()<<" frames"<<std::endl;
110  return 0;
111  }
112 
116  virtual int specifyHWParams()=0;
117 
121  virtual int hwFree(){
123  return 0;
124  }
125 
129  virtual void dump(snd_output_t *out){
130  //std::cout<<typeid(this).name()<<'\t'<<__func__<<std::endl;
131  }
132 
136  virtual int init(){
137  //std::cout<<typeid(this).name()<<'\t'<<__func__<<std::endl;
138  return 0;
139  }
140 
141  snd_pcm_sframes_t doTransfer(const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset, const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset, snd_pcm_uframes_t size){
142  return transfer(dst_areas, dst_offset, src_areas, src_offset, size);
143  }
144 
147  virtual snd_pcm_sframes_t transfer(const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset, const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset, snd_pcm_uframes_t size){
148  //std::cout<<typeid(this).name()<<'\t'<<__func__<<std::endl;
149  return size;
150  }
151 
154  int create(const char *name, snd_config_t * root, snd_pcm_stream_t stream, int mode){
155  if (!slaveConf) {
156  std::ostringstream oss;
157  oss<<"No slave configuration for "<<name<<" : "<<extplug.name<<" external plugin";
158  SNDERR(oss.str().c_str());
159  return -EINVAL;
160  }
161  return snd_pcm_extplug_create(&extplug, name, root, slaveConf, stream, mode);
162  }
163 
167  virtual int parseConfig(const char *name, snd_config_t *conf, snd_pcm_stream_t stream, int mode){
168  //std::cout<<typeid(this).name()<<'\t'<<__func__<<std::endl;
169  snd_config_iterator_t i, next;
170  snd_config_for_each(i, next, conf) {
171  snd_config_t *n = snd_config_iterator_entry(i);
172  const char *id;
173  if (snd_config_get_id(n, &id) < 0)
174  continue;
175  if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
176  continue;
177  if (strcmp(id, "slave") == 0) {
178  slaveConf = n;
179  continue;
180  }
181  SNDERR("Unknown field %s", id);
182  return -EINVAL;
183  }
184 
185  if (!slaveConf) {
186  std::ostringstream oss;
187  oss<<"No slave configuration for "<<name<<" : "<<extplug.name<<" external plugin";
188  SNDERR(oss.str().c_str());
189  return -EINVAL;
190  }
191  return 0;
192  }
193 
197  virtual snd_pcm_t *getPCM(){
198  std::cout<<'\t'<<__func__<<'\t'<<__LINE__<<std::endl;
199  return extplug.pcm;
200  }
201 
202 };
203 };
204 #endif // ALSAEXTERNALPLUGIN_H
void copyFrom(snd_pcm_hw_params_t *hParamsIn)
Definition: Hardware.H:83
int getPeriodSize(snd_pcm_uframes_t *p, int *dir=NULL)
Definition: Hardware.H:275
#define STATICFNNAME(name)
size(signal)
Definition: ALSA.H:26
#define STATICFN2(retType, name, arg1_type, arg1)
virtual snd_pcm_t * getPCM()
#define STATICFN(retType, name)
void setName(const char *name)
The slave to use.
virtual snd_pcm_sframes_t transfer(const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset, const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset, snd_pcm_uframes_t size)
int getSampleRate(int dir=0)
Definition: Hardware.H:261
virtual int specifyHWParams()=0
virtual void dump(snd_output_t *out)
snd_pcm_sframes_t doTransfer(const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset, const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset, snd_pcm_uframes_t size)
int create(const char *name, snd_config_t *root, snd_pcm_stream_t stream, int mode)
virtual int parseConfig(const char *name, snd_config_t *conf, snd_pcm_stream_t stream, int mode)
static snd_pcm_sframes_t transfer_static(snd_pcm_extplug_t *extplug, const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset, const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset, snd_pcm_uframes_t size)
The callback functions.
int getChannels()
Definition: Hardware.H:222
snd_pcm_extplug_callback_t callbacks
virtual int hwParams(snd_pcm_hw_params_t *params)
snd_config_t * slaveConf
The ALSA plugin.
gtkIOStream: /tmp/gtkiostream/include/ALSA/ALSAExternalPlugin.H Source File
GTK+ IOStream  Beta