Sonic π ︰ 秘境《丁》

假使你曾經為電玩遊戲之

晶片音樂

晶片音樂英文:Chiptune),也被稱為8bit音樂,是一種電子音樂形式,形成於1980年代。它利用老式電腦電動遊戲機街機等的音樂晶片,或者使用仿真器製作。[1] 晶片音樂一般包括基本波形 ,如方波鋸齒波三角波和基本的打擊樂器。

使用Game Boy播放晶片音樂的裝置組態

誕生

早期的遊戲音樂不像現在可以使用商業授權的CD音質的錄音,因為當時的遊戲機沒有條件回放高解析度的PCM錄音。所以遊戲音樂就需要實時合成,必須將基本的聲音合成引擎植入硬體當中。[2]晶片音樂由此產生。

 

聲音所吸引,難到不想知道它的奧秘乎?

Making Chiptune Music using Sonic Pi v2.0

Warning: this might not work on a RaspberryPi yet

※ 註︰ sonic π v. 3.0.1 OK

I was curious about making retro gaming sounds using Sonic Pi. A couple of months and a lot of Googling later, here’s the original Mario Bros theme as it was heard on the NES console.

I’m (just about) old enough to remember rushing home from school to play this game at Philip Boucher’s house, sitting cross-legged in front of the TV till my feet got pins and needles. Working out how to recreate it for Sonic Pi was a lot of fun!

Getting the sounds of the NES chip

I’m no expert on this, but the sounds of the NES chip boils down to 4 kinds of sound:

  • pulse wave A (AKA square wave)
  • pulse wave B
  • triangle wave C
  • noise D

With just these four synths the games programmers managed to make a staggering variety of sounds. If you want to compose your own chiptunes just fire up Sonic Pi with four threads like this:

in_thread do
  use_synth :pulse
  play 60
end

in_thread do
  use_synth :pulse
  play 60
end

in_thread do
  use_synth :tri
  play 60
end

in_thread do
  use_synth :fm
  use_synth_defaults divisor: 1.6666, attack: 0.0, depth: 1500, sustain: 0.05, release: 0.0
  play 60
end

 

For the noise channel, you could use one of the noise synths that have been added in v2.0 but I’ve used a ‘noisy’ FM synth instead. The reason is that some of the drum-type sounds from the Mario theme are achieved by changing the note given to the noise chip, which produces a slightly different kind of noise. I’m not 100% sure but I think the original NES was using an FM type osciallator too.

 

在欣賞了『瑪利歐』之後︰

its_a_me_mario.rb

comment do
# transcribed from the MML notation here: http://www.mmlshare.com/tracks/view/403
#
# Sonic Pi currently has a size limit of about 9k which is a known issue (#102).
# I’ve kept the comments up here to get around that as comment blocks don’t get
# sent to the interpreter. Some of the layout here is an exercise in reducing bytes.
# I’m using Ruby’s stabby lambda syntax ( -> { … } ) in case you want to google it 🙂
#
# THIS HAS ONLY BEEN TESTED ON A MAC – on an RaspberryPi you might want to change it to
# use_bpm 60
#
# Regarding the choice of an FM synth for drums:
# You could use a noise synth here, but I think the NES sound
# chip would have used something like this FM as the character
# of the noise would change with different notes which I’m making
# use of in drum_pattern_b
end

 

或許會想把它錄起來吧!

Hi,
Sonic Pi sounds can be recorded into a .wav file. Can this recording be somehow started / stopped programmatically? I’d like to be able to send an osc message for “start recording” and another one for “stop recording”.
If this is not possible out of the box, any workarounds (external Python script that does it etc) would be great.

Thanks,
Avishay

……

I had a play with them this evening and found that the following code worked, which saves a demo 10 second wav file. I used Sonic Pi to send osc messages to itself on port 4557. The commands are as documented by@bahamut657 above, but require some further detail First each command has to be followed by a GUID value which you choose. I used the string “myGUID”. The start and stop recording commands need no further info, but the save-recording commands needs the full pathname to where you want the file to be saved.

osc_send "localhost",4557, "/start-recording","myGUID"
#generate some sound to record
95.times do
  play scale(:c4,:major,num_octaves:2).choose,release: 0.1
  sleep 0.1
end
#stop recording
osc_send "localhost",4557, "/stop-recording","myGUID"
sleep 0.5
#change the path in the next command to suit your system and usernames
osc_send "localhost",4557, "/save-recording","myGUID","/Users/rbn/testfile.wav"

 

port 4557. The specimen file had 9.5 seconds of random notes. You can play the resulting file using a suitable player eg audacity.

All of the above uses undocumented internals of Sonic Pi and is not supported for use in this way, so use at your own risk. (You can see where the commands are set up in the file sonic-pi-server.rb which can be seen at https://github.com/samaaron/sonic-pi/blob/master/app/server/ruby/bin/sonic-pi-server.rb 5 in the Sonic Pi github code. (Note this is in a different location to that employed in the current releases of Sonic Pi).

………

 

故而試錄了一小段,喜與讀者分享也☺

      1. testfile