view src/MatchMarkers.cpp @ 56:4394c9490716 tip

minor changes
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Mon, 24 Dec 2012 18:58:39 +0000
parents 2eca10a31ae2
children
line wrap: on
line source
/*
 *  MatchMarkers.cpp
 *  MultipleAudioMathcher
 *
 *  Created by Andrew on 14/06/2012.
 *  Copyright 2012 QMUL. All rights reserved.
 *
 */

#include "MatchMarkers.h"

MatchMarkers::MatchMarkers(){
	load();
}


void MatchMarkers::addMarker(const double& markerTime){
	int i = 0;
	while (i < markers.size() && markers[i] < markerTime){
		i++;
	}
	vector<double>::iterator it;
	it = markers.begin();
	markers.insert(it + i, markerTime);

	printf("\n");
	for (i = 0;i< markers.size();i++)
		printf("marker[%i] %f\n", i, markers[i]);
	
}


void MatchMarkers::deleteMarker(const int& markerIndex){
	if (markerIndex >= 0 && markerIndex < markers.size()){
		vector<double>::iterator it;
		it = markers.begin();
		markers.erase(it+markerIndex);
	}
}

void MatchMarkers::saveMarkers(){
	std::string filePath = "../../../data/markers.txt";
	saveMarkers(filePath);
	
}

void MatchMarkers::saveMarkers(const std::string& filePath){
	if (!markerOutputFile.is_open()){
	markerOutputFile.open(filePath.c_str());
		for (int i = 0;i < markers.size();i++){
			markerOutputFile << markers[i] << endl;
		}
	markerOutputFile.close();
	}
}

void MatchMarkers::load(){
	loadMarkerFile("/Users/andrew/Documents/work/programming/of_preRelease_v007_osx/apps/myOpenFrameworks007/MultipleAudioMatcher/bin/data/lewesSavedMarkers.txt");
}



void MatchMarkers::loadMarkerFile(std::string filePath){
		markers.clear();
		
		printf("MARKERS : READ FILE '%s'\n", filePath.c_str());
	
		ifstream file ( filePath.c_str());
		string tmpLine, value;
		stringstream iss;

		while ( file.good() )
		{
			getline(file, tmpLine);
			iss << tmpLine;
		//	printf("tmp line %s\n", tmpLine.c_str());
			
			while(getline ( iss, value, '\n' )){ // read a string until next comma: http://www.cplusplus.com/reference/string/getline/
				//	cout << string( value, 1, value.length()-2 ); // display value removing the first and the last character from it
				//		printf("line:%s\n", value.c_str());
				string::size_type start = value.find_first_not_of(" ,\t\v\n");
				string firstpart = value.substr(start, string::npos);
				string::size_type end = firstpart.find_first_of(" ,\t\v\n");
				string part = firstpart.substr(0, end);
				markers.push_back(atof(part.c_str()));
				printf("marker[%i]: %f ms\n", (int)markers.size()-1, markers[markers.size()-1]);
			
			}//end while reading line
			iss.clear();
			
		}//end while
		
	
}


//--------------------------------------------------------------
void MatchMarkers::loadFile(){
	// first, create a string that will hold the URL
	string fileName;
	// openFile(string& URL) returns 1 if a file was picked
	// returns 0 when something went wrong or the user pressed 'cancel'
	int response = ofxFileDialogOSX::openFile(fileName);
	if(response){
		loadMarkerFile(fileName);
	}else {
		printf("Could not open marker file\n");
	}
}


//--------------------------------------------------------------
void MatchMarkers::saveFile(){
	// create a string to hold the folder URL
	string folderURL;
	// and one for the filename
	string fileName;
	// saveFile(string& folderURL, string& fileName) returns 1 if a folder + file were specified
	// returns 0 when something went wrong or the user pressed 'cancel'
	int response = ofxFileDialogOSX::saveFile(folderURL, fileName);
	if(response){
		// now you can use the folder URL and the filename.
		printf("\nfolder:'%s'\nfile'%s'\n", folderURL.c_str(), fileName.c_str());
		saveMarkers(folderURL+"/"+fileName);
	}else {
		printf("Could not save marker file\n");
	}
}