Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Files Functions Variables Typedefs Macros Groups
WriteFile.h
1 /*
2  * WriteFile.h
3  *
4  * Created on: 5 Oct 2015
5  * Author: giulio
6  */
7 
8 #ifndef WRITEFILE_H_
9 #define WRITEFILE_H_
10 #include <Bela.h>
11 #include <vector>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 
17 typedef enum {
18  kBinary,
19  kText
20 } WriteFileType;
21 
22 class WriteFile {
23 private:
24  static AuxiliaryTask writeAllFilesTask;
25  bool echo;
26  int echoedLines;
27  int echoPeriod;
28  char *header;
29  char *footer;
30  char *stringBuffer;
31  int stringBufferLength;
32  std::vector<float> buffer;
33  int textReadPointer;
34  int binaryReadPointer;
35  int writePointer;
36  bool variableOpen;
37  char* format;
38  int lineLength;
39  WriteFileType fileType;
40  static int sleepTimeMs;
41  FILE *file;
42  char* _filename;
43  void writeLine();
44  void writeHeader();
45  void writeFooter();
46  void allocateAndCopyString(const char* source, char** destination);
47  void print(const char* string);
48  void printBinary(const char* string);
49  void setLineLength(int newLineLength);
50  int getOffsetFromPointer(int aPointer);
51  std::vector<char *> formatTokens;
52  static void sanitizeString(char* string);
53  static void sanitizeString(char* string, int numberOfArguments);
54  static bool isThreadRunning();
55  static bool auxiliaryTaskRunning;
56  static bool threadShouldExit();
57  static bool threadIsExiting;
58  static bool threadRunning;
59  static bool threadScheduled;
60  static bool staticConstructed;
61  static void staticConstructor();
62  static std::vector<WriteFile *> objAddrs;
63  void writeOutput(bool flush);
64 public:
65  WriteFile();
66  WriteFile(const char* filename, bool overwrite, bool append);
67 
74  void setFileType(WriteFileType newFileType);
75 
80  void setEcho(bool newEcho);
81 
86  void setEchoInterval(int newPeriod);
87 
95  void setBufferSize(unsigned int newSize);
96 
103  void setFormat(const char* newFormat);
109  void setHeader(const char* newHeader);
115  void setFooter(const char* newFooter);
116 
120  void log(float value);
124  void log(const float* array, int length);
125 
132  void setup(const char* filename, bool overwrite = false, bool append = false);
133 
138  int getOffset();
139 
146  float getBufferStatus();
147  void cleanup();
148  ~WriteFile();
149  static int getNumInstances();
150  static void writeAllHeaders();
151  static void writeAllFooters();
152  static void writeAllOutputs(bool flush);
153  static void startThread();
154  static void stopThread();
155  static void run(void*);
163  static char* generateUniqueFilename(const char* original);
164 };
165 
166 #endif /* WRITEFILE_H_ */
void setup(const char *filename, bool overwrite=false, bool append=false)
Definition: WriteFile.cpp:111
void setEchoInterval(int newPeriod)
Definition: WriteFile.cpp:151
void setEcho(bool newEcho)
Definition: WriteFile.cpp:148
float getBufferStatus()
Definition: WriteFile.cpp:275
int getOffset()
Definition: WriteFile.cpp:285
void setFooter(const char *newFooter)
Definition: WriteFile.cpp:362
static char * generateUniqueFilename(const char *original)
Definition: WriteFile.cpp:59
void log(float value)
Definition: WriteFile.cpp:199
void setFileType(WriteFileType newFileType)
Definition: WriteFile.cpp:143
void setBufferSize(unsigned int newSize)
Definition: WriteFile.cpp:159
Definition: WriteFile.h:22
Main Bela public API.
void * AuxiliaryTask
Definition: Bela.h:456
void setFormat(const char *newFormat)
Definition: WriteFile.cpp:222
void setHeader(const char *newHeader)
Definition: WriteFile.cpp:357