[SOLVED!] [Help Request] Simple Sound Addition

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
User avatar
eskayelle
Posts: 16
Joined: Wed Jul 29, 2020 5:07 pm
Contact:

[SOLVED!] [Help Request] Simple Sound Addition

Post by eskayelle » Sat Aug 01, 2020 12:54 pm

Hey, there. Apologies for jumping the gun on my first post a few days back; it likely should have gone in the Newbie Help Center too.

In any event, I'm working on a Pong game (something reminiscent of the old AY-3-8550 chips used in those '70s Pong clones), so I'd like to add some very simple beeps. The Nerdy Night tutorials get into more music than sound effects, so I'm not sure I really need a detailed sound engine; I'm really just looking to be able to call a short beep (perhaps in the Square 1 register?) upon certain events (like when the score is incremented).

I see that I can start with something like this to enable the sound channel...

Code: Select all

    lda #%00000001
    sta $4015 ;enable square 1
 
    lda #%10111111 ;Duty 10, Volume F
    sta $4000
 
but is there a small subroutine I can then add (add jump to at certain events) that plays a sound that doesn't run in an infinite loop? If I manually set the length counter bit, is there a fairly simple code snippit to call a sound (any note will do for now) with a short length defined (perhaps somewhere between a 1/32 note and a 1/8)?

Any efficient methods/examples you might be able to point me to?
Last edited by eskayelle on Sun Aug 02, 2020 10:46 am, edited 1 time in total.

User avatar
NeverCameBack
Posts: 36
Joined: Mon Feb 24, 2020 12:22 am

Re: [Help Request] Simple Sound Addition

Post by NeverCameBack » Sat Aug 01, 2020 1:58 pm

I'm not sure off the top of my head because I'm working on learning more about the NN sound engine myself, but in the second chapter in that sound tutorial, there is a sample program called "triad" that you can download. In the description it says:
"Try to silence the various channels by either disabling them via $4015 or silencing them via $4000/$4004/$4008."

So I imagine if you command the APU to turn on a sound, you can silence that sound by turning the volume on that channel to zero after some amount of time. I made a clock variable by increasing the variable each time the NMI cycles through (60 ticks per second). So I would use that to play the sound for as long as you want, but it could be better to play the sound and alter it a little over the course of several frames, and then turn the channel volume to 0.

User avatar
eskayelle
Posts: 16
Joined: Wed Jul 29, 2020 5:07 pm
Contact:

Re: [Help Request] Simple Sound Addition

Post by eskayelle » Sat Aug 01, 2020 7:30 pm

Interesting. I tried the tutorial and added the silence code and got a blip. I applied the logic to my code but got an incessant beep. So I pulled everything in as a subroutine to just call to it during certain events.

Code: Select all

square:
  lda #%00000111 
  sta $4015 ;enable Square 1, Square 2 and Triangle
  lda #%00111000 ;Duty 00, Length Counter Disabled, Saw Envelopes disabled, Volume 8
  sta $4000
  lda #$C9    ;0C9 is a C# in NTSC mode
  sta $4002   ;low 8 bits of period
  lda #$00
  sta $4003   ;high 3 bits of period
  LDY #$05
  LDX #$05
kill1:
  JSR kill2
  DEX
  BNE kill1
  DEY
  BNE kill1
kill2: 
  lda #$00
  sta $4000
  sta $4015
  rts
The kill labels are my counter. Looks like when I use it, the loads into X and Y cause my screen background to fail on the right side. Removing the counter gives me pure silence, no beep. Removing the STA to $4015 at the end gives me an infinite beep regardless of hitting my events or not. Do you happen to have a different counter concept I could try?

Any other thoughts on how I might get a quick beep to play? How might that NMI variable work?

User avatar
Controllerhead
Posts: 90
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: [Help Request] Simple Sound Addition

Post by Controllerhead » Sat Aug 01, 2020 9:12 pm

Hey stranger. The length counter (cutoff for sound timing) is bits 3-7 of the fourth register of each non-DPCM channel, so, $4003 %LLLLLxxx, for example. As long as bit 5 of the first register (for square and noise), say $4000, is %xx0xxxxx, the length counter will halt your sound after a specified amount of time. Here is a chart that displays each timing for each bit value at the bottom of the first table here.
https://wiki.nesdev.com/w/index.php/APU_Length_Counter

Code: Select all

     |  0   1   2   3   4   5   6   7    8   9   A   B   C   D   E   F
-----+----------------------------------------------------------------
00-0F  10,254, 20,  2, 40,  4, 80,  6, 160,  8, 60, 10, 14, 12, 26, 14,
10-1F  12, 16, 24, 18, 48, 20, 96, 22, 192, 24, 72, 26, 16, 28, 32, 30
Fig 1.1: A convoluted chart

Anyway, the timing values for the length counter ...uhhh ...are absolute lunacy and make no logical sense whatsoever. Bits might as well be random. Most lengths are too short to be useful. I kind of just play with the bits until i get a length that sounds good. How do i do that? Well...

I modified an existing APU test program to make sound FX and instruments. You may find it useful. Enjoy!

Image

Anyway, in the meantime, here's a nice little pong blip.

Code: Select all

;  Pong blip. Make sure $4015 is enabled
LDA #%10000011
STA $4000
LDA #$00
STA $4001
STA $4002
LDA #%01000001
STA $4003
Also, the triangle length counter disable is bit 7 of $4008 %1xxxxxxx. It does have an extra length counter, a "linear counter", for some reason, in bits 0-6 of $4008 %xLLLLLLL. It acts fairly reasonably; larger values will make it longer, shorter values will make it shorter. Both counters can be active, and both will halt your sound.

DPCM is its own beast... save that for a rainy day...
Attachments
sndtest2.zip
(22.89 KiB) Downloaded 10 times

User avatar
eskayelle
Posts: 16
Joined: Wed Jul 29, 2020 5:07 pm
Contact:

Re: [Help Request] Simple Sound Addition

Post by eskayelle » Sun Aug 02, 2020 10:46 am

My man! Thanks for the tips! I looked through the asm and messed with the sound test rom and got some beeps and boops working. I realized I stupidly set my sounds to play during the draw score subroutine, which is "always" being called. Pulling my code back to the score implemenation subroutine stopped the incessant beep, and I'm back in business!

Post Reply