![]() Next we have the block that tells the code to wait 6 seconds before pausing the file. Inside our loop, we start by playing the file. The audio file is now ready to go, and can be played at any time with ay(wave)! Now we will ask the audio playback system to load the wave data from the file wave = audiocore.WaveFile(wave_file) and finally request that the audio is played through the A0 analog output pin audio = audioio.AudioOut(board.A0). Next we then open the file, "StreetChicken.wav" as a readable binary and store the file object in wave_file which is what we use to actually read audio from: wave_file = open("StreetChicken.wav", "rb"). Print("Waiting for button press to continue!")įirst we create the button object, assign it to pin A1, and set it as an input with a pull-up. # This allows you to do other things while the audio plays! Wave_file = open("StreetChicken.wav", "rb") """CircuitPython Essentials Audio Out WAV example""" You can change it to whatever works for you! You can increase or decrease the length of time it plays by increasing or decreasing the number of seconds provided to time.sleep(). ![]() The time.sleep(1) tells it to loop (play) for 1 second. Once the button is pressed, we play the sample we created and we loop it. So, we check to see if not button.value which is the equivalent of not True, or False. So, to check if it has been pressed, we're looking for the False state. The button.value defaults to the True state when not pressed. The button has two states True and False. Inside our loop, we check to see if the button is pressed. We create a sample of the sine wave by using RawSample and providing the sine_wave we created. Next, we create the audio object, and assign it to pin A0. Then, we generate one period of a sine wave with the math.sin function, and assign it to sine_wave. To set the frequency of the generated tone, change the number assigned to the frequency variable to the Hz of the tone you'd like to generate. You can also control volume with the potentiometer by rotating the knob. You can use the code to control the volume by increasing or decreasing this number to increase or decrease the volume. Since the default volume was incredibly high, we included a tone_volume variable in the sine wave code. Even though the button switch involves digitalio, we're using an A-pin so that the same setup code will work across all the boards. # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industriesįirst we create the button object, assign it to pin A1, and set it as an input with a pull-up. Tone_volume = 0.1 # Increase this to increase the volume of the tone.įrequency = 440 # Set this to the Hz of the tone you want to generate. Pass # not always supported by every board!īutton = digitalio.DigitalInOut(board.A1)īutton.switch_to_input(pull=) """CircuitPython Essentials Audio Out tone example"""įrom audiopwmio import PWMAudioOut as AudioOut Our full article on PyAudio covers additional functions and explains how to record sound.Copy Code # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries It’s a useful function to know if you want to stop the stream half way through or something. The stop_stream() function isn’t actually needed here as by the time the code arrives at it’s location, the stream has already finished. Using commands like file.getnchannels() we extract the relevant data from the file and use it to create the appropriate audio stream. To create a stream, you need to define certain things such as the number of channels, the sampling rate etc. # Play the sound by writing the audio data to the stream Stream = p.open(format = p.get_format_from_width(file.getsampwidth()), # Setting output to "True" makes the sound be "played" rather than recorded # Creates a Stream to which the wav file is written to. # Defines a chunk size of 1024 samples per data frame.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |