Mercurial > hg > vampy-host
comparison vampyhost-junk.cpp @ 0:68f3f32565b4
Import the early draft version
author | Chris Cannam |
---|---|
date | Mon, 22 Oct 2012 16:10:46 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:68f3f32565b4 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 // Moving stuff around | |
4 | |
5 static PyObject * | |
6 vampyhost_process(PyObject *self, PyObject *args) | |
7 { | |
8 //check if the plugin has been initialised | |
9 //obtain sample Rate: maybe library:identifier:channels:stepSize:blockSize | |
10 PyObject *pyPluginHandle; | |
11 PyObject *pyBuffer; | |
12 | |
13 if (!PyArg_ParseTuple(args, "OO", | |
14 &pyPluginHandle, // C object holding a pointer to a plugin and its descriptor | |
15 &pyBuffer)) { // Audio data | |
16 PyErr_SetString(PyExc_TypeError, | |
17 "Required: plugin handle, buffer, timestmap."); | |
18 return NULL; } | |
19 | |
20 string *key; | |
21 Plugin *plugin; | |
22 long frame = 0; | |
23 | |
24 if ( !getPluginHandle(pyPluginHandle, &plugin, &key) ) { | |
25 PyErr_SetString(PyExc_AttributeError, | |
26 "Invalid or already deleted plugin handle."); | |
27 return NULL; } | |
28 | |
29 PyPluginDescriptor *pd = (PyPluginDescriptor*) key; | |
30 | |
31 if (!pd->isInitialised) { | |
32 PyErr_SetString(PyExc_StandardError, | |
33 "Plugin has not been initialised."); | |
34 return NULL; } | |
35 | |
36 size_t channels = pd->channels; | |
37 size_t blockSize = pd->blockSize; | |
38 | |
39 /* | |
40 Handle the case when we get the data as a character buffer | |
41 Handle SampleFormats: int16, float32 | |
42 | |
43 */ | |
44 | |
45 if (PyString_Check(pyBuffer)) { | |
46 cerr << ">>> String obj passed in." << endl; | |
47 } | |
48 | |
49 // size_t chlen = sizeof(short) / sizeof(char); | |
50 | |
51 //Assume interleaved signed 16-bit PCM data | |
52 | |
53 //int *intch = new int*[buflen/2]; | |
54 //int *intch = (int*) PyString_AS_STRING(pyBuffer); | |
55 //short *tmpch = | |
56 //reinterpret_cast <short*> (PyString_AS_STRING(pyBuffer)); | |
57 | |
58 typedef char int16[2]; //can we trust sizeof(short) = 2 ? | |
59 size_t sample_size = sizeof(int16); | |
60 | |
61 long buflen = (long) PyString_GET_SIZE(pyBuffer); | |
62 | |
63 size_t input_length = | |
64 static_cast <size_t> (buflen/channels/sample_size); | |
65 | |
66 if (input_length == pd->blockSize) { | |
67 cerr << ">>> A full block has been passed in." << endl; } | |
68 | |
69 int16 *input = | |
70 reinterpret_cast <int16*> (PyString_AS_STRING(pyBuffer)); | |
71 | |
72 // int16 *input = new int16[buflen/sample_size]; | |
73 // input = reinterpret_cast <int16*> (PyString_AS_STRING(pyBuffer)); | |
74 | |
75 // short *input = | |
76 // reinterpret_cast <short*> (PyString_AS_STRING(pyBuffer)); | |
77 | |
78 //float ffirst = | |
79 //static_cast <float> (*input[1000]) / | |
80 //static_cast <float> (SHRT_MAX); | |
81 | |
82 // int *proba[10]; -> pointer array | |
83 int *proba = new int[10]; // -> actual array of ints | |
84 int p = 234; | |
85 proba[1]=p; | |
86 size_t chlen = (size_t) buflen/2; | |
87 //short smax = SHRT_MAX; | |
88 cerr | |
89 << " c: " << sizeof(char) | |
90 << " s: " << sizeof(short) | |
91 //<< " i16: " << sizeof(int16) | |
92 << " i:" << sizeof(int) | |
93 << " float:" << sizeof(float) | |
94 << " [proba]: " << proba[1] | |
95 //<< " ffirst: " << ffirst | |
96 << endl; | |
97 | |
98 //vector<int> *intch = (vector<int>*) PyString_AS_STRING(pyBuffer); | |
99 //size_t chlen = intch->size(); | |
100 //cerr << ">>>Size of ch buffer: " << chlen << endl; | |
101 | |
102 //convert int16 PCM data to 32-bit floats | |
103 float **plugbuf = new float*[channels]; | |
104 float smax = static_cast <float> (SHRT_MAX); | |
105 | |
106 for (size_t c = 0; c < channels; ++c) { | |
107 | |
108 plugbuf[c] = new float[blockSize+2]; | |
109 | |
110 size_t j = 0; | |
111 while (j < input_length) { | |
112 //int *v = (*int) input[j * channels + c]; | |
113 //int value = 5;//input[j * channels + c]; | |
114 // short *v = (short*) input+j; | |
115 // short value = *v; | |
116 //int *v = (int*) input+j; | |
117 int *v = new int; | |
118 *v = 0; | |
119 char *wc = (char*) v; | |
120 char *ic = (char*) input[j]; | |
121 wc=wc+2; | |
122 *wc = *ic; | |
123 wc++; ic++; | |
124 *wc = *ic; | |
125 | |
126 int value = *v; | |
127 | |
128 plugbuf[c][j] = static_cast <float> (value/100000); | |
129 // works if short=2 static_cast <float> (*input[j * channels + c]) / smax; | |
130 // static_cast <float> (input[j * channels + c]) / smax; | |
131 ++j; | |
132 } | |
133 while (j < blockSize) { | |
134 plugbuf[c][j] = 0.0f; | |
135 ++j; | |
136 } | |
137 | |
138 //} | |
139 } | |
140 | |
141 const char *output = reinterpret_cast <const char*> (plugbuf[0]); | |
142 Py_ssize_t len = (Py_ssize_t) channels*blockSize*4; | |
143 | |
144 PyObject* pyReturnBuffer = | |
145 PyString_FromStringAndSize(output,len); | |
146 | |
147 return pyReturnBuffer; | |
148 | |
149 | |
150 /* NOW return the data in a PyBuffer | |
151 | |
152 */ | |
153 | |
154 /* | |
155 char* test = PyString_AS_STRING(pyBuffer); | |
156 cerr << "Passed in: " << buflen << " str: " << test << endl; | |
157 | |
158 //convert the buffer to plugbuf | |
159 | |
160 //plugin->process | |
161 // (plugbuf, RealTime::frame2RealTime(frame, samplerate)) | |
162 | |
163 for(size_t k=0; k<channels; k++){ | |
164 delete[] plugbuf[k]; | |
165 } | |
166 delete[] plugbuf; | |
167 */ | |
168 return pyReturnBuffer; | |
169 | |
170 } |