Problems with converting Arduino Nano code over to Maple Mini. Help needed.

victor_pv
Posts: 1737
Joined: Mon Apr 27, 2015 12:12 pm

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by victor_pv » Wed Mar 01, 2017 7:37 pm

Buleste wrote:Thanks Victor
Okay changed to the following.

Code: Select all

if(TMRpcm_sample_RATE > 48000 ){ TMRpcm_sample_RATE = 48000;
	#if defined (debug)
  	  	Serial.print("TMRpcm_sample RATE TOO HIGH: ");
  	  	Serial.println(TMRpcm_sample_RATE);
  	#endif
  	}
	if(TMRpcm_sample_RATE == 48000 ){
		resolution = 260; //250
		repetition = 5;
	}
	else if(TMRpcm_sample_RATE == 44100 ){
		resolution = 285; //272
		repetition = 5;
	}
	else if(TMRpcm_sample_RATE == 22050 ){
		resolution = 285; //272
		repetition = 11;
	}
	else if(TMRpcm_sample_RATE == 8000 ){
		resolution = 260; //250
		repetition = 35;
	}
	else {
		resolution = 72000000/TMRpcm_sample_RATE;
		repetition = resolution/256;
		resolution = resolution/repetition;
		repetition = repetition-1;
	}
Made those changes and still no joy playing wavs.
I believe there is a note in the sketch, but the default Max number of tasks in CoOS is not enough for that sketch, make sure you have that setting with a number high enough for the number of tasks.

Buleste
Posts: 43
Joined: Sun Nov 06, 2016 12:19 pm

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by Buleste » Wed Mar 01, 2017 11:05 pm

I think I know where my problem is with MapleTape, I put the load buffer in a stupid place and it's just playing the first half to full second of the WAV before going back to the start.

victor_pv
Posts: 1737
Joined: Mon Apr 27, 2015 12:12 pm

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by victor_pv » Thu Mar 02, 2017 12:32 am

Buleste wrote:I think I know where my problem is with MapleTape, I put the load buffer in a stupid place and it's just playing the first half to full second of the WAV before going back to the start.
Just downloaded your code for that one, it seems like it only calls buffer_load once. The way it works, at least in my version, is that buffer load needs to be recalled until the file playing has either reached the end, or has stopped.

So you should put in a while loop. There is a variable called TMRpcm_playing, as long as it's true, the file has not finished yet.

So you could do something like:

Code: Select all

while (TMRpcm_playing)
{TMRpcm_buffer_load}
When it reaches the end of the file, it will exit the loop.

Of course you could add stuff like checking for a button or anything else during the loop to be able to pause or stop the file, then it will exit the loop too.

In the original there is an interrupt set to reload the timer. I thought on using the systick, but I abandoned the idea, don't remember why, probably cause it was way too frequent, so it was nesting, but you could use another Hardware time, and set the interrupt callback to the buffer_load function. Calling it every 5ms should be enough for files of any frequency.

EDIT:
Found another 2 errors, that would cause 16bit files to sound like noise.
There are 2 lines like this:

Code: Select all

TMRpcm_buffer[a][bytes_filled+n] = (TMRpcm_read_buffer[n*2]+128);
You need to change them to this:

Code: Select all

TMRpcm_buffer[a][bytes_filled+n] = (TMRpcm_read_buffer[1+(n*2)]+128);
Last edited by victor_pv on Thu Mar 02, 2017 2:41 pm, edited 1 time in total.

Buleste
Posts: 43
Joined: Sun Nov 06, 2016 12:19 pm

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by Buleste » Thu Mar 02, 2017 10:57 am

Thanks Victor.

That seems to have done the trick.

Still some bugs (doesn't stop at the end of the file) but it plays .WAV files. Now to see if the computers will recognise them.

victor_pv
Posts: 1737
Joined: Mon Apr 27, 2015 12:12 pm

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by victor_pv » Thu Mar 02, 2017 2:47 pm

Buleste wrote:Thanks Victor.

That seems to have done the trick.

Still some bugs (doesn't stop at the end of the file) but it plays .WAV files. Now to see if the computers will recognise them.
:) I'll have another look when I have some time, may have more bugs here and there. I didn't use it other than testing it played some files, and didn't intend to leave like that, but finish it, clean the code, etc. As I do corrections here and there I'll let you know or anything relevant, and will upload it to github again when it's more or less clean. I had planned to use DMA to load the timers, that would take CPU utilization way down, and remove all the interrupts that happen now to load the PWM cycles.

Buleste
Posts: 43
Joined: Sun Nov 06, 2016 12:19 pm

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by Buleste » Thu Mar 02, 2017 2:56 pm

victor_pv wrote: :) I'll have another look when I have some time, may have more bugs here and there. I didn't use it other than testing it played some files, and didn't intend to leave like that, but finish it, clean the code, etc. As I do corrections here and there I'll let you know or anything relevant, and will upload it to github again when it's more or less clean. I had planned to use DMA to load the timers, that would take CPU utilization way down, and remove all the interrupts that happen now to load the PWM cycles.
That's amazing Victor. Thanks. And I think I've figured out why I couldn't get my ILI9341 version of the wav player to play wavs. All mine on the SD card were *.wav rather than *.WAV.

BTW I'm using 8-bit samples but at various frequencies with the MapleTape and they are working beautifully.

victor_pv
Posts: 1737
Joined: Mon Apr 27, 2015 12:12 pm

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by victor_pv » Thu Mar 02, 2017 6:24 pm

Buleste wrote:
victor_pv wrote: :) I'll have another look when I have some time, may have more bugs here and there. I didn't use it other than testing it played some files, and didn't intend to leave like that, but finish it, clean the code, etc. As I do corrections here and there I'll let you know or anything relevant, and will upload it to github again when it's more or less clean. I had planned to use DMA to load the timers, that would take CPU utilization way down, and remove all the interrupts that happen now to load the PWM cycles.
That's amazing Victor. Thanks. And I think I've figured out why I couldn't get my ILI9341 version of the wav player to play wavs. All mine on the SD card were *.wav rather than *.WAV.

BTW I'm using 8-bit samples but at various frequencies with the MapleTape and they are working beautifully.
The code can play both 16bit and 8 bit ones, but they output is always 8bit PWM resolution to allow the timer run at faster frequencies, to reduce noise.
So using 16bit wavs will just increase the processing time by the library, but not the quality.
About wav and WAV, I guess that's another bug to iron out ;)

Buleste
Posts: 43
Joined: Sun Nov 06, 2016 12:19 pm

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by Buleste » Sat Mar 04, 2017 4:16 pm

Oh my. I've gotten CASMaple to work now. Not entirely sure how I did it but I did. I shall have to try TZXMaple now.

victor_pv
Posts: 1737
Joined: Mon Apr 27, 2015 12:12 pm

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by victor_pv » Mon Mar 06, 2017 6:45 am

Buleste wrote:Oh my. I've gotten CASMaple to work now. Not entirely sure how I did it but I did. I shall have to try TZXMaple now.
Nice :)

I have been making some changes to my code, and now uses DMA for loading the samples to the timer registers, that has reduced the CPU load down about 16 percentage points.
I'll try now to do a lot of clean up in that code, turn it into a class, and try to make it match the original TMRpcm API closely, so it can more easily be used.

Using DMA only places a bit of limitation in that DMA channels are shared by different devices and only 1 of them can use DMA at a time. Since the wav playing is a continues process, it requires exclusive use of the DMA channel. Timer 1 Update event shares the DMA channel with SPI2, which I was using for the display, so I had to either not use DMA in the display, or use a different Timer. I opted for that and my sample code uses Timer4 now.

Anyway, I will try to document all that with the library once I have done some cleaning of the code. I don't think I have done any more bugfix at the moment that you may need, so I'll not post any code update at the moment until the clean up is done.

User avatar
RogerClark
Posts: 7433
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Problems with converting Arduino Nano code over to Maple Mini. Help needed.

Post by RogerClark » Mon Mar 06, 2017 9:05 am

Thanks

Post Reply