Week3
Since the musical instrument hardware part has taken shape, this week we will focus on optimizing the code logic to improve sound quality and enrich functions. Posters as well as sustainability reports are also discussed during the week.
This week, our key tasks are as follows:
1. Implement the code for piano playing.
2. Discuss the production of posters, including modules and preliminary content settings.
3. Discuss the content of the sustainability report.
First, the instrument we initially want to implement is piano.
We
chose python as the coding language. Due to the unsatisfactory results of the
buzzer test last week, we decided to complete the playback directly on the PC
this week.
The above code completes the use of the controller to control audio playback, and then controls the pause and resume through the infrared sensor. The first step of communication performance test is completed. At the same time, the main calculations, audio processing and playback are completed on the PC to achieve more functions.
In the second step, we optimized the sound quality.
According to the sound characteristics of the piano instrument, some detailed issues need to be solved, including sustain, polyphony and trigger time issues. Make the simulated piano sound meet all natural characteristics.
Then to deal with pitch we tried various solutions.
Option 1: Play through the pygame library. All sounds of 88 tones need to be stored in advance. However, the number of recordings and required files is mixed, which is quite troublesome.
Option 2: Choose other libraries, such as pydub and pyaudio. The process requires converting the file into files of different frequencies, and then using pygame to read the file. With the continuous dynamic change process, more and more audio files with different frequencies will be generated, and frequent hard disk reads will consume resources.
Option 3: Dynamically calculate the frequency and then output it. This approach cannot be played using the pygame library, because pygame can only play files, not memory data.
We ended up using NumPy arrays with the pygame.sndarray module solution.
This method does not require writing data to a file and can operate directly in memory. This avoids the 1/0 overhead of using temporary files. Below is a picture of part code for how to use this approach.
As for the final piano demonstration, we successfully played the song Little Star.





Comments
Post a Comment