Intel® FPGA University Program
University Program Material, Education Boards, and Laboratory Exercises
1175 Discussions

C Programming with Audio IP Cores

Altera_Forum
Honored Contributor II
1,707 Views

I am trying to take an inputted Audio file and trying to save it to the SDRAM (ie recording a word said into a microphone to the memory). 

 

My system uses a DE2 board with a Nios II Processor and Quartus 7.2 software. 

The Nios II system uses the University program's audio codec IP cores. 

 

I have created a C program using the functions provided by the audio core (found on the Altera website), but am having trouble saving an audio stream to the memory and then playing it back. 

 

Below is my code and any help you could provide would be greatly appreciated. 

 

Thanks, 

Mel 

 

 

 

# include <Z:\try3\app_software\include\alt_up_audio.h

# define Switch (volatile char *) 0x01001020 

 

# define MAX_DATA 4096 

 

int main() 

unsigned len[2000]; 

alt_u32 audiobuf[128]; //audio buffer 

alt_u32 audiostream[MAX_DATA]; //for audio storage 

unsigned index= 0; 

int ii,jj,NUM=0; 

 

 

// Switch 0 should be off for playback 

 

alt_up_audio_reset_audio_core(); //initiating the Audio Core 

 

while(1){ 

 

if ((*Switch) & 0x01){ // if Switch 0 is on, read data in through 

microphone 

 

len[NUM] = alt_up_audio_read_left_channel(audiobuf,128); //array of 

lengths stored for playback 

 

if((index + len[NUM]) < MAX_DATA){ // check for data overflow 

for(ii = 0; ii < len[NUM]; ii++){ 

audiostream[ii+index] = audiobuf[ii]; //saving all the data to an 

array for playback 

 

index = index+len[NUM++]; 

 

else if((*Switch) & 0x02){ // if Switch 2 is on, playback data to 

line-out 

 

index = 0; 

for (jj = 0; jj < NUM; jj++){ 

alt_up_audio_write_left_channel(audiostream+index,len[jj]); 

alt_up_audio_write_right_channel(audiostream+index,len[jj]); 

index=index+len[jj]; 

NUM = 0; 

}
0 Kudos
7 Replies
Altera_Forum
Honored Contributor II
719 Views

What kind of trouble are you in, recording or playback?  

 

One thing, if you are using NiosII/f with data cache, you can not get correct value of Switch because of data caching, I think. To bypass data cache, you have to define  

# define Switch ((volatile char *) (0x01001020 | 0x80000000)) 

or read through IORD macro 

IORD(Switch,0) instead of (*Switch)
0 Kudos
Altera_Forum
Honored Contributor II
719 Views

Thanks, I figured it out though. 

It was because I was incrementing the value NUM within a statement: 

 

index = index+len[NUM++]; 

 

So it was taking the next value, when instead I wanted the present value and the incrementation to happen after. I also changed the max values and the unsigned integers to regular integers. 

 

Seems to work now. This is what I have: 

 

# include <Z:\try3\app_software\include\alt_up_audio.h

# define Switch (volatile char *) 0x01001020 

# define MAX_DATA 480000 

 

int main() 

int len[480000]; 

alt_u32 audiobuf[128]; 

alt_u32 audiostream[MAX_DATA]; 

int index= 0; 

int ii,jj,NUM=0; 

 

 

// Switch 0 should be off for playback 

 

alt_up_audio_reset_audio_core(); 

while(1) 

if ((*Switch) & 0x01) 

len[NUM] = alt_up_audio_read_left_channel(audiobuf,128); 

if((index + len[NUM]) < MAX_DATA) // chek for data overflow 

for(ii = 0; ii < len[NUM]; ii++){ 

audiostream[ii+index] = audiobuf[ii]; 

index = index+len[NUM]; 

NUM=NUM+1; 

 

else if((*Switch) & 0x02) 

index = 0; 

for (jj = 0; jj < NUM; jj++){ 

 

alt_up_audio_write_left_channel(audiostream+index,len[jj]); 

alt_up_audio_write_right_channel(audiostream+index,len[jj]); 

index=index+len[jj]; 

NUM = 0; 

index = 0; 

}
0 Kudos
Altera_Forum
Honored Contributor II
719 Views

Hi, I'm trying to do a similar program with Audio IP core but I am using the the latest University IP audio core ==> "altera_up_avalon_audio.h". 

 

The problem I am having is that before I execute my code on Nios, I connect my 3.55 mm audio cable from my PC to the MIC or LineIn port and I am able to listen from earphones what the PC's audio out is from the audio out of the board without executing the code on Nios. This occurs right after I compile and run the board. Does anyone have ideas? 

 

Also, I was wondering foleyme, what component you used to develop with just the normal "alt_up_audio.h". 

 

 

Thanks, 

 

Charlton
0 Kudos
Altera_Forum
Honored Contributor II
719 Views

Also, when plugging in a MP3 player and playing music into the MIC port, I am able to hear music from the LINE-OUT port.  

 

The main problem is that even after adding the audio components from the Audio IP cores to the Quartus hardware generated (Audio and Video Config Core, Audio Core and DE Board External Interface) and setting in the Audio Config to "Power Down Audio Chip", I am still able to hear music from the MP3 player. 

 

Is this supposed to be occuring or is there a mistake I am making in regard to the adding of my hardware components?
0 Kudos
Altera_Forum
Honored Contributor II
719 Views

I'm having the same problem as well, even when i configure it to avoid the bypass mode. Also, I can't enable the fifospace. When i try to write to the audio, the fifospace is always emptly. 

 

If anyone can help it would be great.
0 Kudos
Altera_Forum
Honored Contributor II
719 Views

Well I found out what the problem was and it was basically my stupidity. When building the hardware, not all the pins were connected so you may want to check to see if the PIN map is correct.

0 Kudos
Altera_Forum
Honored Contributor II
719 Views

hi! 

i m working on talking mic input and store it in sdram... 

i m using Quartus 8. 

i m using the following components 

1.NIOS2 cpu 

2. SDRAM 

3. Audio Core 

4. Audio and Video Config Core... 

 

but on simply running the " hello world" software on this, it wudnt build... 

also if i use the sopc builder file given in the examples itself, it wud build in NIOS IDE, but on adding the audio core and audio and video and video config in SOPC builder and then trying to builder the "hello world" on it, it gives error...  

plz help!
0 Kudos
Reply