You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.6KB

  1. /***************************************************/
  2. /*! \class Skini
  3. \brief STK SKINI parsing class
  4. This class parses SKINI formatted text
  5. messages. It can be used to parse individual
  6. messages or it can be passed an entire file.
  7. The SKINI specification is Perry's and his
  8. alone, but it's all text so it shouldn't be too
  9. hard to figure out.
  10. SKINI (Synthesis toolKit Instrument Network
  11. Interface) is like MIDI, but allows for
  12. floating-point control changes, note numbers,
  13. etc. The following example causes a sharp
  14. middle C to be played with a velocity of 111.132:
  15. noteOn 60.01 111.132
  16. See also SKINI.txt.
  17. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  18. */
  19. /***************************************************/
  20. #include "Skini.h"
  21. #include "SKINItbl.h"
  22. #include <cstdlib>
  23. #include <sstream>
  24. namespace stk {
  25. Skini :: Skini()
  26. {
  27. }
  28. Skini :: ~Skini()
  29. {
  30. }
  31. bool Skini :: setFile( std::string fileName )
  32. {
  33. if ( file_.is_open() ) {
  34. oStream_ << "Skini::setFile: already reaading a file!";
  35. handleError( StkError::WARNING );
  36. return false;
  37. }
  38. file_.open( fileName.c_str() );
  39. if ( !file_ ) {
  40. oStream_ << "Skini::setFile: unable to open file (" << fileName << ")";
  41. handleError( StkError::WARNING );
  42. return false;
  43. }
  44. return true;
  45. }
  46. long Skini :: nextMessage( Message& message )
  47. {
  48. if ( !file_.is_open() ) return 0;
  49. std::string line;
  50. bool done = false;
  51. while ( !done ) {
  52. // Read a line from the file and skip over invalid messages.
  53. if ( std::getline( file_, line ).eof() ) {
  54. oStream_ << "// End of Score. Thanks for using SKINI!!";
  55. handleError( StkError::STATUS );
  56. file_.close();
  57. message.type = 0;
  58. done = true;
  59. }
  60. else if ( parseString( line, message ) > 0 ) done = true;
  61. }
  62. return message.type;
  63. }
  64. void Skini :: tokenize( const std::string& str,
  65. std::vector<std::string>& tokens,
  66. const std::string& delimiters )
  67. {
  68. // Skip delimiters at beginning.
  69. std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  70. // Find first "non-delimiter".
  71. std::string::size_type pos = str.find_first_of(delimiters, lastPos);
  72. while (std::string::npos != pos || std::string::npos != lastPos) {
  73. // Found a token, add it to the vector.
  74. tokens.push_back(str.substr(lastPos, pos - lastPos));
  75. // Skip delimiters. Note the "not_of"
  76. lastPos = str.find_first_not_of(delimiters, pos);
  77. // Find next "non-delimiter"
  78. pos = str.find_first_of(delimiters, lastPos);
  79. }
  80. }
  81. long Skini :: parseString( std::string& line, Message& message )
  82. {
  83. message.type = 0;
  84. if ( line.empty() ) return message.type;
  85. // Check for comment lines.
  86. std::string::size_type lastPos = line.find_first_not_of(" ,\t", 0);
  87. std::string::size_type pos = line.find_first_of("/", lastPos);
  88. if ( std::string::npos != pos ) {
  89. oStream_ << "// Comment Line: " << line;
  90. handleError( StkError::STATUS );
  91. return message.type;
  92. }
  93. // Tokenize the string.
  94. std::vector<std::string> tokens;
  95. this->tokenize( line, tokens, " ,\t");
  96. // Valid SKINI messages must have at least three fields (type, time,
  97. // and channel).
  98. if ( tokens.size() < 3 ) return message.type;
  99. // Determine message type.
  100. int iSkini = 0;
  101. while ( iSkini < __SK_MaxMsgTypes_ ) {
  102. if ( tokens[0] == skini_msgs[iSkini].messageString ) break;
  103. iSkini++;
  104. }
  105. if ( iSkini >= __SK_MaxMsgTypes_ ) {
  106. oStream_ << "Skini::parseString: couldn't parse this line:\n " << line;
  107. handleError( StkError::WARNING );
  108. return message.type;
  109. }
  110. // Found the type.
  111. message.type = skini_msgs[iSkini].type;
  112. // Parse time field.
  113. if ( tokens[1][0] == '=' ) {
  114. tokens[1].erase( tokens[1].begin() );
  115. if ( tokens[1].empty() ) {
  116. oStream_ << "Skini::parseString: couldn't parse time field in line:\n " << line;
  117. handleError( StkError::WARNING );
  118. return message.type = 0;
  119. }
  120. message.time = (StkFloat) -atof( tokens[1].c_str() );
  121. }
  122. else
  123. message.time = (StkFloat) atof( tokens[1].c_str() );
  124. // Parse the channel field.
  125. message.channel = atoi( tokens[2].c_str() );
  126. // Parse the remaining fields (maximum of 2 more).
  127. int iValue = 0;
  128. unsigned int iToken = iValue + 3; //rgh: MIDI extension argument counts are different from regular MIDI
  129. long dataType = skini_msgs[iSkini].data2;
  130. while ( dataType != NOPE ) {
  131. // if ( tokens.size() <= (unsigned int) (iValue+3) ) { //rgh: test iToken rather than always testing iValue+3
  132. // if (tokens.size() <= iToken) { //rgh: iToken only tests it more tokens are to be consumed (SK_INT, SK_DBL, SK_STR)
  133. if ((tokens.size() <= iToken) && (dataType < 0)) { //Don't fail if remaining iValues come from skini_msgs[] rather than tokens[].
  134. oStream_ << "Skini::parseString: inconsistency between type table and parsed line:\n " << line;
  135. handleError( StkError::WARNING );
  136. return message.type = 0;
  137. }
  138. switch ( dataType ) {
  139. case SK_INT:
  140. message.intValues[iValue] = atoi( tokens[iToken].c_str() ); //rgh: use new index
  141. message.floatValues[iValue] = (StkFloat) message.intValues[iValue];
  142. ++iToken; //rgh: increment token index and value index (below)
  143. break;
  144. case SK_DBL:
  145. message.floatValues[iValue] = atof( tokens[iToken].c_str() ); //rgh: use new index
  146. message.intValues[iValue] = (long) message.floatValues[iValue];
  147. ++iToken; //rgh: increment token index and value index (below)
  148. break;
  149. case SK_STR: // Must be the last field.
  150. message.remainder = tokens[iToken]; //rgh: use new index
  151. return message.type;
  152. default: // MIDI extension message
  153. message.intValues[iValue] = dataType;
  154. message.floatValues[iValue] = (StkFloat) message.intValues[iValue];
  155. //iValue--; //rgh: iValue must increment even when iToken does not; resetting iValue only works sometimes
  156. }
  157. if ( ++iValue == 1 )
  158. dataType = skini_msgs[iSkini].data3;
  159. else
  160. dataType = NOPE;
  161. }
  162. return message.type;
  163. }
  164. std::string Skini :: whatsThisType(long type)
  165. {
  166. std::string typeString;
  167. for ( int i=0; i<__SK_MaxMsgTypes_; i++ ) {
  168. if ( type == skini_msgs[i].type ) {
  169. typeString = skini_msgs[i].messageString;
  170. break;
  171. }
  172. }
  173. return typeString;
  174. }
  175. std::string Skini :: whatsThisController( long number )
  176. {
  177. std::string controller;
  178. for ( int i=0; i<__SK_MaxMsgTypes_; i++) {
  179. if ( skini_msgs[i].type == __SK_ControlChange_ &&
  180. number == skini_msgs[i].data2) {
  181. controller = skini_msgs[i].messageString;
  182. break;
  183. }
  184. }
  185. return controller;
  186. }
  187. } // stk namespace