void audioPlayNote(U8 note) { int fifospaceR,fifospaceL; U16 toWriteNbL=100,toWriteNbR=100; if(audioOutLIndex>=zMax[note]) audioOutLIndex=0; if(audioOutRIndex>=zMax[note]) audioOutRIndex=0; if((zMax[note]-audioOutLIndex)<100) toWriteNbL=zMax[note]-audioOutLIndex; if((zMax[note]-audioOutRIndex)<100) toWriteNbR=zMax[note]-audioOutRIndex; fifospaceR = (IORD_ALT_UP_AUDIO_FIFOSPACE(audio_dev->base)& ALT_UP_AUDIO_FIFOSPACE_WSRC_MSK) >> ALT_UP_AUDIO_FIFOSPACE_WSRC_OFST; fifospaceL = (IORD_ALT_UP_AUDIO_FIFOSPACE(audio_dev->base)& ALT_UP_AUDIO_FIFOSPACE_WSLC_MSK) >> ALT_UP_AUDIO_FIFOSPACE_WSLC_OFST; if(fifospaceL > toWriteNbL) { alt_up_audio_write_fifo(audio_dev,&audioYout[note][audioOutLIndex],toWriteNbL, ALT_UP_AUDIO_LEFT); audioOutLIndex+=toWriteNbL; } if(fifospaceR > toWriteNbR) { alt_up_audio_write_fifo(audio_dev,&audioYout[note][audioOutRIndex],toWriteNbR, ALT_UP_AUDIO_RIGHT); audioOutRIndex+=toWriteNbR; } }
int main() { int main(void) { alt_up_audio_dev * audio_dev; /* used for audio record/playback */ unsigned int l_buf; unsigned int r_buf; // open the Audio port audio_dev = alt_up_audio_open_dev ("/dev/Audio"); if ( audio_dev == NULL) alt_printf ("Error: could not open audio device \n"); else alt_printf ("Opened audio device \n"); /* read and echo audio data */ while(1) { int fifospace = alt_up_audio_read_fifo_avail (audio_dev, ALT_UP_AUDIO_RIGHT); if ( fifospace > 0 ) // check if data is available { // read audio buffer alt_up_audio_read_fifo (audio_dev, &(r_buf), 1, ALT_UP_AUDIO_RIGHT); alt_up_audio_read_fifo (audio_dev, &(l_buf), 1, ALT_UP_AUDIO_LEFT); // write audio buffer alt_up_audio_write_fifo (audio_dev, &(r_buf), 1, ALT_UP_AUDIO_RIGHT); alt_up_audio_write_fifo (audio_dev, &(l_buf), 1, ALT_UP_AUDIO_LEFT); } } }
/* * Overview: This plays the boom sound when the tanks are hit * */ void play_doh(alt_up_audio_dev *audio, unsigned int *buffer, short int handle) { int lbytes; int rbytes; int lindex=22; int rindex=22; alt_up_audio_dev* aud=audio; int i=0; while(i<DOH_LENGTH) //200 for cha ching { if (alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_LEFT) > 22) { lbytes = alt_up_audio_write_fifo(aud, &buffer[lindex], 96, ALT_UP_AUDIO_LEFT); rbytes = alt_up_audio_write_fifo(aud, &buffer[rindex], 96, ALT_UP_AUDIO_RIGHT); lindex += lbytes; rindex += rbytes; i++; } } //printf("all good things come to an end\n"); alt_up_sd_card_fclose(handle); }
void lose_isr(void* context, alt_u32 id) { // FIFO is 75% empty, need to fill it up int sample_counter; for (sample_counter = 0; sample_counter < SAMPLE_SIZE; sample_counter++) { // take 2 bytes at a time byte_data[0] = lose_data[sound_data_counter]; byte_data[1] = lose_data[sound_data_counter+1]; sound_data_counter += 2; // combine the two bytes and store into sample buffer sound_buff[sample_counter] = (byte_data[1] << 8) | byte_data[0]; // if we finish reading our data buffer, then we loop back to start over if (sound_data_counter >= LOSE_SIZE) { sound_data_counter = 0; alt_up_audio_disable_write_interrupt(audio_dev); } } // finally, we write this sample data to the FIFO alt_up_audio_write_fifo(audio_dev, sound_buff, SAMPLE_SIZE, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio_dev, sound_buff, SAMPLE_SIZE, ALT_UP_AUDIO_RIGHT); }
void audio_ISR() { alt_up_audio_write_fifo(audio, &audio_stream[stream_position], BUFFER_SIZE, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, &audio_stream[stream_position], BUFFER_SIZE, ALT_UP_AUDIO_RIGHT); stream_position += BUFFER_SIZE; if (stream_position >= song_size - BUFFER_SIZE) { stream_position = 0; } }
// Requires: // Effects: Plays an audio .wav file, blocking the CPU until the song has completed. int playBlockingMusic(char* audioFile) { int i = 0; int currentSample = 0; unsigned int *sample; file_handle fileHandle = open_file(audioFile, false); if (fileHandle < 0) { printf("Reading file failed \n"); return AUDIO_ERROR; } int fileLength = findWavSize(fileHandle); // Allocate a buffer that is the same byte size as the file unsigned int *buf = (unsigned int*) malloc(fileLength * 2); int bufSize = fileLength/2; for (i = 0; i < bufSize; i++) { // Extract data and store in the buf. unsigned char firstByte = read_file(fileHandle); unsigned char secondByte = read_file(fileHandle); unsigned short val = (secondByte << 8) | firstByte; buf[i] = val; } // Close the file on the sd card. alt_up_sd_card_fclose(fileHandle); while (currentSample < bufSize) { unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); if (space > bufSize - currentSample) { // Don't need to fully fill the rest of the buffer. space = bufSize - currentSample; } if (space > 0) { sample = &(buf[currentSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); currentSample += space; } } free(buf); return 0; }
void swapInSound(int* buf, int len) { if (bufSizeSwap == 0) { musicSwapBuffer = interruptMusicBuffer; bufSizeSwap = interruptBufSize; swapSample = interruptSample; swapLoop = musicLoop; swapDone = musicDone; interruptMusicBuffer = buf; interruptBufSize = len; interruptSample = 0; musicLoop = 0; musicDone = 0; unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); unsigned int* sample; if (space > interruptBufSize - interruptSample) { // Don't need to fully fill the rest of the buffer. space = interruptBufSize - interruptSample; } if (space > 0) { sample = &(interruptMusicBuffer[interruptSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); interruptSample += space; } if (interruptSample >= interruptBufSize) { if (musicLoop) { interruptSample = 0; alt_up_audio_enable_write_interrupt(audio); } } else { printf("Enabling interrupt."); // Enable the write interrupt alt_up_audio_enable_write_interrupt(audio); } } }
/* Checks if the write FIFO for the left channel has at least BUF_THRESHOLD space available. * If it doesn't, then just returns 0. If it does, then data from buf is written into the * FIFO, up to a maximum of len words. */ unsigned int alt_up_audio_play_l(alt_up_audio_dev *audio, unsigned int *buf, int len) { unsigned int space = alt_up_audio_write_fifo_space (audio, ALT_UP_AUDIO_LEFT); if (space <= BUF_THRESHOLD) return 0; else return (alt_up_audio_write_fifo(audio, buf, len, ALT_UP_AUDIO_LEFT)); }
/*************************************************************************************** * Audio - Interrupt Service Routine * * This interrupt service routine records or plays back audio, depending on which type * interrupt (read or write) is pending in the audio device. ****************************************************************************************/ void audio_ISR(alt_up_audio_dev* audio_dev, unsigned int id) { if (alt_up_audio_write_interrupt_pending(audio_dev)) // check for write interrupt { //int numToWrite = 0; //int spaceAvailable = alt_up_audio_write_fifo_space(audio_dev, ALT_UP_AUDIO_LEFT); int temp = 0, temp1 = 0; if(soundMixer->indexSize <= 0) return; temp = alt_up_audio_write_fifo(audio_dev, soundMixer->buffer[soundMixer->currIndex], 96, ALT_UP_AUDIO_LEFT); temp1 = alt_up_audio_write_fifo(audio_dev, soundMixer->buffer[soundMixer->currIndex], 96, ALT_UP_AUDIO_RIGHT); /*s->position += 96; if(s->position>=s->length) s->position = 0;*/ incIndex(); } return; }
void play_wav (unsigned int **arr) { alt_up_audio_reset_audio_core(audio); unsigned int * input; int k = 0; input = arr[k]; while (1) { if (alt_up_audio_write_interrupt_pending(audio) == 1) { alt_up_audio_write_fifo(audio, input, 100, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, input, 100, ALT_UP_AUDIO_RIGHT); k++; input = arr[k]; } if (k >= 4377) break; } }
void audioPlaySampling(void) { int fifospaceL,fifospaceR; U32 ptrIndexL=0,ptrIndexR=0; while(ptrIndexR<AUDIO_SAMPLING_MAX_DATA||ptrIndexL<AUDIO_SAMPLING_MAX_DATA) { fifospaceL = (IORD_ALT_UP_AUDIO_FIFOSPACE(audio_dev->base)& ALT_UP_AUDIO_FIFOSPACE_WSLC_MSK) >> ALT_UP_AUDIO_FIFOSPACE_WSLC_OFST; if(fifospaceL>100&&ptrIndexL<AUDIO_SAMPLING_MAX_DATA) { alt_up_audio_write_fifo(audio_dev,&audioSamplingLData[ptrIndexL],100, ALT_UP_AUDIO_LEFT); ptrIndexL+=100; } fifospaceR = (IORD_ALT_UP_AUDIO_FIFOSPACE(audio_dev->base)& ALT_UP_AUDIO_FIFOSPACE_WSRC_MSK) >> ALT_UP_AUDIO_FIFOSPACE_WSRC_OFST; if(fifospaceR>100&&ptrIndexR<AUDIO_SAMPLING_MAX_DATA) { alt_up_audio_write_fifo(audio_dev,&audioSamplingRData[ptrIndexR],100, ALT_UP_AUDIO_RIGHT); ptrIndexR+=100; } } }
int loadMusic(char* audioFile, unsigned short loop, float volumeFactor) { int i = 0; unsigned int *sample; alt_up_audio_disable_write_interrupt(audio); printf("Opening file\n"); file_handle fileHandle = open_file(audioFile, false); if (fileHandle < 0) { printf("Reading file failed \n"); return AUDIO_ERROR; } int fileLength = findWavSize(fileHandle); // Discard header-- we are making an assumption about // how the data is stored to make it easier to // add sound to the music. for (i = 0; i < 32; i++) read_file(fileHandle); // Allocate the main music buffer to be the size of the file. if (interruptMusicBuffer != 0) free(interruptMusicBuffer); musicLoop = loop; musicDone = 0; interruptMusicBuffer = (int*) malloc((fileLength-32) * 2); interruptBufSize = (fileLength-32)/2; interruptSample = 0; if (volumeFactor <= 0) volumeFactor = 1; for (i = 0; i < interruptBufSize; i++) { // Extract data and store in the buf. char firstByte = read_file(fileHandle); char secondByte = read_file(fileHandle); short val = ( (unsigned char) secondByte << 8) | (unsigned char) firstByte; val = val * volumeFactor; interruptMusicBuffer[i] = val; } alt_up_sd_card_fclose(fileHandle); unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); if (space > interruptBufSize - interruptSample) { // Don't need to fully fill the rest of the buffer. space = interruptBufSize - interruptSample; } if (space > 0) { sample = &(interruptMusicBuffer[interruptSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); interruptSample += space; } if (interruptSample >= interruptBufSize) { if (musicLoop) { interruptSample = 0; alt_up_audio_enable_write_interrupt(audio); } } else { // Enable the write interrupt alt_up_audio_enable_write_interrupt(audio); } return 0; }
static void playMusicISR (void* context, alt_u32 id) #endif { /* Interrupt for writing*/ unsigned int space = alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_RIGHT); unsigned int* sample; int i; if (space > interruptBufSize - interruptSample) { // Don't need to fully fill the rest of the buffer. space = interruptBufSize - interruptSample; } if (space > 0) { if (addSound) { /* Add a sound in-- must add word by word. */ for (i = 0; i < space; i++) { int currentWord = interruptMusicBuffer[interruptSample++]; if (soundBufferSample < soundBufSize) { currentWord += soundBuffer[soundBufferSample++]; } else { removeSound(); } alt_up_audio_write_fifo(audio, ¤tWord, 1, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, ¤tWord, 1, ALT_UP_AUDIO_RIGHT); } } else { sample = &(interruptMusicBuffer[interruptSample]); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, sample, space, ALT_UP_AUDIO_RIGHT); interruptSample += space; } } if (interruptSample >= interruptBufSize) { if (musicLoop) { interruptSample = 0; } else { musicDone = 1; alt_up_audio_disable_write_interrupt(audio); if (bufSizeSwap != 0) { swapOutSound(); } } } }
//Audio interrupt function, plays sound effects and/or loops audio //REQ: audmode->id must point to correct id void audio_isr(audisr * audmode, alt_u32 irq_id) { int i; unsigned int ** temp; unsigned int * second; if (audmode->resetmix == true) { audmode->resetmix = false; audio_isr_2 = 0; } //if (audmode->mode == 0) { if (audmode->id != audmode->oldid) { audio_isr_k = 0; seek = 0; init_copy(audmode->id, audmode->oldid); audmode->oldid = audmode->id; } if (audmode->id2 != audmode->oldid2) { audio_isr_2 = 0; audmode->oldid2 = audmode->id2; } if (audio_isr_k == 0) { copy_bgm(audmode->id); alt_up_rs232_write_data(uart, 0x6); alt_up_rs232_write_data(uart, (int)audiosecs[audmode->id]); } audio_log.bgmin = copyarr[audio_isr_k]; //Mode 0, loop music if (audmode->mode == 0) { for (i = 0; i < 96; i++) { audio_log.bgmin[i] = volume_adjust(audio_log.bgmin[i], audmode->newvolume); } } else if (audmode->mode == 1) { temp = arr[audmode->id2]; second = temp[audio_isr_2]; for (i = 0; i < 96; i++) { unsigned int tempmix = audio_log.bgmin[i]; audio_log.bgmin[i] = mix_adjust(tempmix,second[i],5); } audio_isr_2++; if (audio_isr_2 > audiosize[audmode->id2]) { audio_isr_2 = 0; audmode->mode = 0; } } if (alt_up_audio_write_interrupt_pending(audio) == 1) { alt_up_audio_write_fifo(audio, audio_log.bgmin, 96, ALT_UP_AUDIO_LEFT); alt_up_audio_write_fifo(audio, audio_log.bgmin, 96, ALT_UP_AUDIO_RIGHT); if (audmode->loop == true){ audio_isr_k = (audio_isr_k + 1) % audiosize[audmode->id]; seek = (seek + 1) % 333; if (seek == 0) { //alt_up_rs232_write_data(uart, 0x0A); } if (audio_isr_k == 0) { alt_up_rs232_write_data(uart, 0x3); } } else { if(audio_isr_k <= audiosize[audmode->id]) { audio_isr_k += 1; seek = (seek + 1) % 333; if (seek == 0) { //alt_up_rs232_write_data(uart, 0x0A); } } else { seek = 0; audmode->mode = 0; audio_isr_2 = 0; audio_isr_k = 0; //alt_up_rs232_write_data(uart, 0x3); alt_up_rs232_write_data(uart, 0x4); wait(); if (audmode->shuffle == false) { audmode->id += 1; if ((audmode->id >= audmode->files) && (audmode->listloop == true)) { audmode->id = 0; //printf("Restarting songs\n"); } else if((audmode->id >= audmode->files) && (audmode->listloop == false)) { printf("End of Playlist\n"); audmode->id = 0; alt_up_audio_disable_write_interrupt(audio); } } else { /* do { while(rs_flag == false); alt_up_rs232_read_data(uart, &data, &parity); }while((int)data == 0); */ wait(); alt_up_rs232_read_data(uart, &data, &parity); int nextindex = (int)data; nextindex -= 4; //printf("Next Index: %d\n", nextindex); if ((nextindex < 0) || (nextindex > audmode->files)) { nextindex = 0; printf("Error, next Index: %d\n", nextindex); } audmode->id = nextindex; } } } } //} /* else if (audmode->mode == 1) { if (audmode->id != audmode->oldid) { //TEMP audmode->id2 = audmode->id++; if (audmode->id2 >= audmode->files) { audmode->id2 = 0; } if (audiosize[audmode->id] > audiosize[audmode->id2]) { audmode->large = audmode->id; } else { audmode->large = audmode->id2; } // audio_isr_k = 0; init_copy(audmode->large, audmode->oldid); audmode->oldid = audmode->id; } if (audio_isr_k == 0) { copy_bgm(audmode->large); alt_up_rs232_write_data(uart, 0x6); } audio_log.bgmin = copyarr[audio_isr_k]; for (i = 0; i < 96; i++) { //audio_log.bgmin[i] = mix_adjust(audio_log.bgmin[i], ,audmode->newvolume); } if (alt_up_audio_write_interrupt_pending(audio) == 1) { audio_isr_k = 0; audmode->id += 1; alt_up_rs232_write_data(uart, 0x3); if ((audmode->id >= audmode->files) && (audmode->listloop == true)) { audmode->id = 0; printf("Restarting songs\n"); } else if((audmode->id >= audmode->files) && (audmode->listloop == false)) { printf("End of Playlist\n"); audmode->id = 0; alt_up_audio_disable_write_interrupt(audio); } } } */ }
euint8 open_wav(SDfile fileinfo) { euint8 buff[600]; euint32 buff2[512]; euint32 right[512]; euint32 left[512]; euint8 leftovers[512]; euint8 leftoverCount=0; euint32 count = 0; euint32 i,zz; euint32 tempright; euint32 temp[5]; euint32 overflow_amount = 0; euint32 LoopC = 0; euint32 stuff_to_write = 0; euint32 ints_to_send = 0; euint32 sample_rate = 0; euint32 bitdepth = 0; euint32 channels = 0; euint32 formatcode = 0; euint32 blocksize = 0; euint32 header_size = 0; euint32 next_address = 0; euint32 clust_address = 0; euint32 FAT_address = 0; euint32 filesize = 0; euint32 end_offset = 0; euint32 bytes_read = 0; euint8 end_of_file = 0; alt_up_audio_dev* audio_dev = alt_up_audio_open_dev(AUDIO_NAME); alt_up_audio_reset_audio_core(audio_dev); filesize = fileinfo.file_size; FAT_address = (euint32)fileinfo.startL | ((euint32)fileinfo.startH)<<16; clust_address = (FAT_address-2)*Part_Boot.sec_per_clust + clust_start; sd_readSector(clust_address,buff); //read in wav header info bytes_read += 512; //address = next_sect_address(address); header_size = get_wav_header(buff,&sample_rate,&bitdepth,&channels,&formatcode,&blocksize); //psuedo code for reading in wav details, returns wav info if(formatcode != 1) { UART_write("Error: incorrect wav format type, please use PCM\n\r"); return(1); } if(channels != 2) { UART_write("Error, number of channels must be 2\n\r"); return(1); } if((bitdepth != 32)&&(bitdepth != 16)&&(bitdepth != 24)&&(bitdepth != 8)) { UART_write("Error, bitdepth is incorrect\n\r"); return(1); } if(sample_rate != 8000) { UART_write("Error, sample rate is incorrect\n\r"); return(1); } switch(sample_rate) { case 8000: AUDIO_SetSampleRate(RATE_ADC8K_DAC8K_USB); break; case 32000: AUDIO_SetSampleRate(RATE_ADC32K_DAC32K_USB); break; case 44100: AUDIO_SetSampleRate(RATE_ADC44K_DAC44K_USB); break; case 48000: AUDIO_SetSampleRate(RATE_ADC48K_DAC48K_USB); break; case 96000: AUDIO_SetSampleRate(RATE_ADC96K_DAC96K_USB); break; default: printf("Non-standard sampling rate\n"); return -1; } count = count + header_size; while(end_of_file == 0) //start reading bytes from offset position, if there are bytes to be read, continue { //convert char buffer into 32 bit buffer while(LoopC < Part_Boot.sec_per_clust) { if(bitdepth == 32) { for(i=0;i<128;i++) { *((unsigned char*)&buff2[i]+0) = buff[count+0]; *((unsigned char*)&buff2[i]+1) = buff[count+1]; *((unsigned char*)&buff2[i]+2) = buff[count+2]; *((unsigned char*)&buff2[i]+3) = buff[count+3]; count += 4; if(count > 512) { ints_to_send = i+1; break; } if(i==127) ints_to_send = i+1; } count = 0; } if(bitdepth == 24) { for(i=0;i<170;i++) // PROBLEM HERE PROBS SINCE 512/3 ISNT WHOLE NUMBER { *((unsigned char*)&buff2[i]+0) = 0; *((unsigned char*)&buff2[i]+1) = buff[count+0]; *((unsigned char*)&buff2[i]+2) = buff[count+1]; *((unsigned char*)&buff2[i]+3) = buff[count+2]; count += 3; if(count >= 512) { count = (count-512)+3; ints_to_send = i+1; break; } } } if(bitdepth == 16) { for(i=0;i<256;i++) { *((unsigned char*)&buff2[i]+0) = 0; *((unsigned char*)&buff2[i]+1) = 0; *((unsigned char*)&buff2[i]+2) = buff[count+0]; *((unsigned char*)&buff2[i]+3) = buff[count+1]; count += 2; if(count > 512) { ints_to_send = i+1; break; } if(i==255) ints_to_send = i+1; } count = 0; } if(bitdepth == 8) { for(i=0;i<512;i++) { *((unsigned char*)&buff2[i]+0) = 0; *((unsigned char*)&buff2[i]+1) = 0; *((unsigned char*)&buff2[i]+2) = 0; *((unsigned char*)&buff2[i]+3) = buff[count+0]; count++; if(count > 512) { ints_to_send = i+1; break; } if(i==511) ints_to_send = i+1; } count = 0; } // pass to left and right channels for(i=0;i<256;i++) { left[i] = buff2[i*2]; right[i] = buff2[i*2+1]; } if(end_offset != 0) { stuff_to_write = end_offset/(1024/ints_to_send); end_of_file = 1; end_offset = 0; } else { stuff_to_write = ints_to_send/2; } //is there space in the fifo? while(alt_up_audio_write_fifo_space(audio_dev,0) < stuff_to_write); //send to audio channels alt_up_audio_write_fifo(audio_dev,right,stuff_to_write,ALT_UP_AUDIO_RIGHT); alt_up_audio_write_fifo(audio_dev,left,stuff_to_write,ALT_UP_AUDIO_LEFT); clust_address++; sd_readSector(clust_address,buff); bytes_read += 512; LoopC++; if(bytes_read >= filesize) { end_of_file = 1; //end_offset = 512 - (bytes_read-filesize); } } LoopC = 0; clust_address = next_sect_address(FAT_address,&next_address); FAT_address = next_address; if(bytes_read >= filesize) { end_of_file = 1; //end_offset = 512 - (bytes_read-filesize); } } return(0); }