From J Wiki
Jump to: navigation, search
User Guide | Development Categories Versions | SVN | Build Log

media/wav - Windows WAV file creation and play

Browse source and examples in SVN.

  • Implementation of player for Windows.
  • WAVE file generation in PCM format is pure J and cross-platform.
  • Capable of reading PCM format header and data: either from noun or from file using efficient memory-mapped files
  • Includes samples of creating musical notes.


Wave information and data can be obtained from using file on disk directly.

   load 'media/wav'
   wavfile jpath '~addons/media/wav/test1.wav'
File Type       RIFF
Total Size      22390
File Format     WAVE
Channels        1
Sample Rate     11025
Sample Count    22108
Bits per Sample 8

   load 'plot'
   plot 2 wavfile jpath '~addons/media/wav/test1.wav'


... or from noun in memory.

   W=. fread jpath '~addons/media/wav/test1.wav'
   plot wavdata W

Extracting Information

As seen in the above example, wavfile monad (default parameter 0) shows the header information. Each header value is assigned to a variable, which can be used in further requests:

  wavinfo_pwav_   NB. show wavinfo definition to identify variables
3 : 0
  wavhead y
  r=.      'File Type       ',chunk
  r=. r,LF,'Total Size      ',":8+chunkSize
  r=. r,LF,'File Format     ',format
  r=. r,LF,'Channels        ',":chanels
  r=. r,LF,'Sample Rate     ',":sampleRate
  r=. r,LF,'Sample Count    ',":sampleCount
  r=. r,LF,'Bits per Sample ',":bitsPerSample

For example, sampleCount gives total number of data values.

   $2 wavfile jpath '~addons/media/wav/test1.wav'  NB. too expensive
   sampleCount_pwav_                     NB. get total sample count
   sampleCount_pwav_% sampleRate_pwav_   NB. duration in seconds

To extract the data, the dyad 2 wavfile file is used for whole file, which may be not desirable. Consider (2;index,count) wavfile file for a subset of samples.

Extracting a subset of 20 last samples:

   (2;sampleCount_pwav_(-,])20) wavfile jpath '~addons/media/wav/test1.wav'
122 122 123 118 121 126 130 130 128 129 130 126 126 130 127 125 124 124 124 125

Reading a subset and saving into a new file:

   NB. extract 1.5 seconds of data
   $ d=. (2;<.0,sampleRate_pwav_*1.5) wavfile jpath '~addons/media/wav/test1.wav'

   NB. save it in a new file with same sample rate
   (sampleRate_pwav_ wavmake d) fwrite jpath'~temp/test.wav'

   load'media/wav/view'                  NB. view the result
   wavview jpath'~temp/test.wav'

See more operations and parameter in the Reference section below.

Wave Viewer

wavview zoom viewer shows the wave form up close. Using memory mapped files, it moves around files of arbitrary size very fast (save your biggest >1hr podcast as WAV and try).


Control elements and interaction:

lens view:: (above) shows the whole wave form and highlights selection
dragging positions the selection detail view:: (below) shows selected fragment
dragging shifts the selection gradually zoom value:: (bottom tracker) changes selection size

To run from J session:

   load 'media/wav/view'
   wavview '~addons/media/wav/test1.wav'

You can register the viewer for OS shell to open WAV files. For Windows:

  • edit last line of viewer.reg by changing D:\\Math\\j601\\j.exe to your path (watch double \\)
  • run it by double-clicking and choosing Yes
  • locate any .wav file in Windows Explorer, right-click and select "View"


wavplay v plays wav from file or memory
wavmake v wav format from samples
wavmakenote v make wav format from musical notes
wavnote v plays musical notes
wavdata v WAVE data from noun
wavinfo v WAVE info from noun
wavfile v WAVE info or data from file


wavplay (v) plays wav from file or memory

  y  wav file or char vector of wav format
 [x] SND_* flags, SND_SYNC default, see wav.ijs for details


wavmake (v) wav format from samples

  y  samples vector or (2,N)=$y matrix for stereo
      range is 0..255 or _32768..32767
 [x] sample rate in Hz, RATE default of 11000


wavmakenote (v) make wav format from musical notes


wavnote (v) plays musical notes

  y  tones 0=C, 1=C#, 2=D of main octave
 [x] durations in sec, 0.25 default of 1/4 sec


wavdata (v) WAVE data from noun

  y  wave format
 [x] samples: (index,count)[, .. ,:index,count]


wavinfo (v) WAVE info from noun

  y  wave format


wavfile (v) WAVE info or data from file

  y  wav file
 [x] what[;samples]
     what:    0-info 1-head 2-data
     samples: see wavdata


See Also