Tales from the jar side: Java Audio using OpenAI, 1K vs 1 thousand, Pop-Tarts Bowl memes, and the usual silly tweets, toots, and skeets
My wife told me I've grown as a person. Actually she said, "you've gotten fat," but I knew what she meant. (rimshot, and no, she didn't say that)
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of December 24 - 31, 2023. No classes this week. Happy Holidays!
Here are the regular info messages:
Regular readers of and listeners to, and video viewers of this newsletter are affectionately known as jarheads, and are far more intelligent, sophisticated, and attractive than the average newsletter reader or listener or viewer. If you wish to become a jarhead, please subscribe using this button:
As a reminder, when this message is truncated in email, click on the title to open it in a browser tab formatted properly for both the web and mobile.
Easy Java Audio with OpenAI
Here’s the latest video on the Tales from the jar side YouTube channel:
It’s about using OpenAI’s TTS (text-to-speech) service that turns text into audio files, which I accessed using Java. That turned out to be remarkably easy, because all you have to supply is the text itself and a few parameters, and the service returns a generated mp3 file very quickly.
The API documentation shows a sample curl request that returns an mp3 file:
curl https://api.openai.com/v1/audio/speech \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1",
"input": "The quick brown fox jumped over the lazy dog.",
"voice": "alloy"
}' \
--output speech.mp3
There are two models available, tts-1 and tts-1-hd. The HD version sounds a bit better but not dramatically so. It does cost double, however:
Still, neither are terribly expensive. My tests only sent a hundred words or so, which means even with the HD model at 3 cents per 1000 characters (I’m assuming that when they say 1K they mean 1000 rather than 1024 (see below), since that way they can charge more) I’m spending less than a penny each time I use the service.
There are six voices available, named alloy
, echo
, fable
, onyx
, nova
, and shimmer
. You can listen to samples of them on the guide page.
My implementation used Java’s HTTP Client API and Google’s Gson parser to transmit the request. The response comes back as an HttpResponse<byte[]>
when I ask for a byte array, which I store as an mp3 file.
To make the video a bit more interesting, I included two other items:
I did some input validation on the Java record mapped to the JSON structure. That involved using the “compact canonical constructor” for a record, which I haven’t discussed in class before but I think I will in the future, and
I needed an mp3 player I could invoke programmatically from Java. I tried using either the
AudioClip
orMediaPlayer
classes from JavaFX, but ran into problems calling them from test cases. The audio would start, but either cut out or refuse to play entirely unless I did aThread.sleep
in the test case, which is not a good practice. In the end, I used a very old Java library called JLayer, which gave me aPlayer
class whose constructor took anInputStream
as an argument, and then all I had to do was invoke itsplay()
method:
That’s all it took. You can find all the source code in this GitHub repository. To give you an idea about the results, here’s one of my test cases:
Here’s the generated audio file:
I hope that embed works. If not, it should work on the Substack site for this post, which you can access by clicking on the title of the post itself.
My plan for next week is to complete a similar system using the Spring framework, taking advantage of its HTTP exchange interfaces and included Jackson parser. I also intend to replace the validation with the Bean Validation specification, which is easy to include in a Spring app, and I may even try to convert it to a native image using the GraalVM native image compiler. After all, this is a very natural stateless service, since everything it needs is passed in as input parameters with each request. More about that next week, of course.
(Today’s newsletter is around 10,000 characters, so it would cost me about 30 cents to convert to an mp3 in HD, or 15 cents in the default resolution. Just FYI.)
Now THAT’S 1K:
I had some trouble explaining to my wife that while 1000 subscribers on the YouTube channel was nice, the number I was really waiting for was 1024, because otherwise I didn’t feel right referring to it as 1K.
Fortunately, that occurred a few days later:
For the handful of non-nerds who read this newsletter:
1K is 2^10 = 1024 (first binary over 1000)
1M is 2^20 is 1024K = 1,048,576 (first binary over 1 million)
1G is 2^30 is 1024M = 1,073,741,824 (first binary over 1 billion)
and so on.
It’s all binary, of course. That’s how you measure hard drive sizes, the amount of memory you have in your machine, and so on.
As long as I’m showing off, I didn’t realize this was a metric anybody tracked, but I’ll take it:
I guess I need to wait until that number hits 20,480 in order to claim I have 20K total views.
Blast from the past
A friend posted this memory on Facebook:
My dorm at MIT was technically called New House 5, but we called it Desmond House after good old Thomas C. Desmond '09 (that’s 1909, not 2009). The picture is from the Spring of 1981. I can virtually guarantee you can’t find me, so I’ll give you a hint: I’m the nerd in the T-shirt. Also, I still had dark hair. A lot of it, actually.
(Okay, okay. I’m at the top of the stairs, in the upper right corner, wearing glasses. Geez, that was a long time ago.)
Pop-Tarts Gets It
I rarely watch college football. I don’t like the way they pretend the athletes are anything other than unpaid labor, except for those rare few who qualify for an NIL (name/image/licensing) deal, which is yet another example of the rich getting richer. Also, when I was growing up my home team was Penn State, and we all idolized Joe Paterno as the platonic ideal of what a college football coach should be. That was, of course, before the whole Jerry Sandusky scandal finally came out and we realized JoePa had willfully ignored a child molester on his staff for literally decades. I’m sorry, but I think Penn State should have had its football team banned forever.
On a more practical note, I dislike any sport where the top teams are selected by committee, especially when the choices are based on popularity and potential TV ratings, which is exactly what happened this year. Again.
Still, once you get away from the top schools where money dominates everything, every once in a while you get some quirky bowl game where something unexpected happens that crosses into the mainstream. This year that event was the inaugural Pop-Tarts Bowl, featuring an edible Pop-Tart mascot. At the end of the game, it descended into a giant toaster (holding up an ironic sign reading “Dreams really do come true”), came out the other side, and the winning team ate him/it (!).
Seriously. It’s worth watching the first 30 seconds of this video, but not the whole thing, because eventually horrors abound.
As you might imagine, Twitter lit up like a firefly. I saw posts like:
I’m sorry, but did they sacrifice a human sized pop tart on live tv or was that a fever dream (@ItsMattsLaw)
modern society is so divorced from our food sources that most people who eat pop tarts have never actually killed one themselves (@lolennui)
“this is my body, which is broken for you: do this in remembrance of me” (@joshcarlosjosh)
Honestly, I never want to watch another game in which the mascot is not devoured by the winning team. All sport has become meaningless after the #PopTartsBowl (@Jrsosa18)
A good summary can be found in this BuzzFeed article, The Pop-Tarts Bowl Is Truly The Most Unhinged American Thing I've Ever Seen, And The Internet Can't Get Enough.
I’ll finish with this conversation I had when I told my wife about the madness:
Her: Who won the game?
Me: Does it matter? Kansas State, I think.
What I should have said: We all did, dear. We all did.
That, truly, was the right way to end 2023.
Tweets, Toots, and Skeets
Dad jokes clearinghouse
Here’s a direct link to the National Responsible Fatherhood Clearinghouse, Dad Jokes section. First joke I saw:
Q. Why did the pizza cutter get a speeding ticket?
A. Because he was rounding the corner too fast.
You can add your own, because what could possibly go wrong with that?
Clever insight (seriously)
I would add to that the idea of money. Most people have no conception how much $1 billion is worth, much less the $100 billion so many centibillionaires (Elon, Zuckerberg, Bezos, and so on) possess, or they would be horrified.
Network IDs
There are some really good ones in there.
Monkeys, typewriters, etc
That’s an old one, I know, but it’s a reminder we’re headed into another horrible election year. I’ll just take it as my cue to repeat my favorite related joke:
Me: What did you think of my book?
You: You know how a million monkeys on a million typewriters will eventually write the books of Shakespeare?
Me: So?
You: Your book? Three monkeys, two days.
The Pragmatic Programmers had their annual countdown of the Top Ten best selling books of 2023. Sadly, Mockito Made Clear did not make the list, even though it came out in February, I created a whole series of Medium posts about it, and recorded a YouTube playlist for it as well.
I couldn’t resist taking one of their daily posts (like this one), editing it to put my book in there, and calling it #0. I’m not sure that’s a good move. It feels a bit too bitter and not enough funny, and I don’t mean it that way.
Should I send it to the publisher? What if I add some kind of disclaimer to it? Let me know what you think.
Breakfast is the most important meal of the day
Isn’t that one of the Grand Slam breakfasts offered at Denny’s?
Finishing up the Xmas memes
A few belated Xmas memes for those late shoppers:
That does seem only fair.
Gary doesn’t quite have the same ring to it.
Hey, it rhymes, fits the meter and everything!
That’s how the Creator pronounced it, right?
Have a great week, everybody!
The video version of this newsletter will be on the Tales from the jar side YouTube channel tomorrow.
Last week:
No classes
This week:
Making New Java Features Work For You, on the O’Reilly Learning Platform