Make a CLI version of the time management tool (15)

This is the 16th day of my participation in the August More Text Challenge.

preface

A previous article refined a Timec Page directive that generates the progress of Web page browsing transactions

However, two pain points were found in the commonly used tools in Japan:

  1. With so many things to do, it’s common to forget to finish one thing and move on to the next
  2. When switching tasks, complete input of the task name is required to switch tasks

Of course, there are other pain points, and this installment focuses on pain point number one

Regular reminder Feature development

The timec thing -s command is automatically executed and a prompt tone is used

The expected instruction timec remind

  • -c,--cycle [time]: Indicates the alarm interval. The unit is minute. The default value is 40 minutes
timec remind [option]
Copy the code

Registration instructions

Register the remind directive with commander.command and set an optional parameter cycle using the.option method, passing the default value ’40’ for the second parameter.

commander.command('remind')
  .description('Open auto remind music')
  .option('-c,--cycle [time]'.'Set the duration of the Reminder cycle (minute) '.'40')
  .action(remindCommand);
Copy the code

The specific logic is as follows:

  1. willcycleParameter to integer
  2. And then use a timersetTimeoutIn thecycle*oneMinuteAfter milliseconds, play the audio with automatic recording
const spawn = require('cross-spawn');

// Reminder interval (minute)
  const time = +cmdObj.cycle;
  const oneMinute = 1000 * 60;
  const loop = () = > {
    setTimeout(() = > {
      playRemindAudio(loop);
      // automatically record
      const cwd = getCWD();
      const { thing } = getConfig();
      spawn('timec'['thing', thing.name], {
        cwd,
        stdio: 'inherit'}); }, time * oneMinute); }; loop();Copy the code

The automatic recording method is relatively simple. Run the timec thing [newthing] command to automatically record events through spawn

PlayRemindAudio contains the logic to play preset audio

The play

This is a knowledge blind spot and has never played audio using node.js’s native API before

System instructions

The first solution that comes to mind is to play audio through the spawn call system directive

spawn('mpg123'['url.mp3');
Copy the code

This is calling the MPG123 system instruction

The Linux installation instructions are as follows

sudo apt-get install mpg123
Copy the code

Calling system instructions to play music has limitations. There are three common operating systems. Not all operating systems support the same instructions

If you use this method, you need to research the target platform and install the default support instructions

Existing NPM package

This is certainly not the first time that developers have encountered this problem, but it must have been encountered by their predecessors.

  • play
  • audio-play

First is the first one,API is relatively simple, by looking at the source code, in fact, it is also called system instructions play

const play = require('play/lib/play');

play.sound('filepath.wav');
Copy the code

The second is audio-play, which needs to be used together with the audio-loader

const audioPlay = require('audio-play');
const audioLoad = require('audio-loader');
audioLoad('filepath.wav').then((v) = > {
  audioPlay(v);
});
Copy the code

The test played correctly on MAC, but still failed to play correctly on Linux

Use the system buzzer

This one is relatively simple, just execute the following code, but the test on my Linux still doesn’t work, emmmm cracks

process.stdout.write('\x07')
Copy the code

TODO

Node.js audio playback practice article, target 3 common API

The last

Due to limited free time each day, I will stop here and share the node.js audio playback article in the next installment

If you’re still feeling the urge, stay tuned for updates or keep an eye on the warehouse

Welcome to comment on the need to discuss

This series will continue to update iteration, until the completion of the first generation of products

  • The warehouse address