Skip to content

Commit

Permalink
tape: return false immediately on open() if stream is running (#1770)
Browse files Browse the repository at this point in the history
* tape: return false immediately on open() if stream is running

* tape: cleanup prints and comments
  • Loading branch information
catfact authored Mar 11, 2024
1 parent 28bb75c commit f5c3f3c
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions crone/src/Tape.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ namespace crone {
virtual // from any thread
void start() {
if (isRunning) {
std::cout << "Tape::SfStream::start(): already running" << std::endl;
return;
} else {
std::cout << "Tape::SfStream: starting..." << std::endl;
envIdx = 0;
envState = Starting;

this->th = std::make_unique<std::thread>(
[this]() {
this->diskLoop();
Expand Down Expand Up @@ -127,7 +130,7 @@ namespace crone {
envIdx = 0;
envState = Stopped;
shouldStop = true;
std::cerr << "Tape: fade-out finished; stopping" << std::endl;
std::cout << "Tape: fade-out finished; stopping" << std::endl;
}
}

Expand Down Expand Up @@ -164,7 +167,7 @@ namespace crone {

if (bytesToPush > bytesAvailable) {
#if 0
std::cerr << "Tape: writer overrun: "
std::cout << "Tape: writer overrun: "
<< bytesAvailable << " bytes available; "
<< bytesToPush << " bytes to push; "
<< numFramesCaptured << " frames captured"
Expand Down Expand Up @@ -229,7 +232,7 @@ namespace crone {

if (framesToWrite > (int) maxFramesToWrite) {
// _really_ shouldn't happen
std::cerr << "warning: Tape::Writer has too many frames to write" << std::endl;
std::cout << "warning: Tape::Writer has too many frames to write" << std::endl;
framesToWrite = (int) maxFramesToWrite;
}

Expand All @@ -243,22 +246,22 @@ namespace crone {
if (sf_writef_float(this->file, diskOutBuf, framesToWrite) != framesToWrite) {
char errstr[256];
sf_error_str(nullptr, errstr, sizeof(errstr) - 1);
std::cerr << "error: Tape::writer failed to write (libsndfile: " << errstr << ")" << std::endl;
std::cout << "error: Tape::writer failed to write (libsndfile: " << errstr << ")" << std::endl;
this->status = EIO;
break;
}

numFramesCaptured += framesToWrite;
if (numFramesCaptured >= maxFrames) {
std::cerr << "Tape: writer exceeded max frame count; aborting.";
std::cout << "Tape: writer exceeded max frame count; aborting.";
break;
}

}

std::cerr << "Tape::writer closing file...";
std::cout << "Tape::writer closing file...";
sf_close(this->file);
std::cerr << " done." << std::endl;
std::cout << " done." << std::endl;
SfStream::isRunning = false;
}

Expand All @@ -267,6 +270,13 @@ namespace crone {
size_t maxFrames = JACK_MAX_FRAMES, // <-- ridiculous big number
int sampleRate = 48000,
int bitDepth = 24) {

if (SfStream::isRunning) {
std::cout << "Tape Writer::open(): stream is running; no action was taken" << std::endl;
return false;
}


SF_INFO sf_info;
int short_mask;

Expand Down Expand Up @@ -295,7 +305,7 @@ namespace crone {
if ((this->file = sf_open(path.c_str(), SFM_WRITE, &sf_info)) == NULL) {
char errstr[256];
sf_error_str(nullptr, errstr, sizeof(errstr) - 1);
std::cerr << "cannot open sndfile" << path << " for output (" << errstr << ")" << std::endl;
std::cout << "cannot open sndfile" << path << " for output (" << errstr << ")" << std::endl;
return false;
}

Expand Down Expand Up @@ -337,6 +347,7 @@ namespace crone {
private:
// prime the ringbuffer
void prime() {
// std::cout << "priming tape reader" << std::endl;
jack_ringbuffer_t *rb = this->ringBuf.get();
size_t framesToRead = jack_ringbuffer_write_space(rb) / frameSize;
if (framesToRead > maxFramesToRead) { framesToRead = maxFramesToRead; };
Expand All @@ -347,7 +358,7 @@ namespace crone {

// couldn't read enough, file is shorter than the buffer
if (framesRead < framesToRead) {
std::cerr << "Tape::Reader: short file, disable loop" << std::endl;
std::cout << "Tape::Reader: short file, disable loop" << std::endl;
SfStream::shouldStop = false;
}
}
Expand Down Expand Up @@ -425,22 +436,26 @@ namespace crone {

// from any thread
bool open(const std::string &path) {
SF_INFO sfInfo;
if (SfStream::isRunning) {
std::cout << "Tape Reader::open(): stream is running; no action was taken" << std::endl;
return false;
}

SF_INFO sfInfo;
if ((this->file = sf_open(path.c_str(), SFM_READ, &sfInfo)) == NULL) {
char errstr[256];
sf_error_str(0, errstr, sizeof(errstr) - 1);
std::cerr << "Tape Reader:: cannot open sndfile" << path << " for output (" << errstr << ")" << std::endl;
std::cout << "Tape Reader:: cannot open sndfile" << path << " for output (" << errstr << ")" << std::endl;
return false;
}

if (sfInfo.frames < 1) {

std::cerr << "Tape Reader:: error reading file " << path << " (no frames available)" << std::endl;
std::cout << "Tape Reader:: error reading file " << path << " (no frames available)" << std::endl;
return false;
}
this->frames = static_cast<size_t>(sfInfo.frames);
std::cerr << "Tape Reader:: file size " << this->frames << " samples" << std::endl;
std::cout << "Tape Reader:: file size " << this->frames << " samples" << std::endl;
inChannels = sfInfo.channels;
if (inChannels > NumChannels)
return 0;//more than stereo is going to break things
Expand Down Expand Up @@ -498,12 +513,13 @@ namespace crone {
if (loopFile) {
// couldn't perform full read so must be end of file. Seek to start of file and keep reading
while (framesRead < framesToRead) {
// std::cout << "tape reader: couldn't perform full read; looping file..." << std::endl;
sf_seek(this->file,0, SEEK_SET);
auto nextRead = (size_t) sf_readf_float(this->file, diskBufPtr, framesToRead-framesRead);
if (nextRead < 1)
{
//Shouldn't happen
std::cerr << "Tape::Reader: unable to read file" << std::endl;
std::cout << "Tape::Reader: unable to read file" << std::endl;
SfStream::shouldStop = true;
break;
}
Expand All @@ -514,7 +530,6 @@ namespace crone {
}
}
else {
std::cerr << "Tape::Reader::diskloop() reached EOF" << std::endl;
SfStream::shouldStop = true;
}

Expand All @@ -525,7 +540,6 @@ namespace crone {

}
sf_close(this->file);
std::cerr << "Tape::reader closed file" << std::endl;
}

}; // Reader class
Expand Down

0 comments on commit f5c3f3c

Please sign in to comment.