Fork me on GitHub
#clojurescript
<
2020-01-31
>
cddr11:01:57

What do clojurescripters do to workaround the fact that BigDecimal doesn't exist if they need to calculate something like (* 100 10.2) and would like the answer to be exact?

dominicm11:01:13

I think closure has a number thingy, and gfredericks has a wrapper

cddr12:01:58

Aha this? https://github.com/gfredericks/exact Didn't show up in my initial search but the addition of "gfredericks" got me there. Thanks

mfikes13:01:52

This tagged literal library can also be useful with exact: https://github.com/mfikes/precise

Jakub Holý12:01:04

Hello! What do people use to embed the content of a file (e.g. .css, .svg) in their cljs code as a string? Write a custom macro (so that it is resolved at compile time) calling slurp?

Jakub Holý12:01:29

Thanks a lot! My googling revealed nothing, sadly.

p-himik12:01:41

Yeah, often you need to specifically go "clojureversing". :)

👍 4
dominicm12:01:53

We've always done the macro thing

Jakub Holý12:01:52

I guess there is no out of the box macro I could use, I have to write my own, right?

sova-soars-the-sora16:01:03

Hi, Can someone help me translate plain vanilla js to cljs?

sova-soars-the-sora16:01:17

I'm wanting to play audio files in sequence. My plan is to cut a long mp3 into words and then play them one after another

sova-soars-the-sora16:01:28

I found a nice code on SO:

function play(audio) {
    audio.play();
    return new Promise(function(resolve, reject) {
        audio.addEventListener('ended', resolve);
    });
}

function myFunction() {
    var audio1 = new Audio('');
    var audio2 = new Audio('');
    var audio3 = new Audio('');

    play(audio1).then(function() {
        return play(audio2);
    }).then(function() {
        return play(audio3);
    });
}

sova-soars-the-sora16:01:22

Not sure/certain how to do eventListeners or Promises in CLJS. My goal is to play audio files one after another in a sequence according to a vector that represents the whole sentence, mp3 clip by mp3 clip

sova-soars-the-sora16:01:56

something like ["part01.mp3" "part02.mp3" "part03.mp3"] much like in the above example

borkdude17:01:25

@sova (defn play [audio] (.play audio) (js/Promise. (fn [resolve reject] (.addEventListener audio "ended" resolve))))

sova-soars-the-sora17:01:23

thanks a lot! looks like kung fu at this point, will learn up

sova-soars-the-sora17:01:46

hmm how would you do the

.then

borkdude17:01:25

the .then is used in the gist I posted

sova-soars-the-sora17:01:37

so just to confirm

resolve
is a function that gets called when the play event finishes?

sova-soars-the-sora17:01:06

or that's invoked by the promise

sova-soars-the-sora18:01:11

(defn play [audio] 
  (.play audio) 
  (js/Promise. 
    (fn [resolve reject] 
      (.addEventListener audio "ended" resolve))))

(let [s1 (.createElement js/document "audio")
      s2 (.createElement js/document "audio")
      s3 (.createElement js/document "audio")]
							  
					  (set! (.-src s1) "ji.mp3")
					  (set! (.-src s2) "byo.mp3")
					  (set! (.-src s3) "chyu.mp3")

					  (set! (.-preload s1) "auto")
					  (set! (.-preload s2) "auto")
					  (set! (.-preload s3) "auto")

							(-> (play s1)
							    (.then (play s2))
								(.then (play s3))))

sova-soars-the-sora18:01:16

they all play simultaneously

borkdude18:01:02

@sova try (.then #(play s2))

sova-soars-the-sora18:01:29

thank you. i don't get it. could you help explain?

borkdude18:01:49

you have to pass a function, else the expression will already be evaluated

sova-soars-the-sora18:01:43

thank you very much! 😃

sova-soars-the-sora18:01:52

now i'm trying to get it to highlight text alongside playback

sova-soars-the-sora18:01:05

lots of surgery but in the end i think it will be worth it

sova-soars-the-sora18:01:52

xD it might as well be! japanese reading tutor

😂 4
sova-soars-the-sora19:01:22

eventually i would like to be able to take a neural net with two inputs: script and audio, and output sliced up mp3s tagged with relevant audio, which seems like a simple idea, but getting the training data is ... surgery 😄

sova-soars-the-sora19:01:26

is there an accepted way to "toggle class" in cljs?

borkdude19:01:21

@sova I would say that's not specific to CLJS but to your browser

dnolen19:01:03

@sova yes Google Closure library has a thing for that

borkdude20:01:06

ah, the great goog lib, totally forgot about that again

sova-soars-the-sora21:01:25

So I want to generalize this "play a sequence of mp3s" function. is there a way to (.createElement js/document "audio") and also set its source (set! (.-src name-of-audio-element) "chyu.mp3") without naming it? Or maybe just name it and conj it to a vector?

p-himik21:01:03

If you replace set! with some function that accepts the element as its first argument, you could use doto.

p-himik21:01:25

(doto (.createElement js/document "audio")
  (.setAttribute "src" "chyu.mp3"))

sova-soars-the-sora23:01:35

wow you're a life saver thank you

sova-soars-the-sora23:01:55

is it possible to load a map into clojurescript via a regular file?

sova-soars-the-sora23:01:06

i'm likely adding many audio files for the relevant text, is there a way to keep expanding this index list of audio files - with - text without resorting to regenerating the js every time? am thinking some sort of external file w/ the map that i can update on the side. seems reasonable?

isak00:02:26

You can make an ajax request, and then create a map based on the response

isak00:02:55

If you want to embed the data instead of making a request, check out https://clojureverse.org/t/using-none-code-resources-in-cljs-builds/3745

sova-soars-the-sora17:02:46

yeah that's what i want, need shadow-cljs i suppose