ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÔÚLinux²Ù×÷ϵͳÖбà³ÌÉùÒôÉ豸ʵÀý

·¢²¼Ê±¼ä:2006-11-21 09:38:06À´Ô´:ºìÁª×÷Õß:flycocoon
¡¡¡¡LinuxϵÄÉùÒôÉ豸±à³Ì±È´ó¶àÊýÈËÏëÏóµÄÒª¼òµ¥µÃ¶à¡£Ò»°ã˵À´£¬ÎÒÃdz£ÓõÄÉùÒôÉ豸ÊÇÄÚ²¿ÑïÉùÆ÷ºÍÉù¿¨£¬ËüÃǶ¼¶ÔÓ¦/devĿ¼ÏµÄÒ»¸ö»ò¶à¸öÉ豸Îļþ£¬ÎÒÃÇÏó´ò¿ªÆÕͨÎļþÒ»Ñù´ò¿ªËüÃÇ£¬ÓÃioctl£¨£©º¯ÊýÉèÖÃһЩ²ÎÊý£¬È»ºó¶ÔÕâЩ´ò¿ªµÄÌØÊâÎļþ½øд²Ù×÷¡£

¡¡¡¡ÓÉÓÚÕâЩÎļþ²»ÊÇÆÕͨµÄÎļþ£¬ËùÒÔÎÒÃDz»ÄÜÓÃANSI C£¨±ê×¼C£©µÄfopen¡¢fcloseµÈÀ´²Ù×÷Îļþ£¬¶øÓ¦¸ÃʹÓÃϵͳÎļþI/O´¦Àíº¯Êý£¨open¡¢read¡¢write¡¢lseekºÍclose£©À´´¦ÀíÕâЩÉ豸Îļþ¡£ioctl£¨£©»òÐíÊÇLinuxÏÂ×îÅÓÔӵĺ¯Êý£¬Ëü¿ÉÒÔ¿ØÖƸ÷ÖÖÎļþµÄÊôÐÔ£¬ÔÚLinuxÉùÒôÉ豸±à³ÌÖУ¬×îÖØÒªµÄ¾ÍÊÇʹÓô˺¯ÊýÕýÈ·ÉèÖñØÒªµÄ²ÎÊý¡£

¡¡¡¡ÏÂÃæÎÒÃǾÙÁ½¸öʵ¼ÊµÄÀý×ÓÀ´ËµÃ÷ÈçºÎʵÏÖLinuxϵÄÉùÒô±à³Ì¡£ÓÉÓÚ´ËÀà±à³ÌÉæ¼°µ½ÏµÍ³É豸µÄ¶Áд£¬ËùÒÔ£¬ºÜ¶àʱºòÐèÒªÄãÓÐrootȨÏÞ£¬Èç¹ûÄ㽫ÏÂÃæµÄÀý×Ó±àÒëºó²»ÄÜÕýÈ·Ö´ÐУ¬ÄÇô£¬Ê×ÏÈÇëÄã¼ì²éÊÇ·ñÊÇÒòΪûÓвÙ×Ýij¸öÉ豸µÄȨÏÞ¡£

¡¡¡¡¶ÔÄÚ²¿ÑïÉùÆ÷±à³ÌÄÚ²¿ÑïÉùÆ÷ÊÇ¿ØÖÆ̨µÄÒ»²¿·Ö£¬ËùÒÔËü¶ÔÓ¦µÄÉ豸ÎļþΪ/dev/console¡£±äÁ¿KIOCSOUNDÔÚÍ·Îļþ /usr /include /linux /kd.hÖÐÉùÃ÷£¬ioctlº¯ÊýʹÓÃËü¿ÉÒÔÀ´¿ØÖÆÑïÉùÆ÷µÄ·¢Éù£¬Ê¹ÓùæÔòΪ£º

ÒýÓÃ:
¡¡¡¡ioctl ( fd, KIOCSOUND, (int) tone);


¡¡¡¡fdΪÎļþÉ豸ºÅ£¬tone ÊÇÒôƵֵ¡£µ±toneΪ0ʱ£¬ÖÕÖ¹·¢Éù¡£±ØÐëÒ»ÌáµÄÊÇËüËùÀí½âµÄÒôƵºÍÎÒÃÇƽ³£ÒÔΪµÄÒôƵÊDz»Í¬µÄ£¬ÓÉÓÚ¼ÆËã»úÖ÷°å¶¨Ê±Æ÷µÄʱÖÓƵÂÊΪ1.19MHZ£¬ËùÒÔÒª½øÐÐÕýÈ·µÄ·¢Éù£¬±ØÐë½øÐÐÈçϵÄת»»£ºÑïÉùÆ÷ÒôƵֵ=1190000/ÎÒÃÇÆÚÍûµÄÒôƵֵ¡£

¡¡¡¡ÑïÉùÆ÷·¢Éùʱ¼äµÄ³¤¶ÌÎÒÃÇͨ¹ýº¯Êýusleep£¨unsigned long usec£©À´¿ØÖÆ¡£ËüÊÇÔÚÍ·Îļþ/usr /include /unistd.hÖж¨ÒåµÄ£¬ÈóÌÐò˯Ãßusec΢Ãë¡£ÏÂÃæ¼´ÊÇÈÃÑïÉùÆ÷°´Ö¸¶¨µÄ³¤¶ÈºÍÒôƵ·¢ÉùµÄ³ÌÐòµÄÍêÕûÇåµ¥£º

ÒýÓÃ:
#include < fcntl.h >
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < unistd.h >
#include < sys/ioctl.h >
#include < sys/types.h >
#include < linux/kd.h >

/* É趨ĬÈÏÖµ */
#define DEFAULT_FREQ 440 /* É趨һ¸öºÏÊʵÄƵÂÊ */
#define DEFAULT_LENGTH 200 /* 200 ΢Ã룬·¢ÉùµÄ³¤¶ÈÊÇÒÔ΢ÃëΪµ¥Î»µÄ*/
#define DEFAULT_REPS 1 /* ĬÈϲ»Öظ´·¢Éù */
#define DEFAULT_DELAY 100 /* ͬÑùÒÔ΢ÃëΪµ¥Î»*/

/* ¶¨ÒåÒ»¸ö½á¹¹£¬´æ´¢ËùÐèµÄÊý¾Ý*/
typedef struct {
int freq; /* ÎÒÃÇÆÚÍûÊä³öµÄƵÂÊ£¬µ¥Î»ÎªHz */
int length; /* ·¢Éù³¤¶È£¬ÒÔ΢ÃëΪµ¥Î»*/
int reps; /* Öظ´µÄ´ÎÊý*/
int delay; /* Á½´Î·¢Éù¼ä¸ô£¬ÒÔ΢ÃëΪµ¥Î»*/
} beep_parms_t;


/* ´òÓ¡°ïÖúÐÅÏ¢²¢Í˳ö*/
void usage_bail ( const char *executable_name ) {
printf ( "Usage: \n \t%s [-f frequency] [-l length] [-r reps] [-d delay] \n ",
executable_name );
exit(1);
}

/ * ·ÖÎöÔËÐвÎÊý£¬¸÷ÏîÒâÒåÈçÏ£º
* "-f <ÒÔHZΪµ¥Î»µÄƵÂÊÖµ >"
* "-l <ÒÔºÁÃëΪµ¥Î»µÄ·¢Éùʱ³¤ >"
* "-r <Öظ´´ÎÊý >"
* "-d <ÒÔºÁÃëΪµ¥Î»µÄ¼äЪʱ³¤ >"
*/
void parse_command_line(char **argv, beep_parms_t *result) {
char *arg0 = *(argv++);
while ( *argv ) {
if ( !strcmp( *argv,"-f" )) { /*ƵÂÊ*/
int freq = atoi ( *( ++argv ) );
if ( ( freq <= 0 ) | | ( freq > 10000 ) ) {
fprintf ( stderr, "Bad parameter: frequency must be from 1..10000\n" );
exit (1) ;
} else {
result->freq = freq;
argv++;
}
} else if ( ! strcmp ( *argv, "-l" ) ) { /*ʱ³¤*/
int length = atoi ( *(++argv ) );
if (length < 0) {
fprintf(stderr, "Bad parameter: length must be >= 0\n");
exit(1);
} else {
result->length = length;
argv++;
}
} else if (!strcmp(*argv, "-r")) { /*Öظ´´ÎÊý*/
int reps = atoi(*(++argv));
if (reps < 0) {
fprintf(stderr, "Bad parameter: reps must be >= 0\n");
exit(1);
} else {
result->reps = reps;
argv++;
}
} else if (!strcmp(*argv, "-d")) { /* ÑÓʱ */
int delay = atoi(*(++argv));
if (delay < 0) {
fprintf(stderr, "Bad parameter: delay must be >= 0\n");
exit(1);
} else {
result->delay = delay;
argv++;
}
} else {
fprintf(stderr, "Bad parameter: %s\n", *argv);
usage_bail(arg0);
}
}
}

int main(int argc, char **argv) {
int console_fd;
int i; /* Ñ­»·¼ÆÊýÆ÷ */
/* Éè·¢Éù²ÎÊýΪĬÈÏÖµ*/
beep_parms_t parms = {DEFAULT_FREQ, DEFAULT_LENGTH, DEFAULT_REPS,
DEFAULT_DELAY};
/* ·ÖÎö²ÎÊý£¬¿ÉÄܵĻ°¸üз¢Éù²ÎÊý*/
parse_command_line(argv, &parms);

/* ´ò¿ª¿ØÖÆ̨£¬Ê§°ÜÔò½áÊø³ÌÐò*/
if ( ( console_fd = open ( "/dev/console", O_WRONLY ) ) == -1 ) {
fprintf(stderr, "Failed to open console.\n");
perror("open");
exit(1);
}

/* ÕæÕý¿ªÊ¼ÈÃÑïÉùÆ÷·¢Éù*/
for (i = 0; i < parms.reps; i++) {
/* Êý×Ö1190000´ÓºÎ¶øÀ´£¬²»µÃ¶øÖª*/
int magical_fairy_number = 1190000/parms.freq;

ioctl(console_fd, KIOCSOUND, magical_fairy_number); /* ¿ªÊ¼·¢Éù */
usleep(1000*parms.length); /*µÈ´ý... */
ioctl(console_fd, KIOCSOUND, 0); /* Í£Ö¹·¢Éù*/
usleep(1000*parms.delay); /* µÈ´ý... */
} /* Öظ´²¥·Å*/
return EXIT_SUCCESS;
}


¡¡¡¡½«ÉÏÃæµÄÀý×ÓÉÔ×÷À©Õ¹£¬Óû§¼´¿ÉÒÔÈÃÑïÉùÆ÷³ª¸è¡£Ö»ÒªÕÒµ½ÎåÏßÆ×»ò¼òÆ×µÄÒô½×¡¢Òô³¤¡¢½ÚÅĺÍƵÂÊ¡¢·¢Éùʱ³¤¡¢¼ä¸ôµÄ¶ÔÓ¦¹Øϵ¾Í¿ÉÒÔÁË¡£ÎÒÏÖÔÚ»¹¼ÇµÃÒÔÇ°ÔÚDOSϱàд³ö¡¶ÊÀÉÏÖ»ÓÐÂèÂèºÃ¡·Ê±µÄÐË·Ü¡£×îºó£¬ËµÒ»Ð©ÌáÍâ»°£¬ÕâÆäʵÊÇÒ»¸öºÜ¼òµ¥µÄ³ÌÐò£¬µ«ÊÇÎÒÃÇÈ´ÓÃÁ˺ܳ¤µÄƪ·ù£¬Ï£Íû¶ÁÕß´ÓÒÔÉϵĴúÂëÀïÄÜÌå»áµ½Ð´ºÃµÄ³ÌÐòµÄһЩ·½·¨£¬»òÐí×îÖØÒªµÄÊÇÌí¼Ó×¢ÊÍ°É¡£Ò»¸ö³ÌÐòµÄ×¢ÊÍÓÀÔ¶²»»áÏӶ࣬¼´±ãÄãдµÄʱºò¾õµÃËü¸ù±¾ÊǶàÓ࣬µ«ÏàÐÅÎÒ£¬ÏàÐÅÔøÕâÑù¸æËßÎÒÃǵÄÐí¶àÓÅÐãµÄ³ÌÐòÔ±£ºÑø³ÉдºÜ¶à×¢Ê͵ÄÏ°¹ß¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2236 ÌõÆÀÂÛ

  1. 72.36.178.* ÓÚ 2006-11-29 13:07:23·¢±í:

    Nice site!
    My homepage | Cool site

  2. ÓÚ 2006-11-23 18:27:03·¢±í:

    :ha3nd

  3. flycocoon ÓÚ 2006-11-21 09:38:52·¢±í:

    ¡¡¡¡¶ÔÉù¿¨±à³Ì

    ¡¡¡¡Ö»ÒªÎÒÃDz»ÊǽøÐÐÖîÈçÇý¶¯É豸¿ª·¢Ö®ÀàµÄ¹¤×÷£¬¶ÔÉù¿¨µÄ±à³ÌºÍÉÏÃæ¶ÔÑïÉùÆ÷µÄ±à³ÌûÓÐʲô±¾ÖʵÄÇø±ð¡£µ±ÄãÊÔͼÀ´±àдÖîÈçCD²¥·ÅÆ÷¡¢MP3²¥·ÅÆ÷Ö®ÀàµÄ¸´ÔӵijÌÐòʱ£¬ÄãµÄ¹¤×÷ÊÇÈ¡»ñµÃÓëCDROM¿ØÖÆ¡¢MP3½âÂëÖ®ÀàµÄÐÅÏ¢£¬¶ø¶ÁдϵͳÉ豸µÄÕâÒ»²½ÔÚLinuxϳ¬»¥ÏëÏóµÄ¼òµ¥¡£ÀýÈ磬LinuxÏÂ×î¼òµ¥µÄ²¥·ÅwavµÄ³ÌÐòÖ»ÓÐÒ»ÐУºcp $< >/dev/audio¡£½«Ëüд³ÉÒ»¸öshellÎļþ£¬Í¬ÑùÊÇÒ»¸ö³ÌÐò£¨shell ±à³Ì£©¡£

    ¡¡¡¡ÎÒÃÇÊ×ÏÈÐèÒªÖªµÀһ̨»úÆ÷ÉÏÊÇ·ñÓÐÉù¿¨£¬Ò»¸ö¼ì²éµÄ°ì·¨ÊǼì²éÎļþ/dev/sndstatÎļþ£¬Èç¹û´ò¿ª´ËÎļþ´íÎ󣬲¢ÇÒ´íÎóºÅÊÇENODEV£¬Ôò˵Ã÷´Ë»úÆ÷ûÓа²×°Éù¿¨¡£³ý´ËÖ®Í⣬ÊÔ×ÅÈ¥´ò¿ªÎļþ/dev/dspÒ²¿ÉÒÔÀ´¼ì²éÊÇ·ñ°²×°ÁËÉù¿¨¡£

    ¡¡¡¡LinuxϺÍÉù¿¨Ïà¹ØµÄÎļþÓÐÐí¶à£¬Èç²É¼¯Êý×ÖÑù±¾µÄ/dev/dspÎļþ£¬Õë¶Ô»ìÒôÆ÷µÄ/dev/mixerÎļþÒÔ¼°ÓÃÓÚÒôÐòÆ÷µÄ/dev/sequencerµÈ¡£Îļþ/dev/audioÊÇÒ»¸ö»ùÓÚ¼æÈÝÐÔ¿¼ÂǵÄÉùÒôÉ豸Îļþ£¬Ëüʵ¼ÊÊǵ½ÉÏÊöÊý×ÖÉ豸µÄÒ»¸öÓ³É䣬Ëü×î´óµÄÌØÉ«»òÐíÊǶÔÖîÈçwavÕâÀàÎļþ¸ñʽµÄÖ±½ÓÖ§³Ö¡£ÎÒÃÇÏÂÃæµÄÀý×Ó¼´Ê¹ÓÃÁË´ËÉ豸ÎļþʵÏÖÁËÒ»¸ö¼òµ¥µÄ¼Òô»ú£ºÎÒÃÇ´ÓÉù¿¨É豸£¨µ±È»ÒªÓÃÂó¿Ë·ç£©¶ÁÈ¡ÒôƵÊý¾Ý£¬²¢½«Ëü´æ·Åµ½Îļþtest.wavÖÐÈ¥¡£Òª²¥·ÅÕâ¸öwavÎļþ£¬Ö»ÒªÈçÇ°ÃæËùÊö£¬Ê¹ÓÃÃüÁîcp test.wav >/dev/audio¼´¿É£¬µ±È»ÄãÒ²¿ÉÒÔÓÃLinuxÏÂÆäËûµÄ¶àýÌåÈí¼þÀ´²¥·ÅÕâ¸öÎļþ¡£

    ¡¡¡¡ÏÂÃæ¼´ÊÇÍêÕûµÄ³ÌÐòÇåµ¥£º

    ÒýÓÃ:
    /* ´ËÎļþÖж¨ÒåÁËÏÂÃæËùÓÐÐÎÈçSND_µÄ±äÁ¿*/
    #include
    #include
    #include
    #include
    #include

    main()
    {
    /* id£º¶ÁÈ¡ÒôƵÎļþÃèÊö·û£»fd£ºÐ´ÈëµÄÎļþÃèÊö·û¡£i£¬jΪÁÙʱ±äÁ¿*/
    int id,fd,i,j;
    /* ´æ´¢ÒôƵÊý¾ÝµÄ»º³åÇø£¬¿ÉÒÔµ÷Õû*/
    char testbuf[4096];
    /* ´ò¿ªÉù¿¨É豸£¬Ê§°ÜÔòÍ˳ö*/
    if ( ( id = open ( "/dev/audio", O_RDWR ) ) < 0 ) {
    fprintf (stderr, " Can't open sound device!\n");
    exit ( -1 ) ;
    }
    /* ´ò¿ªÊä³öÎļþ£¬Ê§°ÜÔòÍ˳ö*/
    if ( ( fd = open ("test.wav",O_RDWR))<0){
    fprintf ( stderr, " Can't open output file!\n");
    exit (-1 );
    }
    /* ÉèÖÃÊʵ±µÄ²ÎÊý£¬Ê¹µÃÉùÒôÉ豸¹¤×÷Õý³£*/
    /* ÏêϸÇé¿öÇë²Î¿¼Linux¹ØÓÚÉù¿¨±à³ÌµÄÎĵµ*/
    i=0;
    ioctl (id,SNDCTL_DSP_RESET,(char *)&i) ;
    ioctl (id,SNDCTL_DSP_SYNC,(char *)&i);
    i=1;
    ioctl (id,SNDCTL_DSP_NONBLOCK,(char *)&i);
    i=8000;
    ioctl (id,SNDCTL_DSP_SPEED,(char *)&i);
    i=1;
    ioctl (id,SNDCTL_DSP_CHANNELS,(char *)&i);
    i=8;
    ioctl (id,SNDCTL_DSP_SETFMT,(char *)&i);
    i=3;
    ioctl (id,SNDCTL_DSP_SETTRIGGER,(char *)&i);
    i=3;
    ioctl (id,SNDCTL_DSP_SETFRAGMENT,(char *)&i);
    i=1;
    ioctl (id,SNDCTL_DSP_PROFILE,(char *)&i);
    /* ¶ÁÈ¡Ò»¶¨ÊýÁ¿µÄÒôƵÊý¾Ý£¬²¢½«Ö®Ð´µ½Êä³öÎļþÖÐÈ¥*/
    for ( j=0; j<10;){
    i=read(id,testbuf,4096);
    if(i>0){
    write(fd,filebuf,i);
    j++;
    }
    }
    /* ¹Ø±ÕÊäÈë¡¢Êä³öÎļþ*/
    close(fd);
    close(id);
    }