File: FeatureSave.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.7.1~repack-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,332 kB
  • ctags: 11,704
  • sloc: cpp: 140,180; lisp: 14,746; ansic: 2,133; xml: 86; makefile: 82; haskell: 21; sh: 8
file content (213 lines) | stat: -rw-r--r-- 4,396 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
	SuperCollider real time audio synthesis system
 Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "SC_PlugIn.h"

#include "stdio.h"

InterfaceTable *ft; 


struct FeatureSave : public Unit    
{
 
	int frameswritten_; 
	int numfeatures_; 
	//char * filename_;
	int fileready_; 
	
	//open file data
	FILE *fp;
	
};

extern "C" {  
	
	void FeatureSave_next(FeatureSave *unit, int inNumSamples);
	void FeatureSave_Ctor(FeatureSave* unit);
	//void FeatureSave_Dtor(FeatureSave* unit);

}


void FeatureSaveUnitCmdFunc (Unit *unit, struct sc_msg_iter *args); 
void FeatureSaveUnitCmdFunc2 (Unit *unit, struct sc_msg_iter *args); 



//will need to create in NRT mode

void FeatureSave_Ctor(FeatureSave* unit) {
	
	unit->frameswritten_ = 0; 
	unit->fileready_ = 0; 
	unit->numfeatures_ = IN0(0); 
	
	//printf("FeatureSave: numfeatures %d \n",unit->numfeatures_); 
	
	
	DefineUnitCmd("FeatureSave","createfile",FeatureSaveUnitCmdFunc);
	DefineUnitCmd("FeatureSave","closefile",FeatureSaveUnitCmdFunc2);
	
	
	//printf("FeatureSave sanity check: unit pointer %p \n",unit->mUnitDef); 
	
	
	//mCmds undefined here 
	//unit->mUnitDef->mCmds; 
	
	
	SETCALC(FeatureSave_next); 
	
}



void FeatureSave_next(FeatureSave *unit, int inNumSamples) {

	float trig = IN0(1); //trigger input

	//if trigger and file open 

	//printf("%f %d %d %d \n",trig, unit->fileready_, unit->numfeatures_, trig>(-0.01f));
	//printf("FeatureSave:next sanity check: unit pointer %p \n",unit->mUnitDef); 
	
	if((trig>(-0.01f)) && (unit->fileready_==1)) {
		
		//printf("%f %d \n",trig, unit->fileready_); 
		
		//write to file
		
		for (int i=0; i<unit->numfeatures_; ++i) {
		
			float featureval = IN0(i+2);  
			//printf("%d %f trig %f \n",i, featureval, trig); 
			
			//printf("featureval %f %d \n",featureval, unit->fileready_);
			
			
			fwrite(&featureval, sizeof(float),1, unit->fp);
			
		}
		
		
		++unit->frameswritten_; 
		
	}
	
	
}



void FeatureSaveUnitCmdFunc (Unit *unit, struct sc_msg_iter *args) {
	
	FeatureSave * fsave = (FeatureSave*) unit; 
	
	//message type:
	//int cmdtype = args->geti();
	 
	
	const char * stringarg = args->gets();
	
	//printf("got here at least %s\n",stringarg);
	
	//strcpy(fsave->filename_, stringarg);  
	
	//create file for writing 
	//won't push to NRT thread for now since assuming only using this UGen in NRT mode to start with
	
	//fsave->fp = fopen(fsave->filename_, "wb");
	fsave->fp = fopen(stringarg, "wb+");

	//dummy header data for now
	fwrite(&fsave->frameswritten_, sizeof(int),1, fsave->fp);
	
	fwrite(&fsave->numfeatures_, sizeof(int),1, fsave->fp);
		
	fsave->fileready_ = 1; 
	
}



void FeatureSaveUnitCmdFunc2 (Unit *unit, struct sc_msg_iter *args) {
	
	FeatureSave * fsave = (FeatureSave*) unit; 
	
	//message type:
	//int cmdtype = args->geti();
	
	fsave->fileready_ = 0; 
	
	//printf("ever called? %d \n", fsave->frameswritten_);
	
	//finalise header data: 
	
	//back to start
	//rewind(fsave->fp); 
	
	fseek(fsave->fp, 0, SEEK_SET);
	
	//int testval = 64; 
	//fwrite(&testval, sizeof(int),1, fsave->fp);
	
	fwrite(&fsave->frameswritten_, sizeof(int),1, fsave->fp);
	
//	int test; 
//	fread(&test, sizeof(int),1, fsave->fp);
//	printf("ever called 2? %d \n", test);
//	
//	
//	fseek(fsave->fp, 0, SEEK_END);
	
	//close file
	fclose(fsave->fp); 
	
	
}

PluginLoad(FeatureSave) {
	
	//FILE * testfile = fopen("blah", "wb");
//	
//	int numelements = 1000; 
//	
//	fwrite(&numelements, sizeof(int),1, testfile);
//	
//	for (int i=0; i<numelements; ++i) {
//		float now = i*0.67; 
//		fwrite(&now, sizeof(int),1, testfile);
//	
//	}
//
//	fclose(testfile); 
	
	
	ft = inTable;
	
	DefineSimpleUnit(FeatureSave); 
	
}