8000 Add cross-platform audio file recording, >sf, and >sfo by chairbender · Pull Request #18 · ahihi/sapf · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add cross-platform audio file recording, >sf, and >sfo #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 27, 2025

Conversation

chairbender
Copy link
@chairbender chairbender commented Apr 7, 2025

Relates to #2 .

TLDR

Adds ability to record to file using record as well as >sf or >sfo

The >sf and >sfo stuff is contained within the SndfileSoundFile and SoundFiles files changes and is more straightforward because it's fine for it to block. Everything else is for record which is more complex...

The bulk of the logic here is in implementing / testing a cross-platform alternative to ExtAudioFileWriteAsync. Using a ring buffer which itself is non-blocking, it minimizes blocking the main thread and delegates the file IO to a separate thread. I have attempted to use a similar approach. The ring buffer itself isn't implemented by me (see the attribution at the top of it) but the rest is. Using optimistic concurrency, it should minimize the need for explicit locking (but falls back to that if needed).

After building this, you should be able to call record instead of play like so:

((.4 0 lfsaw) 2 * ([8 7.23] 0 lfsaw) .25 * 9.667 + + exp2) 0 sinosc .04 * .2 0 4 combn "test" record

For >sf and >sfo you can try (take 5 seconds of analog bubbles)

(([.4 [8 7.23]] 0 lfsaw) [24 3] * +/ 81 + nnhz) 0 sinosc 4c * .2 0 4 combn 5 T "test" >sfo

After you stop it, you should see the test.wav file output to your tmp folder (on windows it's in a different place then on OSX / Linux).

I've also tried to apply some things I've learned recently about writing modern C++ (I just binged the entirety of "A Tour of C++, Third Edition"), at least in new code.

Details

This is just a starting point and there's probably room for optimization. This ring buffer has a fixed total size, for example. There are some other constants I had to choose like chunk size. The size I chose was just an arbitrary guess. There is a different implementation I found elsewhere which can supposedly grow if needed which we could also consider.

It's multithreaded so I've commented a bit more heavily than usual to help explain my reasoning for why I think the implementation should be safe. But this is my first time writing multithreaded C++, so be warned.

@chairbender chairbender force-pushed the audio-write branch 2 times, most recently from fa8baaf to 8f4efdf Compare April 8, 2025 03:25
@chairbender chairbender marked this pull request as ready for review April 8, 2025 05:12
@chairbender chairbender changed the title Add cross-platform audio file recording. Add cross-platform audio file recording, >sf, and >sfo Apr 9, 2025
@chairbender
Copy link
Author
chairbender commented Apr 9, 2025

Um...so I just discovered >sf and >sfo today and since I ended up being able to get them working in only a few hours, I decided to tack it onto this PR.

tempDir = ".";
snprintf(path, len, "%s\\sapf-%s-%04d.wav", tempDir, gSessionTime, count);
#else
snprintf(path, len, "/tmp/sapf-%s-%04d.wav", gSessionTime, count);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like this could be simplified? im thinking the tempdir retrieval could be factored out, and maybe the last two snprintfs could be merged into one

@ahihi
Copy link
Owner
ahihi commented Apr 21, 2025

thanks for the extensive comments! this looks reasonable to me.

the list initialization syntax which youre using pervasively is new to me (had to look it up) and although it looks a bit weird, it does seem like the better option.

@chairbender
Copy link
Author
B500

Should be fixed. I refactored the tempdir retrieval.

@ahihi
Copy link
Owner
ahihi commented Apr 27, 2025

thank you!

@ahihi ahihi merged commit 08ad572 into ahihi:main Apr 27, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0