blog

here's what i'm working on

Sampling rate

October 16, 2021 — ~kai

Music is recorded and typically stored (encoded) using pulse code modulation (PCM). These files can get large, and FLAC is a common format that uses lossless compression to compress raw PCM files.

A digital audio file can be encoded in b bits and at a sampling rate of r kHz1. For example a “24/96” file is shorthand for b = 24 and r = 96.

Generally the maximum frequency stored in a file is r/2 (half the sampling rate). Humans can listen up to 20 kHz so it can be argued that a sampling rate r > 40 has no effect on enjoyment of music. CDs have r = 44.1.

The bit depth b helps give it dynamic range - how soft and loud it can get.

If someone gives you a file and claims it to be sampled at 96 kHz but a spectrogram (using software such as Spek or Sonic Visualizer) shows the maximum frequency to cut off at less than 48 kHz, then you can assume that they weren’t honest! It is likely that they took a lower quality file and converted it to FLAC.

Most of what I understood comes from here.

tags: music

read more...

Concat to PDF

September 17, 2021 — ~kai

I wrote a script a few months ago to scrape web novels. I extracted them as plaintext and saved every chapter. Since some books had over a hundred chapters, I wanted to concatenate them so it would be easier to read.

Concatenation was a little difficult if the files were sorted oddly or if only select files needed to be concatenated. I used a little KDE servicemenu to achieve this task in a fun way (check kf5-config --path services to see where it can be placed):

[Desktop Entry]
Type=Service
Icon=smiley-shape
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=all/allfiles;
Actions=mergeEntry;
Encoding=UTF-8

[Desktop Action mergeEntry]
Name=Merge selected file(s)
Icon=document-send
Exec=kdialog --msgbox "Will merge the following files:\n$(echo %F | head -c 500)..." && awk 'FNR==1{print ""}1' %F > "./merged$(date +'%s').out"

The neat thing about my process is that I was concatenating Markdown files. You know what that means? pandoc can parse these easily and give me a pretty PDF!

Font size larger (https://stackoverflow.com/a/46055046), use extarticle package, which also supports 14, 17, and 20pt:

pandoc -V geometry:margin=1in -V documentclass="extarticle" -V fontsize=14pt ...

If markdown:

pandoc -V geometry:margin=1in -V fontsize=12pt -f markdown -t pdf ... ...

If you’re converting books, try fonts (eg. Garamond) installed on your computer:

pandoc -V geometry:margin=1in -V fontsize=12pt -V mainfont="Garamond" -f markdown -t pdf ... ...

OR (check your ~/.fonts directory)

pandoc -V geometry:margin=1in -V fontsize=12pt -V mainfont="pala.tff" -f markdown -t pdf ... ...

If toc and chapters:

pandoc -V geometry:margin=1in -V fontsize=12pt -f markdown -t pdf myinput.md -o myoutput.pdf --toc

If want headers: --top-level-division=chapter

If CJK characters, read (https://stackoverflow.com/a/48090656), you must use xelatex and set a valid font:

pandoc -V geometry:margin=1in -V fontsize=12pt -V CJKmainfont="Noto Sans CJK JP" -f markdown -t pdf myinput.md -o myoutput.pdf --toc --pdf-engine=xelatex --standalone

If want headers:

-s aka --standalone

And the best settings, all together:

markdown to pdf:

pandoc -V geometry:margin=1in -V documentclass="extarticle" -V fontsize=14pt -V mainfont="Garamond" -V CJKmainfont="Noto Sans CJK JP" -f markdown -t pdf myinput.md -o myoutput.pdf --toc --pdf-engine=xelatex --standalone

epub to pdf:

pandoc -V geometry:margin=1in -V fontsize=12pt -V mainfont="Garamond" -V CJKmainfont="Noto Sans CJK JP" -f epub -t pdf myinput.epub -o myoutput.pdf

tags: code

Overriding exit to tmux detach

September 17, 2021 — ~kai

I was using mosh like this:

function mssh {
    echo "> mosh --no-init $1 -- tmux new-session -A -s main"
    mosh --no-init $1 -- tmux new-session -A -s main
}

But sometimes I would enter exit instead of tmux detach and I would lose the session I had on the remote!

So I found this code block that I inserted into my remote ~/.bashrc that would help me. It’s simple to read, if the session is not tmux, we exit, but if it is, check if it is desirable to preserve the session.

# also see https://medium.com/@toja/tip-using-mosh-with-scrollback-257a54a848b3
exit() {
  if [[ -z $TMUX ]]; then
    builtin exit
    return
  fi

  panes=$(tmux list-panes | wc -l)
  wins=$(tmux list-windows | wc -l)
  count=$(($panes + $wins - 1))
  if [ $count -eq 1 ]; then
    tmux detach
  else
    builtin exit
  fi
}

Note, pressing (CTRL+D) will still exit the session, if you want.

tags: code

Resolving changes overwritten

September 17, 2021 — ~kai

So I got this error the other day when I ran git pull: “Your local changes to the following files would be overwritten by merge:” because my package.json file was outdated.

I always keep git update-index --assume-unchanged for my package.json so it was frustrating that git is trying to overwrite it. (I know I could edit the .gitignore, but there are other developers on this project.)

Anyway, in my heart of hearts, I knew that keeping the new package.json is probably better, so this is what I did:

git update-index --no-assume-unchanged package.json package-lock.json
git checkout -b k-localchanges1
git add -v .
git commit -m "message" # I'm backing up my copy of package.json into my local branch
git checkout development
git pull
rm -rf node_modules/
npm install
npx expo -c

tags: git, code

Reaper and Linux

September 13, 2021 — ~kai

Are you brave enough to produce music on Linux?

To be honest, this is just a guide/notes for myself because I’m not the kind of person to instantly get it on the first try. I’ll be referencing this myself from time to time.

setup

Well, if you are brave enough, one of the more full-fledged DAWs out there that support Linux is Reaper. It’s not open source (as I know of), but it is probably more powerful than the ones that are open source. And it’s also very lightweight, which was perfect for my use cases.

Download, extract Reaper from their website https://reaper.fm, I’m sure you can figure out how. At the time of this post, I was using 6.36.

cd into that folder, ~/Desktop/somewhere/reaper_linux_x86_64/REAPER/, and launch it directly. You don’t need to run install-reaper.sh. Personally I’m never a fan of stuff that mess with my system, just run the executable.

cd REAPER
./reaper

Okay, now if everything looks good, install sfizz, which is an open source VST plugin that lets you play soundfonts. Here’s the downloads page here and here’s the GitHub page here.

AFAIK sfizz is the only tool capable of playing Virtual Playing Orchestra and Freepats well enough.

Finally, find out where it’s installed, because the docs never explain it anywhere.

dpkg -L sfizz 
# I see most vst3 files under /usr/lib/vst3

Now, in Reaper, we want it to look into directories for grabbing the VST plugins. So, go to Options > Preferences > Plug-ins > VST and add /usr/lib/vst3/; as one of the paths to scan. Then, hit Re-scan....

soundfonts

Okay, you can download soundfonts now. The ones I recommend are the standard Virtual Playing Orchestra here and FreePats various sound fonts here. I don’t know if there are any mirrors for these sound banks. You should get the WAV formats of these files since they are uncompressed .

.sfz files are basically metadata that map each note to the corresponding WAV file. sfizz uses the .sfz file to play the appropriate note when called for.

Download and extract the ZIPs from FreePats in the directory ~/Documents/SFZ instruments/, which is the default path sfizz looks at scanning for .sfz files.

actually using Reaper

So, the fastest way to get started is to import a MIDI file into Reaper. MIDI files are essentially sheet music in plaintext form. If you use MuseScore, you can generate MIDIs pretty easily. Or you can use vanbasco’s search engine to find MIDIs of your favorite music that other people have already created. You’re probably smart enough to figure out the technicalities, so I’ll leave that out.

Import the MIDI file with the default settings: Expand n MIDI tracks… Import MIDI tempo map… and Import MIDI markers.

If you try to play it, you won’t hear anything. That’s because the tracks aren’t mapped to any sounds.

The very first thing you should do is configure your audio playback settings (Options > Preferences > Device > Audio Systems > PulseAudio).

For each track, there’s probably a name. You need a soundfont to play that track. To do this, click the [FX] button and find VST3i - you should see sfizz. If it exists, that’s good. For now, we only want to see if sound works. So, instaed of using sfizz, go to Categories > Synth > ReaSynth which is the default synth. Now scroll to a position where the track has notes, and press play.

  • If the green bar flashes, Reaper recognizes it is playing something.
    • If the green bar flashes, but you don’t hear audio output, fix your audio preferences in Options > Preferences > Device > Audio Systems and try again.
  • If the green bar does not flash, you messed up when setting up the synth. Try going to FX and Adding the correct synth.

Now, if everything has been good so far, you can see if the soundfonts work.

Press [FX] and either (a) Remove the synth (the button is on the bottom left of the FX pop-up window or (b) uncheck the ReaSynth plugin.

Then, in the same screen Add > VST3i > sfizz. Click the little dropdown button next to where it says “No file”, navigate to where your sfz is, and double click on the .sfz file. If it loads properly, you can press the little piano keyboard at the bottom and you should hear notes.

Some .sfz formats may not support sfizz. Virtual Playing Orchestra and FreePats have been tested and should work properly on sfizz.

If you play the piece now, it should make the sound of the .sfz you selected.

A note of caution: in my experience sfizz randomly breaks if you press CTRL+A (select the whole piece) or HOME (go to the front of the piece). The only way I know to fix this is to go to FX, click on sfizz, select a different .sfz and go back to the current .sfz. A quick way to do this is to press < and > if there are multiple .sfz in that folder (e.g. Virtual Playing Orchestra).

more reaper things

I’ll add more stuff here by editing this post or just making a new post. Until next time!

tags: music, reaper

Does this work?

August 16, 2021 — ~kai

Here’s my first post. *uh oh*

I’m going to try and use this to write short posts and notes. Don’t expect any essays here.

tags: misc