⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tchord.cpp

📁 在linux下多媒体开发实例linux下多媒体开发
💻 CPP
字号:
//  tchord.cpp// guitar chord database and sound functions#include <fstream.h>#include <string.h>#include <v/vdebug.h>#include "tchord.h"#include "tutils.h"// Constructor. Just calls load routine.tutChords::tutChords(char *fileName){  UserDebug1(Constructor, "tutChords::tutChords(%s)\n", fileName);  load(fileName);}// Load chord database file. Returns -1 on error.int tutChords::load(char *fileName){  UserDebug1(Misc, "tutChords::load(%s)\n", fileName);  fstream fs(fileName, ios::in);  if (fs.bad()) {    UserDebug1(BadVals, "tutChords::load(%s) can't open file\n", fileName);    /*    return -1; */  }  // check for "magic number" at top of file  char tmp[255];  fs >> tmp;  if (strcmp(tmp, "#GuitarTutor")) {    UserDebug1(BadVals, "tutChords::load(%s) not a valid data file\n", fileName);    return -1;  }  strcpy(tutChords::fileName, fileName);  numChords = 0;  char longName[255], shortName[255], strings[255], sampleFile[255];         do {    fs >> longName >> shortName >> strings >> sampleFile;    if (strlen(sampleFile)) {      // sanity check on strings field      if ((strlen(strings) != numStrings) ||	  (strspn(strings, "X012345678") != numStrings)) {	UserDebug1(BadVals, "tutChords::load(%s) bad data in file\n",		   fileName);	return -1;      }      // input file uses underscore to indicate space      StrSub(longName, '_', ' ');      StrSub(shortName, '_', ' ');      strcpy(chord[numChords].longName, longName);      strcpy(chord[numChords].shortName, shortName);      strcpy(chord[numChords].strings, strings);      strcpy(chord[numChords].sampleFile, sampleFile);      chordList[numChords] = chord[numChords].longName;      numChords++;      if (numChords >= maxChords) { // check for array overflow	UserDebug1(BadVals, "tutChords::load(%s) too many chords in file\n",		   fileName);	return -1;      }      chordList[numChords] = 0; // clear next entry    }  }  while (!fs.eof());  UserDebug1(Misc, "tutChords::load() read %d chords\n", numChords);  fs.close();  if (numChords == 0)    return -1;  else    return 0;}// Given chord name, return fingering info. Returns -1 on error.int tutChords::fingering(char *longName, strings_t &strings){  UserDebug1(Misc, "tutChords::fingering(%s)\n", longName);  for (int i = 0 ; i < numChords ; i++) {    if (!strcmp(longName, chord[i].longName)) {      strings = chord[i].strings;      return 0;    }  }  UserDebug1(BadVals, "tutChords::fingering(%s) can't find chord\n", longName);  return -1;}// Save chord database to file.int tutChords::save(char *filename){  UserDebug1(Misc, "tutChords::save(%s)\n", fileName);  fstream fs(filename, ios::out);  if (fs.bad()) {    UserDebug1(BadVals, "tutChords::save(%s) can't open file\n", fileName);    return -1;  }    // write "magic number" at top of file  fs << "#GuitarTutor" << endl;  for (int i = 0 ; i < numChords ; i++) {    char longName[255], shortName[255];    strcpy(longName, chord[i].longName);    strcpy(shortName, chord[i].shortName);    // convert spaces to underscore    StrSub(longName, ' ', '_');    StrSub(shortName, ' ', '_');    fs << longName << " " << shortName << " " <<      chord[i].strings[0] << chord[i].strings[1] <<      chord[i].strings[2] << chord[i].strings[3] <<      chord[i].strings[4] << chord[i].strings[5] <<      " " << chord[i].sampleFile << endl;  }  fs.close();  return 0;}// Play a chord. Returns -1 on error.int tutChords::play(char *chordName){  UserDebug1(Misc, "tutChords::play(%s)\n", chordName);  char *sampleFile = 0;  const char *sound = "/dev/dsp"; // sound device  // find the sample  for (int i = 0 ; i < numChords ; i++) {    if (!strcmp(chordName, chord[i].longName)) {      sampleFile = chord[i].sampleFile;      break;    }  }  // this chord was not found  if (sampleFile == 0) {    UserDebug1(BadVals, "tutChords::play(%s) can't find chord\n", chordName);    return -1;  }  // open sample file for read  fstream in(sampleFile, ios::in);  if (in.bad()) {    UserDebug1(BadVals, "tutChords::play() can't open %s\n", sampleFile);    return -1;  }  // open DSP for write  fstream out(sound, ios::out);  if (out.bad()) {    UserDebug1(BadVals, "tutChords::play() can't open %s\n", sound);    return -1;  }  // read sample file, write to DSP  unsigned char buf;  while (!in.eof()) {    in >> buf;    out << buf;  }  // close files  out.close();  in.close();  return 0;}// return array of chord namesvoid tutChords::chordNames(chordList_t list){  UserDebug(Misc, "tutChords::chordNames()\n");  int i;  for (i = 0 ; i < numChords ; i++)    list[i] = chordList[i];  list[i] = 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -