Programmers - Digital sound on the Spectrum: How to make SPECCY play digitized sounds.

ZX Format #08
Digital Sounds on Spectrum  
                               
music by DNK                   
(C)PSB SOFTWARE/HALLOWEEN      
________________________________
                               
  Pe this article will talk about how
make SPECCY play digital
bathroom sounds. First of all this article
dedicated to those who have covox, sound-
drive or, at worst, AY.           
  A little theory...From a school course in physics
Ziki You know that sound represents co-
fight a wave changing sinusoidally
according to the law. The amplitude of such a wave is determined
determines the volume of a sound, and frequency determines its intensity
coty.                                    
  So, we need a device that can-
can turn a wave into an analog signal
(i.e. a signal varying in voltage
nia). This device is called a DAC (Digital
ro-Analog Converter) or covox.
DACs come in 8-bit, 4-bit,
10,12,16 and maybe some others
- I don’t know. We are only interested in 4 and
8-bit (since there is a 4-bit DAC in
AY, and the 8-bit DAC is Covox).
8 and 4 bits are very convenient becausefor
data storage and transmission is used
either a whole byte or half of it.      
  The DAC's task is to set the output voltage
range from 0 to N (from -n to +n) volts, where
N is determined by the type of DAC, but this is not for us
important. The higher the bit depth of the DAC, the more
You can reproduce the sound more honestly.     
  The sound wave is stored in digital data
expressed as values from 0 to 255 (0-15 for
AY).  128 is taken as zero (i.e. actually
In fact, 0 is -128, and 255 is +127). Ta-
Which wave is called unsigned.        
  Thus, throwing the kovox into the port
values from the sample body (sound), we get
sound output. It can be done like this
application:                             
                               
        DI ;so that nothing disturbs our program
        LD HL,INS_ADR  ;sample starting address        
        LD BC,INS_END-INS_ADR  ;sample length                  
LOOP LD A,(HL)  ;take a byte from the sample body     
        OUT (#XX),A ; throw it into the covox port       
;(for pentagon #FB, for scorpion - #DD, see belowapprox.)       
        DEFS NN ; we fill the memory for delay with NOPs
                        ;ki, try 20-100.           
        INC HL ;next byte in sample                   
        DEC BC ;decrease the length by one.          
        LD A,B ; check it for zero              
        OR C                                                  
        JR NZ,LOOP ;if not the entire instrument has already been lost
                        ;ral, then we repeat...                 
        ..... ;we do what we need to do,                   
                        ;for example EI                          
        RET ;No comments                          
                               
INS_ADR INCBIN FileName ; load the sample (take it, by the way  
                        ;measure, from the library ed.Instrument)  
INS_END  ;the byte following the tool.    
                               
  This is how it is actually reproduced
sound. But what does this give us? We can't
we can make the computer say something
be the word.  But in PC games...
what? Take PC's *.WAV files and download
I eat them in TR-DOS. Playing wav files
practically no different from the usual
new sample. A wav file has a header.
Moreover, it comes in different lengths. These are the ones
The data that I managed to "fish out":   
                               
#00-#03  string "RIFF"               
#04-#07  length_of_something          
#08-#0F string "WAVEmft"               
#10-#17 ----------                      
#18-#19 rate, kHz                       
#1A-#1B ----------                      
#1C-#1D rate, kHz                      
#1E-#21 ----------                       
#22     digits, bit                      
#23     ----------                       
#24-#27 strings "data" or "fact"         
#28-#2В sound_length  length_of_something
#2C-#2F WAVE!         string "data"      
#30-#33 WAVE!         sound_length       
#..-#..        WAAAAVEEEE!               
                               
  Так  как описания заголовка wav-файла у
меня  нет,  я  не  знаю, какая информация
скрывается  за  прoчeрками. Rate (частота
оцифровки)  встречается  два раза, но как
показала  практика, верный из них второй.
Rate может быть 44100, 22050, 11025, 8000
кГц.  Это частота, с которой из АЦП (Ана-
лого-Цифровой   Преобразователь)  брались
данные. Rate также можноmeasure in [bytes
per second].  Digits shows how many
bit sound.  Further according to the circumstances:
if the word "data" is located from #24-#27, then
from #28-#2B there are four bytes of length
sound (in Low-end format, i.e. as on
Spectrum - least significant byte first)
then the sound body itself. If from #24-#27
is "fact", then the length is unclear -
but what, "data", sound length and, finally,
the sound itself. The title in this case is
It's 8 bytes more. After the sound itself
text and graphics can go (that’s how I’ll come)
small Microsoft).                          
  So, playing wavs is different from
regular samples in a fixed number
NOPs in our program. This quantity
NOP's depend on both Rate and hour-
the number of times the computer works (number of cycles per
interrupt).  Its (quantity) can be dis-
read like this:                              
                               
7+11+4*X+6+6+4+4+12=50+4*X - quantity 
                      cycles per cycle
                      programs (4*X -   
                      cycles occupied  
                      NOPs;  if desired
                      can be used
                      to command others
 yes, but the calculation can
                      turn out to beothers).
TPI*50   - number of clock cycles
                      Z80 per second (TPI 
                      - Tacts Per Inter-
                      rupt);  TPI is possible
                      measure with 
                      Jemmini commander
                      or some
                      programs.         
Rate*(4*X+50)   - quantity so-
                      product needed for you -
                      water rate byte
                      (i.e. 8000..44100  
                      byte), i.e. then co-
                      number of bytes
                      which must be
                      sound per second. 
                               
  Because  Rate is measured [bytes per second],
we get:                                
                               
Rate*(4*X+50)=TPI*50           
                               
  Using knowledge of mathematics, obtain-
I eat what I wanted:                       
                               
       25*(TPI-Rate)           
X=INT------------           
          2*Rate               
                               
  This is how you can calculate the quantity
NOP's. However, it should be taken into account that
There are different TPIs for different Spectrum models.
Therefore, if you decide to make a program
with your voice, then you better define
TPI software.                          
  By the way, *.WAV files are different:
reo, mono, PCM, ADPCM, etc.
The above applies to mono *.WAV
files in PCM format (unless I change
no RAM)...                               
  In my practice I also encountered files
*.AIF.  When I tried to play it
thus, nothing worked for me.
Analyzing the title, I saw that the length
file is recorded in Hi-end format, i.e. with
high byte from the beginning. I thought that
this is an Amiga file (or maybe it’s really an Amiga file)
ny?), but then I read in the book that it was
files "common for Apple Computer". Case
is that the data in such files contains
are pressed “with a sign” (signed sample), then
is in the form of numbers from -128 to +127 (1 -
is 1, and -1 is 255, etc.). How can we
bring the information to the required form? It is necessary
just “proxy” the data to #80 (easier
speaking - change the sign). This can be done
put right before OUT:.....                                                         
LOOP LD A,(HL) ; take a byte                           
        XOR #80 ;change sign                          
        OUT (#XX),A ; throw it where needed                 
.....                                                         
                               
 Well, there you go. Now you can voice your
shi programs. It’s also interesting to “chemically”
with sounds (do reverberation, etc.).
You can mix several sounds. This is the case-
it goes like this:                                
                               
.....                                                         
LOOP LD A,(DE) ;take a byte from sample #1                  
        ADD A,(HL) ;add it with the byte from sample #2    
                    ;(take any voice, put DE;to the beginning, and try putting HL on  
                    ;2000 bytes further DE, BC reduce by   
        RRA ;2000) divide the result by 2 (to put it simply, 
                    ;then we find the arithmetic mean).     
        OUT (#XX),A ;throw it where necessary                         
.....                                                         
                               
  Okay, we have learned how to issue
sounds, but what about AY? It’s difficult with him
her.  Firstly, AY has a 4-bit DAC, and
secondly, with an increase in N (ampl.) by
unity, the output signal increases
not 1/16 (!). Therefore, it is necessary to transform
convert sound from 8 bits to 4, and even according to special
al table.  This is done as follows
way:                                 
                               
.....                                                         
        LD BC,#FFFD ; first you need to set 8,9 or 10 
        LD A,8 ;register AY                           
        OUT (C),A                                             
        LD D,HB_TABLE ;the most significant byte of our
                      ;tables (address must be round,    
                      ;i.e. #8000, #7400, #8300, etc.)       
LOOP LD E,(HL) ;after this command in DE is      
                      ;address in the table containing the required
                      ;our number                              
        LD A,(DE) ;take this number                        
        OUT (#FD),A ; throw it in AY                        
.....                                                         
HB_TABLE DEFS 4,#A0 ;this table was taken from AY_PLAYER  
        DEFS 5,#A1 ;SAMPLE TRACKER v2.1 editor          
         DEFS 5,#A2                                           
        DEFS 5,#A3 ;table size25bytes                
  DEFS b,#A4                                           
         DEFS 7,#A5 ; it should be noted that this technique     
        DEFS 12,#Ab ;does not work on "old" scorpions due to  
        DEFS 12,#A7 ;too smart decoding;              
         DEFS 8,#A8 ; for everything to work, instead of numbers you need  
         DEFS 1b,#A9 ;#A0..#AF just write 0..15, and in    
        DEFS 24,#AA ;program to draw the correct conclusion in   
         DEFS 17,#AB ;AY: Ld bc,#bffd; out (c),a...          
         DEFS 23,#AC                                         
         DEFS 32,#AD                                         
         DEFS 39,#AE                                         
         DEFS 41,#AF                                         
                               
  You can also do without a table:
pocutb low 4 bits, but sound quality
it will be much worse.                  
                               
.....                                                         
LOOP LD A,(HL) ; take a byte                             
        AND #F0 ;we leave only the 4 most significant bits        
        RRCA ; move them to the place of the younger ones         
        RRCA                                                 
 RRCA                                                 
        RRCA                                                 
        OR #A0 ;we get the #AX byte (again for scorpions
                      ;rework required)                   
        OUT (#FD),A ; throw it in AY.....                                                         
                               
  Well, I told you almost everything that
I know myself. If you understand everything, then nothing
prevents you from playing around with sounds, but
if you also have an ADC (and more than one(?)),
then here you can fantasize endlessly
ness (overlaying a voice with an echo on some
some sound (voice for arrangement) in RE-
ALTIME, etc.).                          
  Of course, we cannot limit ourselves only to
to this material, because I wrote it, os-
learning from personal experience, and perhaps
someone will add and/or continue this
topic.                                    
                               
  Some info: ZX-SPECTRUM is generally the same
what a machine on which there is practically no
standard (I'm talking about Pentagon, Scorpion,
Kau, Profi, etc.): memory expansion
made differently, ROM - different, TPI...
Kovox is no exception either. At the Pentagon
it is usually connected to the #FB port (ZX-Lprint
III interface), on Scorp instead of #FB -
#DD, does anyone else have any
port  With the same success you can use
vat Soundrive and General Sound, only them
needs to be initialized. For a better pony
mania see table:                       
                               
      +------------------+---------------+-------------+      
      | название  железа | инициализация | порт вывода |      
      +------------------+---------------+-------------+      
      | Pentagon's COVOX | ------------- |     #FB     |      
      +------------------+---------------+-------------+      
      | Scorpion's COVOX | ------------- |     #DD     |      
      +------------------+---------------+-------------+      
      | Profi COVOX     | out (#7F),#80 |   #3F #5F   |      
      +------------------+---------------+-------------|      
      | Flash Soundrive  | out (#3F),#80 |   #0F #1F   |      
      |                  | out (#7F),#80 |   #4F #5F   |      
      +------------------+---------------+-------------+      
      | General Sound    | out (#BB),#0E |     #B3     |      
      +------------------+---------------+-------------+      
       I’ll explain for those who are not quite clear -
was angry. At the very beginning of the program you must
initialize the device on which
data will be transmitted to the swarm (if it is
ordinary covox, then there is nothing to do
before).  Then in our program instead of #XX
set the port indicated in the table. For example
measures:                                     
                               
        DI                                                   
        LD A,#80 ;initialize                         
        OUT (#3F),A ;SOUNDRIVE                              
        OUT (#7F),A                                           
                               
        LD HL,SMP_ADR                                         
        LD BC,SMP_LEN                                         
LOOP LD A,(HL)                                             
        OUT (#0F),A ; instead of #0F there can be #1F, #4F, or #5FDEFS 28       ;или любое другое число                 
        INC HL                                                
        DEC BC                                                
        LD A,B                                                
        OR C                                                  
        JR NZ,LOOP                                            
                               
        EI                                                    
        RET                                                   
                               
  General Sound тоже может работать в ре-
жиме  ковокса,  но у него дело со звуками
обстоит побогаче. Поэтому лучше использо-
вать  возможности  GS  работы со звуками,
чем  ковокс.  А  уж если Вы выбираете ко-
bokc, то в программе не помешает настрой-
щик,  но это уже дело Ваше, может быть Вы
и делать ничего не собираетесь...        
  Вот,  собственно,  и  все. Я надеюсь Вы
все  поняли  изthe above, and maybe
maybe someone will tell you how to do this
designed effects with sounds...           
                               
                   With best wishes, PSB
  of Halloween.
________________________________

Share your thoughts about the article