Generating musical notes on windows

2012-01-01 VI Minimalist Extension to generate musical notes on windows. I compiled this under mingw using:

  gcc -Id:/tools/tcl/8.6b2/include beep.c -shared -o beep.dll -Ld:/tools/tcl/8.6b2/lib/ -ltclstub86

Example Usage:

  load beep.dll
  beep 440 1000  ;# Generate the A4 (middle note) for 1 second

2012-01-02 VI Changed parameter 1 to be an integer since that's what windows accepts.


 /* beep.c
    Minimalist sound generation interface for windows.  Beep is *NOT* in the background.
    Author: Venkat Iyer <[email protected]>.   License: BSD 2-Clause.
 */
 
 
 #define USE_TCL_STUBS
 #include <windows.h>
 #include "tcl.h"
 
 static int 
 beepCmd (dummy, interp, objc, objv)
     ClientData dummy;
     Tcl_Interp *interp;
     int         objc;
     Tcl_Obj     *CONST objv[];
 {
   int freq;
   int duration;

   if (objc != 3) {
     Tcl_WrongNumArgs(interp, 1, objv, "freq duration(ms)");
     return TCL_ERROR;
   }

   if (Tcl_GetIntFromObj(interp, objv[1], &freq) != TCL_OK)
     return TCL_ERROR;     

   if (Tcl_GetIntFromObj(interp, objv[2], &duration) != TCL_OK)
     return TCL_ERROR;
 
   Beep(freq,duration);
   return TCL_OK;
 }
     
 
 
 int Beep_Init (interp)
      Tcl_Interp *interp;
 {
   if (Tcl_InitStubs(interp, "8.4", 0) == NULL) {
     return TCL_ERROR;
   }
   if (Tcl_PkgRequire(interp, "Tcl", "8.4", 0) == NULL) {
     return TCL_ERROR;
   }
   if (Tcl_PkgProvide(interp, "beep", "0.7.5") == TCL_ERROR) {
     return TCL_ERROR;
   }
   Tcl_CreateObjCommand(interp, "beep", beepCmd, 
                        (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
 
   return TCL_OK;
 }

DKF: With critcl, you'd write it like this:

package provide beep 0.7.5
package require critcl
critcl::ccode {
    #include <windows.h>
}
critcl::cproc beep {int freq int duration} void {
    Beep(freq, duration);
}