Tips and tricks #10: Files, long calculation mode

The article describes how to use input and output files in Planetcalc calculators

This page exists due to the efforts of the following people:

Anton

Timur

Timur

Created: 2020-03-04 22:46:33, Last updated: 2022-10-28 08:02:55
Creative Commons Attribution/Share-Alike License 3.0 (Unported)

This content is licensed under Creative Commons Attribution/Share-Alike License 3.0 (Unported). That means you may freely redistribute or modify this content under the same license conditions and must attribute the original author by placing a hyperlink from your site to this work https://planetcalc.com/8631/. Also, please do not modify any references to the original work (if any) contained in this content.

Tips and tricks series

This article may rely on knowledge you should get from previous articles, so you may want to check them first.

File output

Using file output you can save your calculator output as a binary file. For example the following calculator generates sound tones by user parameters and saves it to wave audio file. The file can be downloaded to the user's PC by clicking on the calculator output parameter link.

PLANETCALC, Sound tone generator

Sound tone generator

Output file format

Sound file
 

The code excerpt from the calculator above generates the link for downloading output file:

Code

file.SetValue( {filename:form + period 
  + 'Hz_' + bits +'bit' + (sampling/1000) 
  + 'kHz_' +  length + 'ms.wav'
  ,type:'audio/wave' 
  ,data:[header,data]} );

So to create a downloadable file link you should pass the following structure to the file output:
{ filename, type, data }

  • filename - is a name under which a user browser will save the file after clicking by output file link.
  • type - MIME type of the output file (optional)
  • data - array of ArrayBuffer's, containing the file content.
    N.B. the file data is kept in a user computer memory, so don't generate huge files to avoid out-of-memory issues.

File input

The file input extends the Planetcalc calculator framework capabilities by the option of reading data from local files. You may set the limits on input file types and file size. By default any files of any size are allowed. You can use file input in asynchronous calculation mode only.

PLANETCALC, Audio file waveform

Audio file waveform

Wave file
  • Drag files here
Digits after the decimal point: 3
File format
 
Waveform
The file is very large. Browser slowdown may occur during loading and creation.

File object

The file input returns an array of files, selected by user. Every item of the array has the file property, which is the File object. You may use FileReader API to access the file data.
To process huge files you may implement buffered reading using Blob.slice method.
Also you can reuse library function Planetcalc.Lib8582.bufferedRead( buffer, done ) for this purpose:

Code

var lib = Planetcalc.Lib8582;
return Promise.all( file.map(f=> {
    return new Promise( (success, error ) => {
        lib.bufferedRead(cur, ( buf, finalBuffer ) =>  {
                // process buf - ArrayBuffer
                if ( finalBuffer ) success();
        }); 
    });
}));

Asynchronous/long calculation mode

By switching on 'Long calculation' mode in the calculator properties one may perform a long calculation without UI freeze. In this mode browsers will execute the calculator code in the web worker (separate thread). The code executes asynchronously, the Calculate function must return a Promise resolving on a calculation completion. The asynchronous mode is similar to the long calculation mode, except the calculate function executes in the browser thread. Use the long calculation mode for infinitely long or CPU consuming calculation, use asynchronous mode for the calculations involving finite asynchronous operations, such as file input. You may set output values as usual during async/infinite calculation mode, but the calculator UI will display them only on calculation completion. To display current values of the outputs, use the special output, available in async/long mode, named 'progressControl'. progressControl.SetValue method sets progress bar textual information and forces UI to display the current values.

Code

progressControl.SetValue( '34 of 124 items processed');  //sets progress bar text and forces UI to display current values

URL copied to clipboard
PLANETCALC, Tips and tricks #10: Files, long calculation mode

Comments