gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

beep_to_speaker.sh (add-on for synth.el) Re: synth.el v. 0.2dev


From: D Goel
Subject: beep_to_speaker.sh (add-on for synth.el) Re: synth.el v. 0.2dev
Date: Mon, 01 Aug 2005 09:57:16 -0400
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

"D Goel" <address@hidden> writes:

>  synth.el --- music sheet player, synthesizer, music via bash scripts
>
> (The 2 bash scripts:
>
>  * beep_to_speaker.sh
>  * command-line invocation of synth.el
>
>  ...  which I posted separately a few hours ago, are probably briefly
> held up for moderator-review. If they don't show up by tomorrow, I
> will add "add-on for synth.el" (which, it seems, makes them pass
> automatic mod-check) to the subject and resend them. )
>

The posts haven't shown up. As promised, here is beep_to_speaker.sh

====================================================
#!/usr/bin/octave -qf

### This is needed if you try out the sound-speaker option in the
### just-posted synth.el.  Place this file somewhere in your bash path and
### make it executable, say, in ~/bin/, do make sure you have octave-forge
### installed (apt-get install octave2.1 octave-forge)

## Syntax: beep_to_speaker.sh <freq Hz> <duration ms>  <volume> [more args]
## Every set of 3 consecutive arguments  are taken to mean
## freq,duration,volume, and we beep for them.



## To make anything its default value, specify a -2 for its value.
## Thus, beep_to_speaker.sh -2 -2 30, will be like the default
## beep_to_speaker.sh, except that the volume will be 30 instead of
## the default 400.


## If any value in a triplet is -1, that instruction is ignored
## altogether.


## A very partial implementation of beep, except that the output here goes
## to the speaker.
## The input arguments are (freq, duration, magnitude).
## Freq is supplied in Hz and duration in ms.


## Copyright (C) 2005 and onwards D. Goel
## Author: D. Goel <address@hidden>
## Created: 30 Jul 2005
## Adapted-By: 
##
##
## This file is NOT (yet) part of Octave.
##
## This is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## This software is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, write to the Free
## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
##


function signal=_beep_to_speaker_once(inargs)

  signal=[];
  args=inargs;
  ##len=nargin; this always gives 1
  len=length(args);
  

  ## default values we like:
  fdef=440; ## same as beep
  ddef=200; ## same as beep
  mdef=400; ## default vol.


  if len>=3;
    ## magnitude string
    mstr=args{3};
  else
    mstr="-1"; ## A default volume. 
  endif



  ## if no duration is specified, assume 200, just like beep does
  ## If ANY duration is specified, play *something* for that duration: if
  ## can't parse freq., pause  for that duration!
  if len>=2;
    ## duration string
    dstr=args{2};
  else
    dstr="-1";
  endif


  if len>=1;
    ## freq string
    fstr=args{1};
  else
    fstr="-1"; ## same default as that of beep. 
  endif

  m=str2num(mstr);

  d=str2num(dstr);
  f=str2num(fstr);

  ignorep=0;


  okp=0;
  if isnumeric(d);
    if (length(d)==1);
      if d==-2;
        ignorep=1;
      endif
      if d!=-1;
        okp=1;
      endif
    endif
  endif
  if okp==0;
    d=ddef;
  endif
  if d<0;
    d=0.001;
  endif



  okp=0;
  if isnumeric(f);
    if (length(f)==1);
      if f==-2;
        ignorep=1;
      endif
      if f!=-1;
        okp=1;
      endif
    endif
  endif
  if okp==0;
    f=fdef;
  endif
  if f<0;
    f=0.001;
  endif



  okp=0;
  if isnumeric(m);
    if (length(m)==1);
      if f==-2;
        ignorep=1;
      endif
      if m!=-1;
        okp=1;
      endif
    endif
  endif
  if okp==0;
    m=mdef;
  endif
  if m<0;
    m=0.001;
  endif


  dfinal=d;
  ffinal=f;
  mfinal=m;
  


  ## See http://users.rowan.edu/~shreek/networks1/music.html

  ## convert duration to seconds.
  dsecs=dfinal/1000;


  ## corresponds to the default sampling rate of 8000
  ## per second. 


  sampling=0.000125; ## This is the standard, fixed sampling rate. 

  ## This also tells us that trying to go above 3000ish will fail, and
  ## the answer will be unpredictable. (Well, you can still predict it
  ## using some signal interference considerations,  but I am talking
  ## about predicting for  a general audience.)



  ##disp(["Freq=" num2str(ffinal) ", Duration=" num2str(dfinal) \
  ##     ", Magnitude=" num2str(mfinal)]);

  ##signal=mfinal * sin (2 * pi * (ffinal/7) * [0:sampling:dsecs]);

  ## asharp?
  ##signal=mfinal*sin(2*pi*466.16*(0:0.000125:0.5));

  ##signal=mfinal*sin(2*pi*900*[0:0.000125:0.5]);


  ffinal=ffinal;
  if not(ignorep)
    signal=mfinal*sin(2*pi*ffinal*(0:sampling:dsecs));
  endif

  


  ## We could wavwrite it too.. and then aplay the wave...  but let's
  ## just play it..


  ##playaudio(signal1);
  ##playaudio(signal2);

  ## If playaudio fails for you, you can use this alternate mechanism
  ## shown next: If this problem does happen, do let me know so I can
  ## code this one up properly (we will introduce a 4th arg to this
  ## function).

  ##sleep(1);
  ##wavwrite(signal/200,"/home/deego/tmp.wav")
  ##shell_cmd("aplay /home/deego/tmp.wav");
  

endfunction









function _beep_to_speaker (inargs)


  
  args=inargs;
  ##len=nargin; this always gives 1
  len=length(args);
  signal=[];

  ctr=1;
  while len>=ctr;
    ctr2=ctr+2;
    ctr3=min(ctr2,len);
    thisargs=args(ctr:ctr3);
    ctr=ctr3+1;
    signal=[signal _beep_to_speaker_once(thisargs)];
  endwhile
  playaudio(signal);
endfunction


_beep_to_speaker(argv)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]