C#

General discussions about the c# language, Squirrels | gist.gi...
D G
Mar 19 04:18
Hi, is x:Reference and Reference in MAUI identical?
 

JavaScript

Topic: Anything JavaScript, ECMAScript including Node, React, ...
May 20, 2023 21:09
Thank you for your time thinking of it. :-)
May 20, 2023 20:13
compile type checking does not prevent us from returning number. Bug?
May 20, 2023 20:12
type func = (x: number) => void;
const square: func = (x) => x * x;

console.log(square(2));
May 20, 2023 20:12
How can we return number when the return signature is void?
May 12, 2023 14:06
async function main() {
  let isComplete = false;
  let counter = 0;

  async function job() {
    while (!isComplete) {
      console.log(++counter);

      // Promise.resolve resolves in the next microtask, but setTimeout is a macrotask, so as long as a Promise.resolve is pending the timeout will never fire.
      // await Promise.resolve();

      // This creates a Promise that doesn't resolve until the timeout macrotask runs.
      await new Promise(resolve => setTimeout(resolve));
    }
  }
May 12, 2023 14:06
@Wietlol Solution:
May 9, 2023 16:02
I see. thank you all.
May 9, 2023 15:57
@VLAZ did you mean microtask queue is never emptied?
May 9, 2023 15:55
@OlegValteriswithUkraine Looping stops after setTimeout sets isComplete=true.
May 9, 2023 15:53
I was expecting await Promise.resolve(); releases the thread for other tasks.
May 9, 2023 15:50
No non-trivial purpose. I just want to learn whether there is a solution to stop looping with setTimeout. :-)
May 9, 2023 15:43
Objective: The code prints counter as many as possible in 1 second.
May 9, 2023 15:41
let observable = new Observable((observer) => {
  observer.next();
  setTimeout(() => {
    observer.complete();
  }, 1000);
});

let isComplete = false;
let counter = 0;
observable.subscribe({
  next() {
    while (!isComplete) {
      console.log(++counter);
    }
  },
  complete() {
    isComplete = true;
  },
});
May 9, 2023 15:41
Another failed attempt:
May 9, 2023 15:08
async function main() {
  let isComplete = false;
  let counter = 0;

  async function job() {
    while (!isComplete) {
      console.log(++counter);
      await Promise.resolve();
    }
  }

  setTimeout(() => {
    console.log("Never gets called!");
    isComplete = true;
  }, 1000);

  await Promise.all([job()]);
}

main();
May 9, 2023 15:08
How to stop the looping with setTimeout?
 

Python

Room rules: sopython.com/chatroom Code formatting guide: tinyu...
Apr 19, 2023 11:23
@PM2Ring Thanks for useful links. :-)
Apr 17, 2023 23:02
@KarlKnechtel Yes. Because the amplitude of the wave is np.iinfo(np.int16).max.
Apr 17, 2023 22:50
@KarlKnechtel This one works. Thanks.
Apr 17, 2023 22:49
    wf.write("twinkle.wav", fs, np.concatenate(getTone(n) for n in notes))
  File "<__array_function__ internals>", line 200, in concatenate
TypeError: The first input argument needs to be a sequence
Apr 17, 2023 22:47
    wf.write("twinkle.wav", fs, np.int16(t for n in notes for t in getTone(n)))
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'generator'
Apr 17, 2023 22:44
OK. THank you.
Apr 17, 2023 22:40
I just learnt spread operator. :-)
Apr 17, 2023 22:39
# A
output = []
for n in notes:
    output.extend(getTone(n).tolist())
output = np.array(output)
wf.write("twinkle.wav", fs, output.astype(np.int16))

# B
output = []
for n in notes:
    output = [*output, *getTone(n).tolist()]
wf.write("twinkle.wav", fs, np.int16(output))
Apr 17, 2023 22:39
Which one is preferred?
Apr 16, 2023 19:50
I see. A good advice for long term. :-)
Apr 16, 2023 19:36
notes = [52, 52, 59, 59, 61, 61, 59, 59, 57, 57, 56, 56,
         54, 54, 56, 52, 59, 59, 57, 57, 56, 56, 54, 54]

output = []
for n in notes:
    output.extend(getTone(n).tolist())

output = np.array(output)
Apr 16, 2023 19:36
It makes sense.
Apr 16, 2023 19:34
I see. thanks.
Apr 16, 2023 19:33
Output wrong: [[0.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]
It should be flatten.
Apr 16, 2023 19:31
import numpy as np

output = []
for i in range(5):
    output.append(np.linspace(0,1,2).tolist())

print(output)
Apr 16, 2023 19:24
the resulting wav cannot be played because the format is unknown.
Apr 16, 2023 19:21
produces wrong output which is [[a,b],[c,d]]
Apr 16, 2023 19:21
assuming i have output =[]
for i in [[a,b],[c,d]]:
output.append(i)
Apr 16, 2023 19:18
How to combine [a,b] and [c,d] to become [a,b,c,d]?
Apr 16, 2023 19:08
OK. THanks.
Apr 16, 2023 19:05
ValueError: zero-dimensional arrays cannot be concatenated
Apr 16, 2023 19:04
notes = [52, 52, 59, 59, 61, 61, 59, 59, 57, 57, 56, 56,
         54, 54, 56, 52, 59, 59, 57, 57, 56, 56, 54, 54]

output = np.array(0)
for n in notes:
    output = np.concatenate([output,getTone(n)])
Apr 16, 2023 19:04
How to initialize global array for concatenation?
Apr 16, 2023 14:51
Thanks. :-)
Apr 16, 2023 14:42
Does linspace internally use arange?
Apr 16, 2023 14:34
@AndrasDeak--СлаваУкраїні I see. It should be t0 + np.arange(0, N) * L/(N-1).
Apr 16, 2023 14:29
Yes. I just need another approach if possible without append.
Apr 16, 2023 14:27
I am a masochist. :-)
Apr 16, 2023 14:26
Is there any simpler than np.append(t0 + np.arange(0, N-1) * L/(N-1), t0+L) ?
Apr 16, 2023 14:25
I want to replicate np.linspace(t0, t0+L, N, endpoint=True).
Apr 14, 2023 18:54
    for k in dict:
        print(f"{k},{dict[k]}")

    for k in dict.keys():
        print(f"{k},{dict[k]}")
 

 Python Ouroboros - The Rotating Knives

Messages from the Python room that didn't meet our standards o...
Apr 16, 2023 19:24
import numpy as np
import scipy.io.wavfile as wf


def cosinewave(f):
    return lambda t: np.iinfo(np.int16).max*np.sin(2*np.pi*f*t)


fs = 44100
tn = np.linspace(0, 0.5, int(fs*0.5))


def getTone(n):
    f = 440*(2**((n-49)/12))
    return cosinewave(f)(tn)


notes = [52, 52, 59, 59, 61, 61, 59, 59, 57, 57, 56, 56,
         54, 54, 56, 52, 59, 59, 57, 57, 56, 56, 54, 54]

output = []
for n in notes:
    output.append(getTone(n))

output = np.array(output)


wf.write("song.wav", fs, output.astype(np.int16))