📄 usbaudiodemo.c
字号:
if (FROM_LITTLEW (fmtChunk.formatTag) == WAV_FMT_MS_PCM) { memset (&fmt, 0, sizeof (fmt)); fmt.formatTag = USB_AUDIO_TYPE1_PCM; fmt.formatType = USB_AUDIO_FORMAT_TYPE1; fmt.channels = FROM_LITTLEW (fmtChunk.channels); fmt.subFrameSize = FROM_LITTLEW (fmtChunk.blockAlign) / FROM_LITTLEW (fmtChunk.channels); fmt.bitRes = FROM_LITTLEW (fmtChunk.fmt.msPcm.bitsPerSample); fmt.sampleFrequency = FROM_LITTLEL (fmtChunk.samplesPerSec); if (pSpkrSeqDev->sd_ioctl (pSpkrSeqDev, USB_SPKR_IOCTL_SET_AUDIO_FORMAT, (int) &fmt) == OK) { printf ("\nusbSpeakerLib format set successfully.\n"); } else { printf ("\nFailed to set usbSpeakerLib format.\n"); return ERROR; } } } else if (memcmp (chunkHdr.chunkId, RIFF_WAV_DATA_CHUNK_SIG, RIFF_CHUNK_ID_LEN) == 0) { /* data chunk found */ /* launch thread to dump audio data. */ wavDataLen = FROM_LITTLEL (chunkHdr.length); if (OSS_THREAD_CREATE (audioThread, (pVOID) wavFile, OSS_PRIORITY_INHERIT, "tPlay", &thread) != OK) { printf ("Cannot create audio play thread.\n"); return ERROR; } else { audioThreadBusy = TRUE; return OK; } } else { /* Skip over the chunk. */ fseek (wavFile, chunkLen, SEEK_CUR); } } return ERROR; }/*************************************************************************** play - sends a .wav file to a speaker** RETURNS: OK or ERROR*/STATUS play (char * fileName) { char fnm [MAX_CMD_LEN]; FILE *f;/* strcpy (fnm, fileList[songIndex]);*/ /* Make sure usbSpeakerLib is initialized and a speaker is available */ if (!spkrInitialized) { printf ("USB speaker SEQ_DEV driver not initialized.\n"); return ERROR; } if (audioThreadBusy) { printf ("audioThread is busy.\n"); return ERROR; } if (waitForSpeaker () != OK) return ERROR; /* Attempt to open the file. */ if ((f = fopen (fileName, "rb")) == NULL) { printf ("Unable to open '%s'.\n", fileName); return ERROR; } /* Parse the file */ if (parseWavFile (f) != OK) fclose (f); return OK; }/*************************************************************************** stop - halts the playing of a wav** RETURNS: OK or ERROR*/STATUS stop (void) { /* Make sure usbSpeakerLib is initialized and a speaker is available */ if (!spkrInitialized) { printf ("USB speaker SEQ_DEV driver not initialized.\n"); return ERROR; } if (!audioThreadBusy) { printf ("No audio is being played.\n"); return ERROR; } stopFlag = TRUE; return OK; }/*************************************************************************** vol - displays or sets speaker volume** RETURNS: OK or ERROR*/LOCAL STATUS vol ( long volume /* Generic parameter passed down */ ) { short volSetting; UINT16 channels; pUSB_SPKR_CHANNEL_CAPS pCaps; UINT16 i; /* verify a speaker is available */ if (!spkrInitialized) { printf ("USB speaker SEQ_DEV driver not initialized.\n"); return ERROR; } if (waitForSpeaker () != OK) return ERROR; /* Try to get channel information. */ if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, USB_SPKR_IOCTL_GET_CHANNEL_COUNT, (int) &channels) != OK) { printf ("IOCTL GET_CHANNEL_COUNT returned ERROR.\n"); return ERROR; } if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, USB_SPKR_IOCTL_GET_CHANNEL_CAPS, (int) &pCaps) != OK) { printf ("IOCTL GET_CHANNEL_CAPS returned ERROR.\n"); return ERROR; } /* Get volume parameter (if specified). */ /* If volume specified, then set it, else display current volume info */ for (i = 0; i <= channels; i++) { printf ("Channel %d: ", i); if (!pCaps [i].volume.supported) { printf ("Volume not supported.\n"); } else { if (volume != -1) { /* Set volume */ globalVolume = (long) volSetting = (short) (volume & 0xffff); if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, USB_SPKR_IOCTL_SET_VOLUME, (int) ((i << 16) | ((UINT16) volSetting))) == OK) { printf ("Volume set to %hx.\n", volSetting); } else { printf ("Error setting volume.\n"); } } else { /* Show volume settings. */ printf ("res = %hx, min = %hx, max = %hx, cur = %hx.\n", pCaps [i].volume.res, pCaps [i].volume.min, pCaps [i].volume.max, pCaps [i].volume.cur); } } } return OK; }/*************************************************************************** disVol - displays volume levels in hex** RETURNS: OK or ERROR*/STATUS disVol (void) { return (vol(-1)); } /*************************************************************************** up - increases the volume** RETURNS: OK or ERROR*/STATUS up (void) { return (vol(globalVolume + 0x200)); } /*************************************************************************** down - increases the volume** RETURNS: OK or ERROR*/STATUS down (void) { return (vol(globalVolume - 0x200)); }/*************************************************************************** down - increases the volume** RETURNS: OK or ERROR*/STATUS mute (void) { return (vol(0xd000)); }/*************************************************************************** usbSpkrInit - initializes USB speaker SEQ_DEV driver** RETURNS: ERROR or OK*/UINT16 usbSpkrInit() { if (spkrInitialized) { printf ("USB speaker SEQ_DEV driver already initialized.\n"); return ERROR; } /* Initialize the speaker driver */ if (usbSpeakerDevInit () == OK) { printf ("usbSpeakerDevInit() returned OK\n"); spkrInitialized = TRUE; /* Register for attach notification */ if (usbSpeakerDynamicAttachRegister ((USB_SPKR_ATTACH_CALLBACK)spkrAttachCallback, (pVOID)NULL) != OK) { printf ("usbSpeakerDynamicAttachRegister() returned ERROR\n"); return OK; } } else printf ("usbSpeakerDevInit() returned ERROR\n"); audioThreadBusy = FALSE; return OK; }/*************************************************************************** usbAudioDemo - Entry point to USB audio demo** RETURNS: OK or ERROR*/extern STATUS usbAudioDemo (void) { int fd; REQ_DIR_ENTRY rde; char * directoryName; /* Initialize the speaker class driver */ usbSpkrInit(); directoryName = "/ata0"; /* Initialize the storage device */ if (usrAtaConfig(0, 0, directoryName) != OK) { printf ("ATA configuration failed.\n"); return ERROR; } return OK; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -