The paper presents a two-layered system for learning and encoding a periodic signal and its application to a drumming task. The two layers are the dynamical system responsible for extracting the main frequency of the input signal, based on adaptive frequency oscillators, and the dynamical system responsible for learning of the waveform with a built in learning algorithm. By combining the two dynamical systems we can rapidly teach new trajectories to robots. The systems works online for any periodic signal, requires no signal processing and can be applied in parallel to multiple dimensions. Furthermore, it can adapt to changes in frequency and shape, e.g. to non-stationary signals, and is computationally inexpensive. The algorithm is demonstrated in a drumming task using the HOAP-2 humanoid robot.