Home Programmering Funksjonsgenerator
Funksjonsgenerator PDF Skriv ut E-post
Skrevet av Jens Christoffersen   
onsdag 25. august 2010 16:26

Dette lille programmet, lager lyd.

 

Det er ikke noe hokuspokus, men det bruker noen av resursene som allerede er med Linux.

/******************************************/
/*Program : Use Linux as a sound generator*/
/*Author  : Jens Christoffersen           */
/*Web     : www.nerdegutta.org            */
/*Date    : aug 2010                      */
/******************************************/

#include 
#include 
#include 
#include 

// Definitions

//Function Prototyping
int mainMenu();
int playSine();
int playSawtooth();
int playSquare();

//Global variables

//Functions
int playSine()
{
int iMenuChoice = 0;
while (iMenuChoice !=1)
{
system("clear");
printf("********************************************************\n");
printf("* This section plays a sine wave tone.                 *\n");
printf("********************************************************\n");
printf("\t\t1.\tBack to main menu.\n");
printf("\t\t2.\t250 Khz wave tone.\n");
printf("\t\t3.\t500 Khz wave tone.\n");
printf("\t\t4.\t1000 Khz wave tone.\n");
printf("\t\t5.\t1500 Khz wave tone.\n");
printf("\t\t6.\t2000 Khz wave tone.\n");

printf("\t\t\tEnter choice: ");
scanf("%d",&iMenuChoice);
switch (iMenuChoice)
{
 case 1:
 break;

 case 2:
 system ("play -t null /dev/null sine.wav synth 5.0 sine 250.0");
 break;

 case 3:
 system ("play -t null /dev/null sine.wav synth 5.0 sine 500.0");
 break;

 case 4:
 system ("play -t null /dev/null sine.wav synth 5.0 sine 1000.0");
 break;

 case 5:
 system ("play -t null /dev/null sine.wav synth 5.0 sine 1500.0");
 break;

 case 6:
 system ("play -t null /dev/null sine.wav synth 5.0 sine 2000.0");
 break;

} // end switch
} // end while
} // end playSine

int playSawtooth()
{
int iMenuChoice = 0;
while (iMenuChoice !=1)
{
system("clear");
printf("********************************************************\n");
printf("* This section plays a sawtooth wave tone.                 *\n");
printf("********************************************************\n");
printf("\t\t1.\tBack to main menu.\n");
printf("\t\t2.\t250 Khz wave tone.\n");
printf("\t\t3.\t500 Khz wave tone.\n");
printf("\t\t4.\t1000 Khz wave tone.\n");
printf("\t\t5.\t1500 Khz wave tone.\n");
printf("\t\t6.\t2000 Khz wave tone.\n");

printf("\t\t\tEnter choice: ");
scanf("%d",&iMenuChoice);
switch (iMenuChoice)
{
 case 1:
 break;

 case 2:
 system ("play -t null /dev/null sine.wav synth 5.0 sawtooth 250.0");
 break;

 case 3:
 system ("play -t null /dev/null sine.wav synth 5.0 sawtooth 500.0");
 break;

 case 4:
 system ("play -t null /dev/null sine.wav synth 5.0 sawtooth 1000.0");
 break;

 case 5:
 system ("play -t null /dev/null sine.wav synth 5.0 sawtooth 1500.0");
 break;

 case 6:
 system ("play -t null /dev/null sine.wav synth 5.0 sawtooth 2000.0");
 break;

} // end switch
} // end while
} // end playSawtooth

int playSquare()
{
int iMenuChoice = 0;
while (iMenuChoice !=1)
{
system("clear");
printf("********************************************************\n");
printf("* This section plays a square wave tone.                 *\n");
printf("********************************************************\n");
printf("\t\t1.\tBack to main menu.\n");
printf("\t\t2.\t250 Khz wave tone.\n");
printf("\t\t3.\t500 Khz wave tone.\n");
printf("\t\t4.\t1000 Khz wave tone.\n");
printf("\t\t5.\t1500 Khz wave tone.\n");
printf("\t\t6.\t2000 Khz wave tone.\n");

printf("\t\t\tEnter choice: ");
scanf("%d",&iMenuChoice);
switch (iMenuChoice)
{
 case 1:
 break;

 case 2:
 system ("play -t null /dev/null sine.wav synth 5.0 square 250.0");
 break;

 case 3:
 system ("play -t null /dev/null sine.wav synth 5.0 square 500.0");
 break;

 case 4:
 system ("play -t null /dev/null sine.wav synth 5.0 square 1000.0");
 break;

 case 5:
 system ("play -t null /dev/null sine.wav synth 5.0 square 1500.0");
 break;

 case 6:
 system ("play -t null /dev/null sine.wav synth 5.0 square 2000.0");
 break;

} // end switch
} // end while
} // end playSquare    


int mainMenu()
{
int iMenuChoice = 0;
while (iMenuChoice !=1)
{
system("clear");
printf("********************************************************\n");
printf("* This program generater the tones using the soundcard *\n");
printf("*       Keyboard interface or adapter interface        *\n");
printf("********************************************************\n");
printf("\t\t1.\tExit program.\n");
printf("\t\t2.\tProduce sine wave tone.\n");
printf("\t\t3.\tProcuce sawtooth wave tone.\n");
printf("\t\t4.\tProduce square wave tone.\n");
printf("\t\t\tEnter choice: ");
scanf("%d",&iMenuChoice);

switch (iMenuChoice)
{
case 2:
 playSine();
 break;

case 3:
 playSawtooth();
 break;

case 4:
 playSquare();
 break;

} // end switch
} // end while
return 0;
} // end mainMenu()


main ()
{
mainMenu();
}

 

Kompilert med:

 

gcc -Wall -o func generator.c

 

Dette lager en fil i aktuell katalog med navnet func. Da kan du bare skrive:

 

./func

 

og trykke enter. Da kommer menyen opp.

Levende dokument...

Sist oppdatert lørdag 27. november 2010 03:32
 
Kopirett © 2012 nerdegutta.org. Alle rettigheter reservert.
Joomla! er fri programvare utgitt under GNU/GPL License.